Skip to content

Commit 68557fd

Browse files
authored
Merge pull request #640 from jooby-project/630
jdbc/hikari & flywaydb: rename properties for multi-database usage fi…
2 parents ecd3448 + 530a57e commit 68557fd

File tree

10 files changed

+177
-19
lines changed

10 files changed

+177
-19
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.jooby.issues;
2+
3+
import javax.sql.DataSource;
4+
5+
import org.jooby.jdbc.Jdbc;
6+
import org.jooby.test.ServerFeature;
7+
import org.junit.Test;
8+
9+
import com.typesafe.config.ConfigFactory;
10+
import com.typesafe.config.ConfigValueFactory;
11+
12+
public class Issue622 extends ServerFeature {
13+
14+
{
15+
use(ConfigFactory.empty()
16+
.withValue("db.spock.url", ConfigValueFactory
17+
.fromAnyRef("jdbc:h2:mem:spock;DB_CLOSE_DELAY=-1"))
18+
.withValue("db.spock.user", ConfigValueFactory.fromAnyRef("sa"))
19+
.withValue("db.spock.password", ConfigValueFactory.fromAnyRef(""))
20+
.withValue("db.spock.hikari.maximumPoolSize", ConfigValueFactory.fromAnyRef(100))
21+
.withValue("db.spock.hikari.autoCommit", ConfigValueFactory.fromAnyRef(true))
22+
.withValue("db.vulcan.url", ConfigValueFactory
23+
.fromAnyRef("jdbc:h2:mem:vulcan;DB_CLOSE_DELAY=-1"))
24+
.withValue("db.vulcan.user", ConfigValueFactory.fromAnyRef("sa"))
25+
.withValue("db.vulcan.password", ConfigValueFactory.fromAnyRef(""))
26+
.withValue("db.vulcan.hikari.maximumPoolSize", ConfigValueFactory.fromAnyRef(50))
27+
.withValue("db.vulcan.hikari.autoCommit", ConfigValueFactory.fromAnyRef(true)));
28+
29+
use(new Jdbc("db.spock"));
30+
use(new Jdbc("db.vulcan"));
31+
32+
get("/622", () -> {
33+
require("spock", DataSource.class);
34+
require("vulcan", DataSource.class);
35+
return "OK";
36+
});
37+
}
38+
39+
@Test
40+
public void bootstrap2DbsWithCustomSetup() throws Exception {
41+
request()
42+
.get("/622")
43+
.expect("OK");
44+
}
45+
}

coverage.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
#!/bin/bash
12
mvn -Dlogback.configurationFile=logback-travis.xml -DdryRun=true clean package coveralls:report -P coverage

jooby-jdbc/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@
7373
<classifier>tests</classifier>
7474
</dependency>
7575

76+
<dependency>
77+
<groupId>org.jooby</groupId>
78+
<artifactId>jooby-netty</artifactId>
79+
<version>${project.version}</version>
80+
<scope>test</scope>
81+
</dependency>
82+
83+
<!-- mySQL -->
84+
<dependency>
85+
<groupId>com.h2database</groupId>
86+
<artifactId>h2</artifactId>
87+
<scope>test</scope>
88+
</dependency>
89+
7690
</dependencies>
7791

7892
</project>

jooby-jdbc/src/main/java/org/jooby/jdbc/Jdbc.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,12 @@ private HikariConfig hikariConfig(final String url, final String key, final Stri
411411
props.setProperty(propertyName, propertyValue);
412412
};
413413

414-
Function<String, Config> hikari = path -> Try.of(() -> config.getConfig(path))
414+
Function<String, Config> dbconf = path -> Try.of(() -> config.getConfig(path))
415415
.getOrElse(ConfigFactory.empty());
416416

417-
String dbkey = key.replace("db.", "");
418-
Config $hikari = hikari.apply("hikari." + dbkey)
419-
.withFallback(hikari.apply("hikari." + db))
420-
.withFallback(hikari.apply("hikari"))
421-
.withoutPath(dbkey)
422-
.withoutPath(db);
417+
Config $hikari = dbconf.apply(key + ".hikari")
418+
.withFallback(dbconf.apply("db." + db + ".hikari"))
419+
.withFallback(dbconf.apply("hikari"));
423420

