Skip to content

Commit 00ff3cb

Browse files
committed
Fix for DataSourceConfig.setDefaults() when using 2 and 200 for min and max connections
The included test fails without this change. For example, setting maxConnections 200 explicitly can then be "undone" by setDefaults() because 200 is also the magic default for maxConnections. Using the UNSET -1 value allows it to know when min/max connections have been explicitly set.
1 parent eea5673 commit 00ff3cb

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

ebean-datasource-api/src/main/java/io/ebean/datasource/DataSourceBuilder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ default DataSourceBuilder readOnly(boolean readOnly) {
317317
DataSourceBuilder setReadOnly(boolean readOnly);
318318

319319
/**
320-
* Set the minimum number of connections the pool should maintain.
320+
* Set the minimum number of connections the pool should maintain. Defaults to 2 when not set.
321321
*/
322322
default DataSourceBuilder minConnections(int minConnections) {
323323
return setMinConnections(minConnections);
@@ -330,7 +330,7 @@ default DataSourceBuilder minConnections(int minConnections) {
330330
DataSourceBuilder setMinConnections(int minConnections);
331331

332332
/**
333-
* Set the maximum number of connections the pool can reach.
333+
* Set the maximum number of connections the pool can reach. Defaults to 200 when not set.
334334
*/
335335
default DataSourceBuilder maxConnections(int maxConnections) {
336336
return setMaxConnections(maxConnections);
@@ -892,12 +892,12 @@ default String driverClassName() {
892892
boolean isReadOnly();
893893

894894
/**
895-
* Return the minimum number of connections the pool should maintain.
895+
* Return the minimum number of connections the pool should maintain. Defaults to 2.
896896
*/
897897
int getMinConnections();
898898

899899
/**
900-
* Return the maximum number of connections the pool can reach.
900+
* Return the maximum number of connections the pool can reach. Defaults to 200.
901901
*/
902902
int getMaxConnections();
903903

ebean-datasource-api/src/main/java/io/ebean/datasource/DataSourceConfig.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
@SuppressWarnings("removal")
2828
public class DataSourceConfig implements DataSourceBuilder.Settings {
2929

30+
private static final int UNSET = -1;
3031
private static final String POSTGRES = "postgres";
3132

3233
private String name = "";
@@ -54,8 +55,8 @@ public class DataSourceConfig implements DataSourceBuilder.Settings {
5455
* The optional database owner password (for running InitDatabase).
5556
*/
5657
private String ownerPassword;
57-
private int minConnections = 2;
58-
private int maxConnections = 200;
58+
private int minConnections = UNSET; // defaults to 2
59+
private int maxConnections = UNSET; // defaults to 200
5960
private int isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
6061
private boolean autoCommit;
6162
private boolean readOnly;
@@ -182,10 +183,10 @@ public DataSourceConfig setDefaults(DataSourceBuilder builder) {
182183
if (catalog == null) {
183184
catalog = other.catalog();
184185
}
185-
if (minConnections == 2 && other.getMinConnections() < 2) {
186+
if (minConnections == UNSET) {
186187
minConnections = other.getMinConnections();
187188
}
188-
if (maxConnections == 200 && other.getMaxConnections() != 200) {
189+
if (maxConnections == UNSET) {
189190
maxConnections = other.getMaxConnections();
190191
}
191192
if (!shutdownOnJvmExit && other.isShutdownOnJvmExit()) {
@@ -413,7 +414,7 @@ public DataSourceConfig setReadOnly(boolean readOnly) {
413414

414415
@Override
415416
public int getMinConnections() {
416-
return minConnections;
417+
return minConnections == UNSET ? 2 : minConnections;
417418
}
418419

419420
@Override
@@ -424,7 +425,7 @@ public DataSourceConfig setMinConnections(int minConnections) {
424425

425426
@Override
426427
public int getMaxConnections() {
427-
return maxConnections;
428+
return maxConnections == UNSET ? 200 : maxConnections;
428429
}
429430

430431
@Override

ebean-datasource-api/src/test/java/io/ebean/datasource/DataSourceConfigTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,19 @@ void setDefaults_when_explicit() {
179179
assertThat(readOnly.getMaxConnections()).isEqualTo(22);
180180
}
181181

182+
@Test
183+
void setDefaults_when_explicitSameAsNormalDefaults() {
184+
DataSourceConfig readOnly = new DataSourceConfig();
185+
readOnly.setMinConnections(2);
186+
readOnly.setMaxConnections(200);
187+
188+
// act
189+
readOnly.setDefaults(create());
190+
191+
assertThat(readOnly.getMinConnections()).isEqualTo(2);
192+
assertThat(readOnly.getMaxConnections()).isEqualTo(200);
193+
}
194+
182195
@Test
183196
public void defaults_someOverride() {
184197
DataSourceConfig readOnly = new DataSourceConfig();

0 commit comments

Comments
 (0)