Skip to content

Commit 457cf56

Browse files
committed
correctly default the port number if missing from JDBC URL
1 parent b5f76a5 commit 457cf56

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

example/src/main/resources/META-INF/persistence.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313
<!-- PostgreSQL -->
1414
<property name="javax.persistence.jdbc.url"
15-
value="jdbc:postgresql://localhost:5432/hreact"/>
15+
value="jdbc:postgresql://localhost/hreact"/>
1616

1717
<!-- MySQL -->
1818
<!--property name="javax.persistence.jdbc.url"
19-
value="jdbc:mysql://localhost:3306/hreact"/-->
19+
value="jdbc:mysql://localhost/hreact"/-->
2020

2121
<!-- Credentials -->
2222
<property name="javax.persistence.jdbc.user"

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/SqlClientPool.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ private PoolOptions poolOptions(Map configurationValues) {
110110

111111
private SqlConnectOptions sqlConnectOptions(URI uri) {
112112

113+
String scheme = uri.getScheme();
114+
113115
String database = uri.getPath().substring( 1 );
114-
if (uri.getScheme().equals("db2") && database.indexOf( ':' ) > 0) {
116+
if ( scheme.equals("db2") && database.indexOf( ':' ) > 0 ) {
115117
database = database.substring( 0, database.indexOf( ':' ) );
116118
}
117119

@@ -121,7 +123,7 @@ private SqlConnectOptions sqlConnectOptions(URI uri) {
121123
if (username==null || password==null) {
122124
String[] params = {};
123125
// DB2 URLs are a bit odd and have the format: jdbc:db2://<HOST>:<PORT>/<DB>:key1=value1;key2=value2;
124-
if (uri.getScheme().equals("db2")) {
126+
if ( scheme.equals("db2") ) {
125127
int queryIndex = uri.getPath().indexOf(':') + 1;
126128
if (queryIndex > 0) {
127129
params = uri.getPath().substring(queryIndex).split(";");
@@ -142,9 +144,18 @@ private SqlConnectOptions sqlConnectOptions(URI uri) {
142144
}
143145
}
144146

147+
int port = uri.getPort();
148+
if (port==-1) {
149+
switch (scheme) {
150+
case "postgresql": port = 5432; break;
151+
case "mysql": port = 3306; break;
152+
case "db2": port = 50000; break;
153+
}
154+
}
155+
145156
SqlConnectOptions connectOptions = new SqlConnectOptions()
146157
.setHost( uri.getHost() )
147-
.setPort( uri.getPort() )
158+
.setPort( port )
148159
.setDatabase( database )
149160
.setUser( username );
150161
if (password != null) {

0 commit comments

Comments
 (0)