Skip to content

Commit

Permalink
Merge pull request #221 from groldan/bug/jdbcconfig_pool_size
Browse files Browse the repository at this point in the history
Do allow to configure jdbcconfig datasource connection limits
  • Loading branch information
groldan authored Apr 19, 2022
2 parents bc0e85f + f192939 commit 575287e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.google.common.cache.CacheBuilder;
import com.zaxxer.hikari.HikariDataSource;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -356,21 +358,32 @@ public StoreHelper(DataSource ds) {
return jdbcConfigDataSourceValidator;
}

@Data
@EqualsAndHashCode(callSuper = true)
public static class ExtendedDataSourceProperties extends DataSourceProperties {
int minimumIdle = 2;
int maximumPoolSize = 10;
long connectionTimeout = 250; // ms
long idleTimeout = 60_000; // ms
}

@Bean
@ConfigurationProperties("geoserver.backend.jdbcconfig.datasource")
public DataSourceProperties jdbcconfigDataSourceProperties() {
return new DataSourceProperties();
public ExtendedDataSourceProperties jdbcconfigDataSourceProperties() {
return new ExtendedDataSourceProperties();
}

@Bean(name = {"jdbcConfigDataSource", "jdbcStoreDataSource"})
// @ConfigurationProperties(prefix = "geoserver.backend.jdbcconfig.datasource")
@ConfigurationProperties(prefix = "geoserver.backend.jdbcconfig.datasource")
public DataSource jdbcConfigDataSource() {
return jdbcconfigDataSourceProperties()
.initializeDataSourceBuilder()
.type(HikariDataSource.class)
.build();
// String url = env.getProperty("geoserver.backend.jdbcconfig.datasource.jdbc-url");
// return DataSourceBuilder.create().build();
ExtendedDataSourceProperties props = jdbcconfigDataSourceProperties();
HikariDataSource dataSource =
props.initializeDataSourceBuilder().type(HikariDataSource.class).build();
dataSource.setMaximumPoolSize(props.getMaximumPoolSize());
dataSource.setMinimumIdle(props.getMinimumIdle());
dataSource.setConnectionTimeout(props.getConnectionTimeout());
dataSource.setIdleTimeout(props.getIdleTimeout());
return dataSource;
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* (c) 2022 Open Source Geospatial Foundation - all rights reserved This code is licensed under the
* GPL 2.0 license, available at the root application directory.
*/
package org.geoserver.cloud.autoconfigure.test.backend;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;

import com.zaxxer.hikari.HikariDataSource;

import org.geoserver.cloud.autoconfigure.testconfiguration.AutoConfigurationTestConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLTransientConnectionException;

import javax.sql.DataSource;

@SpringBootTest(
classes = AutoConfigurationTestConfiguration.class,
properties = {
"geoserver.backend.jdbcconfig.enabled=true",
"geoserver.backend.jdbcconfig.datasource.maximumPoolSize=2",
"geoserver.backend.jdbcconfig.datasource.minimumIdle=1",
"geoserver.backend.jdbcconfig.datasource.connectionTimeout=250", // 250ms
"geoserver.backend.jdbcconfig.datasource.idleTimeout=10000", // 10 secs
})
@RunWith(SpringRunner.class)
public class JdbcConfigDataSourceTest extends JDBCConfigTest {

public @Test void testDataSource() throws SQLException {
DataSource ds = context.getBean("jdbcConfigDataSource", DataSource.class);
assertSame(ds, context.getBean("jdbcStoreDataSource", DataSource.class));
assertThat(ds, instanceOf(HikariDataSource.class));
HikariDataSource hds = (HikariDataSource) ds;
assertEquals(2, hds.getMaximumPoolSize());
assertEquals(1, hds.getMinimumIdle());
assertEquals(10_000, hds.getIdleTimeout());
assertEquals(250, hds.getConnectionTimeout());
try (Connection c1 = hds.getConnection();
Connection c2 = hds.getConnection()) {
assertThrows(SQLTransientConnectionException.class, () -> hds.getConnection());
}
}
}

0 comments on commit 575287e

Please sign in to comment.