Skip to content

Commit 3c7eb73

Browse files
authored
Merge pull request #96 from zonkyio/configurable-database-connections
#93 Make max_connections property configurable for all providers
2 parents 84723de + 2b33f94 commit 3c7eb73

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/impl/OpenTablePostgresDatabaseProvider.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.commons.lang3.RandomStringUtils;
3333
import org.springframework.beans.factory.ObjectProvider;
3434
import org.springframework.core.env.Environment;
35+
import org.springframework.jdbc.core.JdbcTemplate;
3536

3637
import javax.sql.DataSource;
3738
import java.io.IOException;
@@ -51,8 +52,6 @@
5152

5253
public class OpenTablePostgresDatabaseProvider implements DatabaseProvider {
5354

54-
private static final int MAX_DATABASE_CONNECTIONS = 300;
55-
5655
private static final LoadingCache<DatabaseConfig, DatabaseInstance> databases = CacheBuilder.newBuilder()
5756
.build(new CacheLoader<DatabaseConfig, DatabaseInstance>() {
5857
public DatabaseInstance load(DatabaseConfig config) throws IOException {
@@ -122,7 +121,12 @@ private DatabaseInstance(DatabaseConfig config) throws IOException {
122121
config.applyTo(builder);
123122

124123
postgres = builder.start();
125-
semaphore = new Semaphore(MAX_DATABASE_CONNECTIONS);
124+
125+
DataSource dataSource = postgres.getDatabase("postgres", "postgres");
126+
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
127+
Integer maxConnections = jdbcTemplate.queryForObject("show max_connections", Integer.class);
128+
129+
semaphore = new Semaphore(maxConnections);
126130
}
127131

128132
public DatabaseTemplate getTemplate(ClientConfig config, DatabasePreparer preparer) {
@@ -204,11 +208,11 @@ private DatabaseConfig(Map<String, String> initdbProperties, Map<String, String>
204208
}
205209

206210
public final void applyTo(EmbeddedPostgres.Builder builder) {
211+
builder.setServerConfig("max_connections", "300");
207212
builder.setPGStartupWait(Duration.ofSeconds(20L));
208213
initdbProperties.forEach(builder::setLocaleConfig);
209214
configProperties.forEach(builder::setServerConfig);
210215
customizers.forEach(c -> c.accept(builder));
211-
builder.setServerConfig("max_connections", String.valueOf(MAX_DATABASE_CONNECTIONS));
212216
}
213217

214218
@Override

embedded-database-spring-test/src/main/java/io/zonky/test/db/provider/impl/ZonkyPostgresDatabaseProvider.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.commons.lang3.RandomStringUtils;
3333
import org.springframework.beans.factory.ObjectProvider;
3434
import org.springframework.core.env.Environment;
35+
import org.springframework.jdbc.core.JdbcTemplate;
3536

3637
import javax.sql.DataSource;
3738
import java.io.IOException;
@@ -51,8 +52,6 @@
5152

5253
public class ZonkyPostgresDatabaseProvider implements DatabaseProvider {
5354

54-
private static final int MAX_DATABASE_CONNECTIONS = 300;
55-
5655
private static final LoadingCache<ClusterKey, DatabaseInstance> databases = CacheBuilder.newBuilder()
5756
.build(new CacheLoader<ClusterKey, DatabaseInstance>() {
5857
public DatabaseInstance load(ClusterKey key) throws IOException {
@@ -126,7 +125,12 @@ private DatabaseInstance(DatabaseConfig config) throws IOException {
126125
config.applyTo(builder);
127126

128127
postgres = builder.start();
129-
semaphore = new Semaphore(MAX_DATABASE_CONNECTIONS);
128+
129+
DataSource dataSource = postgres.getDatabase("postgres", "postgres");
130+
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
131+
Integer maxConnections = jdbcTemplate.queryForObject("show max_connections", Integer.class);
132+
133+
semaphore = new Semaphore(maxConnections);
130134
}
131135

132136
public DatabaseTemplate getTemplate(ClientConfig config, DatabasePreparer preparer) {
@@ -210,11 +214,11 @@ private DatabaseConfig(Map<String, String> initdbProperties, Map<String, String>
210214
}
211215

212216
public final void applyTo(EmbeddedPostgres.Builder builder) {
217+
builder.setServerConfig("max_connections", "300");
213218
builder.setPGStartupWait(Duration.ofSeconds(20L));
214219
initdbProperties.forEach(builder::setLocaleConfig);
215220
configProperties.forEach(builder::setServerConfig);
216221
customizers.forEach(c -> c.accept(builder));
217-
builder.setServerConfig("max_connections", String.valueOf(MAX_DATABASE_CONNECTIONS));
218222
}
219223

220224
@Override

embedded-database-spring-test/src/test/java/io/zonky/test/db/config/ConfigurationPropertiesIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void testConfigurationProperties() throws Exception {
5454
assertThat(collate).isEqualTo("cs_CZ.UTF-8");
5555

5656
String maxConnections = jdbcTemplate.queryForObject("show max_connections", String.class);
57-
assertThat(maxConnections).isEqualTo("300"); // this is a default value that can not be changed
57+
assertThat(maxConnections).isEqualTo("100");
5858

5959
String sharedBuffers = jdbcTemplate.queryForObject("show shared_buffers", String.class);
6060
assertThat(sharedBuffers).isEqualTo("64MB");

embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/impl/OpenTablePostgresDatabaseProviderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void testConfigurationProperties() throws SQLException {
136136
assertThat(collate).isEqualTo("cs_CZ.UTF-8");
137137

138138
String maxConnections = jdbcTemplate.queryForObject("show max_connections", String.class);
139-
assertThat(maxConnections).isEqualTo("300");
139+
assertThat(maxConnections).isEqualTo("100");
140140

141141
String sharedBuffers = jdbcTemplate.queryForObject("show shared_buffers", String.class);
142142
assertThat(sharedBuffers).isEqualTo("64MB");

embedded-database-spring-test/src/test/java/io/zonky/test/db/provider/impl/ZonkyPostgresDatabaseProviderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public void testConfigurationProperties() throws SQLException {
162162
assertThat(collate).isEqualTo("cs_CZ.UTF-8");
163163

164164
String maxConnections = jdbcTemplate.queryForObject("show max_connections", String.class);
165-
assertThat(maxConnections).isEqualTo("300");
165+
assertThat(maxConnections).isEqualTo("100");
166166

167167
String sharedBuffers = jdbcTemplate.queryForObject("show shared_buffers", String.class);
168168
assertThat(sharedBuffers).isEqualTo("64MB");

0 commit comments

Comments
 (0)