Skip to content

Commit e59d1ab

Browse files
committed
Add DataSourceConfig setName() and build() method to build DataSourcePool
Then the DataSourcePool can be created via build() rather than using the DataSourceFactory explicitly like: DataSourcePool pool = new DataSourceConfig() .setName("test") .setUrl("jdbc:h2:mem:tests") .setUsername("sa") .setPassword("") .build();
1 parent d8c4033 commit e59d1ab

2 files changed

Lines changed: 44 additions & 23 deletions

File tree

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@
1212
*
1313
* <pre>{@code
1414
*
15-
* DataSourceConfig config = new DataSourceConfig()
16-
* .setUrl("jdbc:postgresql://127.0.0.1:5432/unit");
17-
* .setUsername("foo");
18-
* .setPassword("bar");
19-
*
20-
* DataSource pool = DataSourceFactory.create("app", config);
15+
* DataSourcePool pool = new DataSourceConfig()
16+
* .setName("test")
17+
* .setUrl("jdbc:h2:mem:tests")
18+
* .setUsername("sa")
19+
* .setPassword("")
20+
* .build();
2121
*
2222
* }</pre>
2323
*/
2424
public class DataSourceConfig {
2525

2626
private static final String POSTGRES = "postgres";
2727

28+
private String name = "";
2829
private String readOnlyUrl;
2930
private String url;
3031
private String username;
@@ -160,6 +161,31 @@ public boolean isEmpty() {
160161
&& password == null;
161162
}
162163

164+
/**
165+
* Build and return the DataSourcePool.
166+
* <pre>{@code
167+
*
168+
* DataSourcePool pool = new DataSourceConfig()
169+
* .setName("test")
170+
* .setUrl("jdbc:h2:mem:tests")
171+
* .setUsername("sa")
172+
* .setPassword("")
173+
* .build();
174+
*
175+
* }</pre>
176+
*/
177+
public DataSourcePool build() {
178+
return DataSourceFactory.create(name, this);
179+
}
180+
181+
/**
182+
* Set the data source pool name.
183+
*/
184+
public DataSourceConfig setName(String name) {
185+
this.name = name;
186+
return this;
187+
}
188+
163189
/**
164190
* Return the clientInfo ApplicationName property.
165191
*/

ebean-datasource/src/test/java/io/ebean/datasource/test/FactoryTest.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111
import java.sql.ResultSet;
1212
import java.sql.SQLException;
1313

14+
@SuppressWarnings({"SqlDialectInspection", "SqlNoDataSourceInspection"})
1415
public class FactoryTest {
1516

1617
@Test
1718
public void createPool() throws Exception {
1819

19-
DataSourceConfig config = new DataSourceConfig();
20-
config.setDriver("org.h2.Driver");
21-
config.setUrl("jdbc:h2:mem:tests");
22-
config.setUsername("sa");
23-
config.setPassword("");
24-
25-
DataSourcePool pool = DataSourceFactory.create("test", config);
20+
DataSourcePool pool = new DataSourceConfig()
21+
.setName("test")
22+
.setUrl("jdbc:h2:mem:tests")
23+
.setUsername("sa")
24+
.setPassword("")
25+
.build();
2626

2727
try (Connection connection = pool.getConnection()) {
2828
try (PreparedStatement stmt = connection.prepareStatement("create table junk (acol varchar(10))")) {
@@ -35,25 +35,20 @@ public void createPool() throws Exception {
3535
@Test
3636
public void dataSourceFactory_get_createPool() throws Exception {
3737

38-
39-
DataSourceConfig config = new DataSourceConfig();
40-
config.setDriver("org.h2.Driver");
41-
config.setUrl("jdbc:h2:mem:tests2");
42-
config.setUsername("sa");
43-
config.setPassword("");
44-
45-
DataSourcePool pool = DataSourceFactory.create("test", config);
38+
DataSourcePool pool = new DataSourceConfig()
39+
.setUrl("jdbc:h2:mem:tests2")
40+
.setUsername("sa")
41+
.setPassword("")
42+
.build();
4643

4744
try (Connection connection = pool.getConnection()) {
4845
try (PreparedStatement stmt = connection.prepareStatement("create table junk (acol varchar(10))")) {
4946
stmt.execute();
5047
connection.commit();
5148
}
5249
}
53-
5450
}
5551

56-
5752
@Test
5853
public void testPreparedStatement() throws Exception {
5954

0 commit comments

Comments
 (0)