424421
// figure it out db type.
425422
dbtype = dbtype(url, config);
@@ -434,8 +431,8 @@ private HikariConfig hikariConfig(final String url, final String key, final Stri
434431
dbtype.ifPresent(type -> config.getConfig("databases." + type)
435432
.entrySet().forEach(entry -> dumper.accept("dataSource.", entry)));
436433

437-
Try.of(() -> config.getConfig(key))
438-
.getOrElse(ConfigFactory.empty())
434+
dbconf.apply(key)
435+
.withoutPath("hikari")
439436
.entrySet().forEach(entry -> dumper.accept("dataSource.", entry));
440437

441438
$hikari.entrySet().forEach(entry -> dumper.accept("", entry));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package jdbc;
2+
3+
import org.jooby.Jooby;
4+
import org.jooby.jdbc.Jdbc;
5+
6+
import com.typesafe.config.ConfigFactory;
7+
import com.typesafe.config.ConfigValueFactory;
8+
9+
public class H2MultiJdbcApp extends Jooby {
10+
11+
{
12+
use(ConfigFactory.empty()
13+
.withValue("db.spock.url", ConfigValueFactory
14+
.fromAnyRef("jdbc:h2:mem:spock;DB_CLOSE_DELAY=-1"))
15+
.withValue("db.spock.user", ConfigValueFactory.fromAnyRef("sa"))
16+
.withValue("db.spock.password", ConfigValueFactory.fromAnyRef(""))
17+
.withValue("db.spock.hikari.maximumPoolSize", ConfigValueFactory.fromAnyRef(15))
18+
.withValue("db.spock.hikari.autoCommit", ConfigValueFactory.fromAnyRef(true))
19+
.withValue("db.vulcan.url", ConfigValueFactory
20+
.fromAnyRef("jdbc:h2:mem:vulcan;DB_CLOSE_DELAY=-1"))
21+
.withValue("db.vulcan.user", ConfigValueFactory.fromAnyRef("sa"))
22+
.withValue("db.vulcan.password", ConfigValueFactory.fromAnyRef(""))
23+
.withValue("db.vulcan.hikari.maximumPoolSize", ConfigValueFactory.fromAnyRef(25))
24+
.withValue("db.vulcan.hikari.autoCommit", ConfigValueFactory.fromAnyRef(true)));
25+
26+
use(new Jdbc("db.spock"));
27+
use(new Jdbc("db.vulcan"));
28+
}
29+
30+
public static void main(final String[] args) {
31+
run(H2MultiJdbcApp::new, args);
32+
}
33+
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package jdbc;
2+
3+
import org.jooby.Jooby;
4+
import org.jooby.jdbc.Jdbc;
5+
6+
import com.typesafe.config.ConfigFactory;
7+
import com.typesafe.config.ConfigValueFactory;
8+
9+
public class MultiJdbcApp extends Jooby {
10+
11+
{
12+
use(ConfigFactory.empty()
13+
.withValue("db.spock.url", ConfigValueFactory
14+
.fromAnyRef("jdbc:h2:mem:spock;DB_CLOSE_DELAY=-1"))
15+
.withValue("db.spock.user", ConfigValueFactory.fromAnyRef("sa"))
16+
.withValue("db.spock.password", ConfigValueFactory.fromAnyRef(""))
17+
.withValue("db.spock.hikari.maximumPoolSize", ConfigValueFactory.fromAnyRef(15))
18+
.withValue("db.spock.hikari.autoCommit", ConfigValueFactory.fromAnyRef(true))
19+
.withValue("db.vulcan.url", ConfigValueFactory
20+
.fromAnyRef("jdbc:h2:mem:vulcan;DB_CLOSE_DELAY=-1"))
21+
.withValue("db.vulcan.user", ConfigValueFactory.fromAnyRef("sa"))
22+
.withValue("db.vulcan.password", ConfigValueFactory.fromAnyRef(""))
23+
.withValue("db.vulcan.hikari.maximumPoolSize", ConfigValueFactory.fromAnyRef(25))
24+
.withValue("db.vulcan.hikari.autoCommit", ConfigValueFactory.fromAnyRef(true)));
25+
26+
use(new Jdbc("db.spock"));
27+
use(new Jdbc("db.vulcan"));
28+
}
29+
30+
public static void main(final String[] args) {
31+
run(MultiJdbcApp::new, args);
32+
}
33+
34+
}

jooby-jdbc/src/test/java/org/jooby/jdbc/JdbcTest.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ public void mysql() throws Exception {
294294
public void dbspecific() throws Exception {
295295
Config config = ConfigFactory.parseResources(getClass(), "jdbc.conf");
296296
Config dbconf = config.withValue("db.url",
297-
ConfigValueFactory.fromAnyRef("jdbc:mysql://localhost/db?useEncoding=true&characterEncoding=UTF-8"))
297+
ConfigValueFactory
298+
.fromAnyRef("jdbc:mysql://localhost/db?useEncoding=true&characterEncoding=UTF-8"))
298299
.withValue("application.charset", fromAnyRef("UTF-8"))
299300
.withValue("application.name", fromAnyRef("jdbctest"))
300301
.withValue("application.tmpdir", fromAnyRef("target"))
@@ -303,7 +304,8 @@ public void dbspecific() throws Exception {
303304
.resolve();
304305

305306
new MockUnit(Env.class, Config.class, Binder.class)
306-
.expect(props("com.mysql.jdbc.jdbc2.optional.MysqlDataSource", "jdbc:mysql://localhost/db?useEncoding=true&characterEncoding=UTF-8",
307+
.expect(props("com.mysql.jdbc.jdbc2.optional.MysqlDataSource",
308+
"jdbc:mysql://localhost/db?useEncoding=true&characterEncoding=UTF-8",
307309
"mysql.db", null, "", false))
308310
.expect(mysql)
309311
.expect(unit -> {
@@ -385,24 +387,28 @@ public void overrideDataSource() throws Exception {
385387
@Test
386388
public void twoDatabases() throws Exception {
387389
Config config = ConfigFactory.parseResources(getClass(), "jdbc.conf");
388-
Config dbconf = config.withValue("db.audit", ConfigValueFactory.fromAnyRef("fs"))
390+
Config dbconf = config.withValue("db.audit.url",
391+
ConfigValueFactory.fromAnyRef("jdbc:h2:mem:audit;DB_CLOSE_DELAY=-1"))
389392
.withValue("application.name", fromAnyRef("jdbctest"))
390393
.withValue("application.tmpdir", fromAnyRef("target"))
391394
.withValue("application.charset", fromAnyRef("UTF-8"))
392-
.withValue("hikari.audit.dataSourceClassName", fromAnyRef("test.MyDataSource"))
395+
.withValue("db.audit.user", fromAnyRef("sa"))
396+
.withValue("db.audit.password", fromAnyRef(""))
397+
.withValue("db.audit.hikari.dataSourceClassName", fromAnyRef("test.MyDataSource"))
393398
.resolve();
394399

395400
new MockUnit(Env.class, Config.class, Binder.class)
396-
.expect(props("org.h2.jdbcx.JdbcDataSource", "jdbc:h2:target/jdbctest", "h2.jdbctest",
397-
"sa", "", true))
401+
.expect(
402+
props("org.h2.jdbcx.JdbcDataSource", "jdbc:h2:mem:audit;DB_CLOSE_DELAY=-1", "h2.audit",
403+
"sa", "", true))
398404
.expect(unit -> {
399405
Properties properties = unit.get(Properties.class);
400406
expect(properties.setProperty("dataSourceClassName", "test.MyDataSource"))
401407
.andReturn(null);
402408
})
403409
.expect(hikariConfig())
404410
.expect(hikariDataSource())
405-
.expect(serviceKey("jdbctest"))
411+
.expect(serviceKey("audit"))
406412
.expect(onStop)
407413
.run(unit -> {
408414
new Jdbc("db.audit").configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration scan="true" scanPeriod="5s" debug="true">
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>[%d{ISO8601}]-[%thread] %-5level %logger - %msg%n</pattern>
6+
</encoder>
7+
</appender>
8+
9+
<root level="DEBUG">
10+
<appender-ref ref="STDOUT" />
11+
</root>
12+
</configuration>

md/doc/jdbc/jdbc.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,16 @@ db.audit.user = ....
166166
db.audit.password = ....
167167
```
168168

169-
Same principle applies if you need to tweak [hikari](https://github.com/brettwooldridge/HikariCP):
169+
Same principle applies if you need to tweak [hikari](https://github.com/brettwooldridge/HikariCP) per database:
170170

171171
```properties
172172
# max pool size for main db
173173

174-
hikari.main.maximumPoolSize = 100
174+
db.main.hikari.maximumPoolSize = 100
175175

176176
# max pool size for audit db
177177

178-
hikari.audit.maximumPoolSize = 20
178+
db.audit.hikari.maximumPoolSize = 20
179179
```
180180

181181
The first registered database is the **default** database. The **second** database is accessible by name and type:

pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,20 @@
963963
<version>${thymeleaf.version}</version>
964964
</dependency>
965965

966+
<!-- Rocker -->
967+
<dependency>
968+
<groupId>com.fizzed</groupId>
969+
<artifactId>rocker-runtime</artifactId>
970+
<version>${rocker.version}</version>
971+
</dependency>
972+
973+
<dependency>
974+
<groupId>com.fizzed</groupId>
975+
<artifactId>rocker-compiler</artifactId>
976+
<version>${rocker.version}</version>
977+
<scope>provided</scope>
978+
</dependency>
979+
966980
<!-- jade4j -->
967981
<dependency>
968982
<groupId>de.neuland-bfi</groupId>
@@ -2749,6 +2763,7 @@ org.eclipse.jdt.apt.processorOptions/defaultOverwrite=true
27492763
<crash.version>1.3.1</crash.version>
27502764
<groovy.version>2.4.7</groovy.version>
27512765
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
2766+
<rocker.version>0.14.0</rocker.version>
27522767

27532768
<!-- config -->
27542769
<ebean.agent.packages>**</ebean.agent.packages>

0 commit comments

Comments
 (0)