Skip to content

Commit fc2d36b

Browse files
authored
Merge pull request #780 from jooby-project/763
Support mySQL driver 6.x and upgrade to latest 5.x fix #763 fix #779
2 parents 2736c94 + 69cc167 commit fc2d36b

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

jooby-jdbc/pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
<dependency>
3131
<groupId>com.impossibl.pgjdbc-ng</groupId>
3232
<artifactId>pgjdbc-ng</artifactId>
33-
<version>0.7.1</version>
3433
<optional>true</optional>
3534
</dependency>
3635

@@ -87,13 +86,20 @@
8786
<scope>test</scope>
8887
</dependency>
8988

90-
<!-- mySQL -->
89+
<!-- h2 -->
9190
<dependency>
9291
<groupId>com.h2database</groupId>
9392
<artifactId>h2</artifactId>
9493
<scope>test</scope>
9594
</dependency>
9695

96+
<!-- mySQL -->
97+
<dependency>
98+
<groupId>mysql</groupId>
99+
<artifactId>mysql-connector-java</artifactId>
100+
<scope>test</scope>
101+
</dependency>
102+
97103
</dependencies>
98104

99105
</project>

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@
4545
import com.google.inject.Binder;
4646
import com.typesafe.config.Config;
4747
import com.typesafe.config.ConfigFactory;
48+
import com.typesafe.config.ConfigObject;
4849
import com.typesafe.config.ConfigValue;
4950
import com.typesafe.config.ConfigValueFactory;
51+
import com.typesafe.config.ConfigValueType;
5052
import com.zaxxer.hikari.HikariConfig;
5153
import com.zaxxer.hikari.HikariDataSource;
5254

@@ -247,7 +249,7 @@ public class Jdbc implements Jooby.Module {
247249
Map<String, String> params = new HashMap<>(result._2);
248250
result = indexOf.apply(result._1, ";", ";");
249251
params.putAll(result._2);
250-
List<String> parts = Splitter.on(CharMatcher.JAVA_LETTER_OR_DIGIT.negate())
252+
List<String> parts = Splitter.on(CharMatcher.javaLetterOrDigit().negate())
251253
.trimResults()
252254
.omitEmptyStrings()
253255
.splitToList(result._1);
@@ -429,7 +431,7 @@ private HikariConfig hikariConfig(final String url, final String key, final Stri
429431
* # db.* -> dataSource.*
430432
* # hikari.* -> * (no prefix)
431433
*/
432-
dbtype.ifPresent(type -> config.getConfig("databases." + type)
434+
dbtype.ifPresent(type -> dbconf(config, type)
433435
.entrySet().forEach(entry -> dumper.accept("dataSource.", entry)));
434436

435437
dbconf.apply(key)
@@ -453,6 +455,30 @@ private HikariConfig hikariConfig(final String url, final String key, final Stri
453455
return new HikariConfig(props);
454456
}
455457

458+
@SuppressWarnings("unchecked")
459+
private Config dbconf(final Config conf, final String type) {
460+
String dbtype = "databases." + type;
461+
ConfigValue value = conf.getValue(dbtype);
462+
if (value.valueType() == ConfigValueType.OBJECT) {
463+
return ((ConfigObject) value).toConfig();
464+
}
465+
List<Config> list = (List<Config>) conf.getConfigList(dbtype);
466+
ClassLoader loader = getClass().getClassLoader();
467+
return list.stream()
468+
.filter(it -> dataSourcePresent(loader, it.getString("dataSourceClassName")))
469+
.findFirst()
470+
.orElse(list.get(0));
471+
}
472+
473+
private boolean dataSourcePresent(final ClassLoader loader, final String className) {
474+
try {
475+
loader.loadClass(className.trim());
476+
return true;
477+
} catch (ClassNotFoundException e) {
478+
return false;
479+
}
480+
}
481+
456482
@SuppressWarnings("unchecked")
457483
protected void callback(final Object value, final Config conf) {
458484
this.callback.forEach(it -> Try.run(() -> it.accept(value, conf))

jooby-jdbc/src/main/resources/org/jooby/jdbc/jdbc.conf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@ databases {
6060
# mysql
6161
# url jdbc:mysql://<host>:<port>/<database>?<key1>=<value1>&<key2>=<value2>...
6262
###############################################################################################
63-
mysql {
63+
mysql: [{
64+
# v6.x
65+
dataSourceClassName = com.mysql.cj.jdbc.MysqlDataSource
66+
}, {
67+
# v5.x
6468
dataSourceClassName = com.mysql.jdbc.jdbc2.optional.MysqlDataSource
6569
encoding = ${application.charset}
6670
cachePrepStmts = true
6771
prepStmtCacheSize = 250
6872
prepStmtCacheSqlLimit = 2048
6973
useServerPrepStmts = true
70-
}
74+
}]
7175

7276
###############################################################################################
7377
# sqlserver

pom.xml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
34

45
<modelVersion>4.0.0</modelVersion>
56
<groupId>org.jooby</groupId>
@@ -1116,6 +1117,13 @@
11161117
<version>${mysql-connector-java.version}</version>
11171118
</dependency>
11181119

1120+
<!-- pgjdbc-ng -->
1121+
<dependency>
1122+
<groupId>com.impossibl.pgjdbc-ng</groupId>
1123+
<artifactId>pgjdbc-ng</artifactId>
1124+
<version>${pgjdbc-ng.version}</version>
1125+
</dependency>
1126+
11191127
<!-- Elasticsearch -->
11201128
<dependency>
11211129
<groupId>org.elasticsearch.client</groupId>
@@ -1871,7 +1879,7 @@
18711879
</goals>
18721880
<configuration>
18731881
<transformers>
1874-
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
1882+
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
18751883
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
18761884
<mainClass>${application.class}</mainClass>
18771885
</transformer>
@@ -2964,7 +2972,8 @@ org.eclipse.jdt.apt.processorOptions/defaultOverwrite=true
29642972
<config.version>1.3.1</config.version>
29652973
<handlebars.version>4.0.5</handlebars.version>
29662974
<h2.version>1.4.190</h2.version>
2967-
<mysql-connector-java.version>5.1.38</mysql-connector-java.version>
2975+
<mysql-connector-java.version>5.1.42</mysql-connector-java.version>
2976+
<pgjdbc-ng.version>0.7.1</pgjdbc-ng.version>
29682977
<hikariCP.version>2.6.1</hikariCP.version>
29692978
<hibernate.version>5.2.1.Final</hibernate.version>
29702979
<hibernate4.version>4.3.9.Final</hibernate4.version>

0 commit comments

Comments
 (0)