Skip to content

Commit 6fb431e

Browse files
authored
Merge pull request #707 from jooby-project/705
Postgresql database reload with 1.1.0 fix #705
2 parents d0dce25 + f7212d3 commit 6fb431e

8 files changed

Lines changed: 109 additions & 21 deletions

File tree

coverage-report/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,25 @@
805805
<scope>test</scope>
806806
</dependency>
807807

808+
<dependency>
809+
<groupId>io.netty</groupId>
810+
<artifactId>netty-common</artifactId>
811+
<version>${netty.version}</version>
812+
</dependency>
813+
814+
<dependency>
815+
<groupId>com.impossibl.pgjdbc-ng</groupId>
816+
<artifactId>pgjdbc-ng</artifactId>
817+
<version>0.7.1</version>
818+
<optional>true</optional>
819+
<exclusions>
820+
<exclusion>
821+
<groupId>io.netty</groupId>
822+
<artifactId>netty-common</artifactId>
823+
</exclusion>
824+
</exclusions>
825+
</dependency>
826+
808827
</dependencies>
809828

810829
</project>

jooby-jdbc/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
<artifactId>HikariCP</artifactId>
2828
</dependency>
2929

30+
<dependency>
31+
<groupId>com.impossibl.pgjdbc-ng</groupId>
32+
<artifactId>pgjdbc-ng</artifactId>
33+
<version>0.7.1</version>
34+
<optional>true</optional>
35+
</dependency>
36+
3037
<!-- Test dependencies -->
3138
<dependency>
3239
<groupId>ch.qos.logback</groupId>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package com.impossibl.postgres.jdbc;
20+
21+
import java.net.InetSocketAddress;
22+
23+
import com.impossibl.postgres.jdbc.ConnectionUtil.ConnectionSpecifier;
24+
25+
// Hack documented here: https://github.com/impossibl/pgjdbc-ng/issues/323
26+
public class PGDataSourceWithUrl extends PGDataSource {
27+
28+
private String url;
29+
30+
public String getUrl() {
31+
return url;
32+
}
33+
34+
public void setUrl(final String url) {
35+
this.url = url;
36+
ConnectionSpecifier specifier = ConnectionUtil.parseURL(url);
37+
setDatabase(specifier.getDatabase());
38+
InetSocketAddress address = specifier.getAddresses().get(0);
39+
setHost(address.getHostString());
40+
setPort(address.getPort());
41+
}
42+
43+
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,7 @@ private HikariConfig hikariConfig(final String url, final String key, final Stri
444444
dataSourceClassName = props.getProperty("dataSource.dataSourceClassName");
445445
props.setProperty("dataSourceClassName", dataSourceClassName);
446446
}
447-
if (Strings.isNullOrEmpty(dataSourceClassName)) {
448-
// Hack old drivers without a setUrl method (pgsql)
449-
props.put("jdbcUrl", url);
450-
}
447+
451448
// remove dataSourceClassName under dataSource
452449
props.remove("dataSource.dataSourceClassName");
453450
// set pool name

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,15 @@ databases {
9090
# url => jdbc:pgsql://<server>[:<port>]/<database>
9191
###############################################################################################
9292
pgsql {
93-
# empty bc dataSource.setUrl missing
94-
# dataSourceClassName = com.impossibl.postgres.jdbc.PGDataSource
95-
dataSourceClassName = ""
93+
dataSourceClassName = com.impossibl.postgres.jdbc.PGDataSourceWithUrl
9694
}
9795

9896
###############################################################################################
9997
# postgresql
10098
# url => jdbc:postgresql://host:port/database
10199
###############################################################################################
102100
postgresql {
103-
# empty bc dataSource.setUrl missing
104-
# dataSourceClassName = org.postgresql.ds.PGSimpleDataSource
105-
dataSourceClassName = ""
101+
dataSourceClassName = org.postgresql.ds.PGSimpleDataSource
106102
}
107103

108104
###############################################################################################
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.impossibl.postgres.jdbc;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class PGDataSourceWithUrlTest {
8+
9+
@Test
10+
public void setUrl() {
11+
PGDataSourceWithUrl ds = new PGDataSourceWithUrl();
12+
ds.setUrl("jdbc:pgsql://server/database");
13+
assertEquals("server", ds.getHost());
14+
assertEquals(5432, ds.getPort());
15+
assertEquals("database", ds.getDatabase());
16+
assertEquals("jdbc:pgsql://server/database", ds.getUrl());
17+
}
18+
19+
@Test
20+
public void setUrlPort() {
21+
PGDataSourceWithUrl ds = new PGDataSourceWithUrl();
22+
ds.setUrl("jdbc:pgsql://server:1234/database");
23+
assertEquals("server", ds.getHost());
24+
assertEquals(1234, ds.getPort());
25+
assertEquals("database", ds.getDatabase());
26+
}
27+
}

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -517,15 +517,12 @@ public void pgsql() throws Exception {
517517
.resolve();
518518

519519
new MockUnit(Env.class, Config.class, Binder.class)
520-
.expect(props("", "jdbc:pgsql://server/database",
521-
"pgsql.database", null, "", false))
520+
.expect(
521+
props("com.impossibl.postgres.jdbc.PGDataSourceWithUrl", "jdbc:pgsql://server/database",
522+
"pgsql.database", null, "", false))
522523
.expect(hikariConfig())
523524
.expect(hikariDataSource())
524525
.expect(serviceKey("database"))
525-
.expect(unit -> {
526-
Properties props = unit.get(Properties.class);
527-
expect(props.put("jdbcUrl", url)).andReturn(null);
528-
})
529526
.expect(onStop)
530527
.run(unit -> {
531528
new Jdbc().configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
@@ -544,15 +541,11 @@ public void postgresql() throws Exception {
544541
.resolve();
545542

546543
new MockUnit(Env.class, Config.class, Binder.class)
547-
.expect(props("", "jdbc:postgresql://server/database",
544+
.expect(props("org.postgresql.ds.PGSimpleDataSource", "jdbc:postgresql://server/database",
548545
"postgresql.database", null, "", false))
549546
.expect(hikariConfig())
550547
.expect(hikariDataSource())
551548
.expect(serviceKey("database"))
552-
.expect(unit -> {
553-
Properties props = unit.get(Properties.class);
554-
expect(props.put("jdbcUrl", url)).andReturn(null);
555-
})
556549
.expect(onStop)
557550
.run(unit -> {
558551
new Jdbc().configure(unit.get(Env.class), dbconf, unit.get(Binder.class));

jooby-netty/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
<version>${netty.version}</version>
5555
</dependency>
5656

57+
<dependency>
58+
<groupId>io.netty</groupId>
59+
<artifactId>netty-common</artifactId>
60+
<version>${netty.version}</version>
61+
</dependency>
62+
5763
<dependency>
5864
<groupId>io.netty</groupId>
5965
<artifactId>netty-handler</artifactId>

0 commit comments

Comments
 (0)