From f192939db6a920bfd1d3a65ad24f66a3a0283582 Mon Sep 17 00:00:00 2001 From: Gabriel Roldan Date: Tue, 19 Apr 2022 15:11:46 -0300 Subject: [PATCH] Do allow to configure jdbcconfig datasource connection limits minimumIdle: minimum number of connections in the pool maximumPoolSize: maximum number of connection in the pool connectionTimeout: connection time out in milliseconds (defaults to 250, which is also the minimum value) idleTimeout: time in milliseconds before closing idle connections (defaults to 60_000) --- .../JDBCConfigBackendConfigurer.java | 31 +++++++---- .../backend/JdbcConfigDataSourceTest.java | 53 +++++++++++++++++++ 2 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 src/starters/starter-catalog-backend/src/test/java/org/geoserver/cloud/autoconfigure/test/backend/JdbcConfigDataSourceTest.java diff --git a/src/starters/starter-catalog-backend/src/main/java/org/geoserver/cloud/config/jdbcconfig/JDBCConfigBackendConfigurer.java b/src/starters/starter-catalog-backend/src/main/java/org/geoserver/cloud/config/jdbcconfig/JDBCConfigBackendConfigurer.java index 7f20c1962..f96963b62 100644 --- a/src/starters/starter-catalog-backend/src/main/java/org/geoserver/cloud/config/jdbcconfig/JDBCConfigBackendConfigurer.java +++ b/src/starters/starter-catalog-backend/src/main/java/org/geoserver/cloud/config/jdbcconfig/JDBCConfigBackendConfigurer.java @@ -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; @@ -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 diff --git a/src/starters/starter-catalog-backend/src/test/java/org/geoserver/cloud/autoconfigure/test/backend/JdbcConfigDataSourceTest.java b/src/starters/starter-catalog-backend/src/test/java/org/geoserver/cloud/autoconfigure/test/backend/JdbcConfigDataSourceTest.java new file mode 100644 index 000000000..e02cf4bf4 --- /dev/null +++ b/src/starters/starter-catalog-backend/src/test/java/org/geoserver/cloud/autoconfigure/test/backend/JdbcConfigDataSourceTest.java @@ -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()); + } + } +}