Skip to content

Commit 02b16e7

Browse files
authored
Unable to connect to a Unix Domain Socket without native transport on JDK16+ (#1635) (#1636)
See #1634 Replaced native transport enabled check with UDS supported check and updated tests. Fixed documentation and examples. Fix documentation Some portions of this content were created with the assistance of IBM Bob Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
1 parent 662a02b commit 02b16e7

File tree

10 files changed

+62
-115
lines changed

10 files changed

+62
-115
lines changed

vertx-mysql-client/src/main/asciidoc/index.adoc

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The client is reactive and non-blocking, allowing to handle many database connec
2323
* MySQL utilities commands support
2424
* Working with MySQL and MariaDB
2525
* Rich collation and charset support
26-
* Unix domain socket
26+
* Unix Domain Socket
2727
2828
== Usage
2929

@@ -132,42 +132,16 @@ include::pool_sharing.adoc[]
132132

133133
=== Unix Domain Socket
134134

135-
Sometimes for simplicity, security or performance reasons, it is required to connect via a https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_socket[Unix Domain Socket].
135+
Sometimes for simplicity, security, or performance reasons, it is required to connect via a https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_socket[Unix Domain Socket].
136136

137-
Since the JVM does not support domain sockets, first you must add native transport extensions to your project.
138-
139-
* Maven (in your `pom.xml`):
140-
141-
[source,xml]
142-
----
143-
<dependency>
144-
<groupId>io.netty</groupId>
145-
<artifactId>netty-transport-native-epoll</artifactId>
146-
<version>${netty.version}</version>
147-
<classifier>linux-x86_64</classifier>
148-
</dependency>
149-
----
150-
* Gradle (in your `build.gradle` file):
151-
152-
[source,groovy]
153-
----
154-
dependencies {
155-
compile 'io.netty:netty-transport-native-epoll:${netty.version}:linux-x86_64'
156-
}
157-
----
158-
159-
NOTE: The native `epoll` support for ARM64 can also be added with the classifier `linux-aarch64`.
160-
161-
NOTE: If there are Mac users in your team, add `netty-transport-native-kqueue` with the classifier `osx-x86_64`.
162-
163-
Then set the path to the domain socket in {@link io.vertx.mysqlclient.MySQLConnectOptions#setHost MySQLConnectOptions#setHost}:
137+
To do so, set the path to the domain socket in {@link io.vertx.mysqlclient.MySQLConnectOptions#setHost MySQLConnectOptions#setHost}:
164138

165139
[source,$lang]
166140
----
167141
{@link examples.MySQLClientExamples#connectWithUnixDomainSocket}
168142
----
169143

170-
More information about native transports can be found in the [Vert.x documentation](https://vertx.io/docs/vertx-core/java/#_native_transports).
144+
NOTE: Unix Domain Sockets are supported when running on JDK 16+, or using a link:/docs/vertx-core/java/#_native_transports[native transport].
171145

172146
== Configuration
173147

vertx-mysql-client/src/main/java/examples/MySQLClientExamples.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -290,15 +290,6 @@ public void connectWithUnixDomainSocket(Vertx vertx) {
290290
.connectingTo(connectOptions)
291291
.using(vertx)
292292
.build();
293-
294-
// Create the pooled client with a vertx instance
295-
// Make sure the vertx instance has enabled native transports
296-
// vertxOptions.setPreferNativeTransport(true);
297-
Pool client2 = MySQLBuilder.pool()
298-
.with(poolOptions)
299-
.connectingTo(connectOptions)
300-
.using(vertx)
301-
.build();
302293
}
303294

304295
public void reconnectAttempts(MySQLConnectOptions options) {

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/MySQLConnectionImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2023 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -25,11 +25,13 @@
2525
import io.vertx.sqlclient.internal.SqlConnectionBase;
2626
import io.vertx.sqlclient.spi.ConnectionFactory;
2727

28+
import static io.vertx.sqlclient.impl.ConnectionFactoryBase.UDS_NOT_SUPPORTED;
29+
2830
public class MySQLConnectionImpl extends SqlConnectionBase<MySQLConnectionImpl> implements MySQLConnection {
2931

3032
public static Future<MySQLConnection> connect(ContextInternal ctx, MySQLConnectOptions options) {
31-
if (options.isUsingDomainSocket() && !ctx.owner().isNativeTransportEnabled()) {
32-
return ctx.failedFuture("Native transport is not available");
33+
if (options.isUsingDomainSocket() && !ctx.owner().transport().supportsDomainSockets()) {
34+
return ctx.failedFuture(UDS_NOT_SUPPORTED);
3335
}
3436
MySQLConnectionFactory client;
3537
try {

vertx-mysql-client/src/test/java/io/vertx/tests/mysqlclient/MySQLUnixDomainSocketTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2020 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -28,6 +28,7 @@
2828
import java.io.UnsupportedEncodingException;
2929
import java.net.URLEncoder;
3030

31+
import static org.junit.Assert.assertEquals;
3132
import static org.junit.Assert.assertFalse;
3233
import static org.junit.Assume.assumeTrue;
3334

@@ -51,7 +52,10 @@ public void setUp() {
5152
options.setHost(rule.domainSocketPath());
5253
}
5354
assumeTrue(options.isUsingDomainSocket());
54-
vertx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(true));
55+
// Only use native transport for JDK < 16 (Unix domain socket support added in JDK 16)
56+
boolean useNativeTransport = Runtime.version().feature() < 16;
57+
vertx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(useNativeTransport));
58+
assertEquals(useNativeTransport, vertx.isNativeTransportEnabled());
5559
}
5660

5761
@After
@@ -93,7 +97,10 @@ public void simpleConnect(TestContext context) {
9397

9498
@Test
9599
public void connectWithVertxInstance(TestContext context) {
96-
Vertx vertx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(true));
100+
// Only use native transport for JDK < 16 (Unix domain socket support added in JDK 16)
101+
boolean useNativeTransport = Runtime.version().feature() < 16;
102+
Vertx vertx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(useNativeTransport));
103+
assertEquals(useNativeTransport, vertx.isNativeTransportEnabled());
97104
try {
98105
client = MySQLBuilder.pool(builder -> builder.connectingTo(new MySQLConnectOptions(options)).using(vertx));
99106
Async async = context.async();

vertx-pg-client/README.adoc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Documentation:
1919
- Direct memory to object without unnecessary copies
2020
- Java 8 Date and Time
2121
- SSL/TLS
22-
- Unix domain socket
22+
- Unix Domain Socket
2323
- HTTP/1.x CONNECT, SOCKS4a or SOCKS5 proxy
2424
- Request cancellation
2525

@@ -66,11 +66,8 @@ You need to add some properties for testing:
6666
- connection.uri(mandatory): configure the client to connect the specified database
6767
- tls.connection.uri(mandatory): configure the client to run `TLSTest` with the specified Postgres with SSL enabled
6868
- tls.force.connection.uri(mandatory): configure the client to run `TLSTest` with the specified Postgres with SSL forced (only option)
69-
- unix.socket.directory(optional): the single unix socket directory(multiple socket directories are not supported) to test Unix domain socket with a specified database, domain socket tests will be skipped if this property is not specified
70-
(Note: Make sure you can access the unix domain socket with this directory under your host machine)
71-
- unix.socket.port(optional): unix socket file is named `.s.PGSQL.nnnn` and `nnnn` is the server's port number,
72-
this property is mostly used when you test with Docker, when you publish your Postgres container port other than 5432 in your host but Postgres may actually listen on a different port in the container,
73-
you will then need this property to help you connect the Postgres with Unix domain socket
69+
- unix.socket.directory(optional): the single unix socket directory(multiple socket directories are not supported) to test Unix Domain Socket with a specified database, domain socket tests will be skipped if this property is not specified (Note: Make sure you can access the Unix Domain Socket with this directory under your host machine)
70+
- unix.socket.port(optional): unix socket file is named `.s.PGSQL.nnnn` and `nnnn` is the server's port number, this property is mostly used when you test with Docker, when you publish your Postgres container port other than 5432 in your host but Postgres may actually listen on a different port in the container, you will then need this property to help you connect the Postgres with Unix Domain Socket
7471

7572
=== Testing with Docker
7673

vertx-pg-client/src/main/asciidoc/index.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The client is reactive and non-blocking, allowing to handle many database connec
1818
* Direct memory to object without unnecessary copies
1919
* Java 8 Date and Time
2020
* SSL/TLS
21-
* Unix domain socket
21+
* Unix Domain Socket
2222
* HTTP/1.x CONNECT, SOCKS4a or SOCKS5 proxy support
2323
* Proxy (level 4 and 7) support
2424
@@ -123,18 +123,18 @@ The {@link io.vertx.pgclient.PgBuilder} allows you to create a pool or a pooled
123123

124124
include::pool_sharing.adoc[]
125125

126-
== Unix domain sockets
126+
== Unix Domain Socket
127127

128-
Sometimes you want to improve performance via Unix domain socket connection, we achieve this with Vert.x Native transports.
128+
Sometimes for simplicity, security, or performance reasons, it is required to connect via a Unix Domain Socket.
129129

130-
Make sure you have added the required `netty-transport-native` dependency in your classpath and enabled the Unix domain socket option.
130+
To do so, set the path to the domain socket in {@link io.vertx.pgclient.PgConnectOptions#setHost PgConnectOptions#setHost}:
131131

132132
[source,$lang]
133133
----
134134
{@link examples.PgClientExamples#unixDomainSockets}
135135
----
136136

137-
More information can be found in the https://vertx.io/docs/vertx-core/java/#_native_transports[Vert.x documentation].
137+
NOTE: Unix Domain Sockets are supported when running on JDK 16+, or using a link:/docs/vertx-core/java/#_native_transports[native transport].
138138

139139
== Connect retries
140140

vertx-pg-client/src/main/java/examples/PgClientExamples.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
/*
2-
* Copyright (C) 2017 Julien Viet
2+
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
158
*
9+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
1610
*/
1711

1812
package examples;
@@ -334,14 +328,6 @@ public void unixDomainSockets(Vertx vertx) {
334328

335329
// Create the pooled client
336330
Pool client = PgBuilder
337-
.pool()
338-
.connectingTo(connectOptions)
339-
.with(poolOptions)
340-
.build();
341-
342-
// Create the pooled client with a vertx instance
343-
// Make sure the vertx instance has enabled native transports
344-
Pool client2 = PgBuilder
345331
.pool()
346332
.connectingTo(connectOptions)
347333
.with(poolOptions)

vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgConnectionFactory.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
/*
2-
* Copyright (C) 2017 Julien Viet
2+
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
158
*
9+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
1610
*/
1711

1812
package io.vertx.pgclient.impl;
@@ -159,8 +153,8 @@ private Future<Connection> doConnect(ConnectOptions connectOptions, ContextInter
159153
@Override
160154
public Future<SqlConnection> connect(Context context, PgConnectOptions options) {
161155
ContextInternal contextInternal = (ContextInternal) context;
162-
if (options.isUsingDomainSocket() && !vertx.isNativeTransportEnabled()) {
163-
return contextInternal.failedFuture(new IllegalArgumentException(NATIVE_TRANSPORT_REQUIRED));
156+
if (options.isUsingDomainSocket() && !vertx.transport().supportsDomainSockets()) {
157+
return contextInternal.failedFuture(UDS_NOT_SUPPORTED);
164158
}
165159
PromiseInternal<SqlConnection> promise = contextInternal.promise();
166160
connect(asEventLoopContext(contextInternal), options)

vertx-pg-client/src/test/java/io/vertx/tests/pgclient/UnixDomainSocketTest.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
/*
2-
* Copyright (C) 2018 Julien Viet
2+
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
158
*
9+
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
1610
*/
1711
package io.vertx.tests.pgclient;
1812

19-
import io.vertx.pgclient.PgBuilder;
20-
import io.vertx.pgclient.PgConnectOptions;
21-
import io.vertx.pgclient.SslMode;
2213
import io.vertx.core.Vertx;
2314
import io.vertx.core.VertxOptions;
2415
import io.vertx.ext.unit.TestContext;
2516
import io.vertx.ext.unit.junit.VertxUnitRunner;
17+
import io.vertx.pgclient.PgBuilder;
18+
import io.vertx.pgclient.PgConnectOptions;
19+
import io.vertx.pgclient.SslMode;
2620
import io.vertx.sqlclient.Pool;
2721
import io.vertx.sqlclient.SqlClient;
2822
import io.vertx.sqlclient.SqlConnectOptions;
@@ -31,8 +25,6 @@
3125
import org.junit.*;
3226
import org.junit.runner.RunWith;
3327

34-
import java.io.File;
35-
3628
import static org.junit.Assert.assertEquals;
3729
import static org.junit.Assert.assertFalse;
3830
import static org.junit.Assume.assumeTrue;
@@ -49,7 +41,10 @@ public class UnixDomainSocketTest {
4941
@Before
5042
public void before() {
5143
Assume.assumeNotNull(rule.domainSocketPath());
52-
vertx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(true));
44+
// Only use native transport for JDK < 16 (Unix domain socket support added in JDK 16)
45+
boolean useNativeTransport = Runtime.version().feature() < 16;
46+
vertx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(useNativeTransport));
47+
assertEquals(useNativeTransport, vertx.isNativeTransportEnabled());
5348
options = rule.options().setHost(rule.domainSocketPath()).setPort(5432);
5449
}
5550

@@ -104,9 +99,10 @@ public void testIgnoreSslMode(TestContext context) {
10499

105100
@Test
106101
public void testNativeTransportMustBeEnabled(TestContext ctx) {
102+
assumeTrue("Unix Domain Sockets are supported on Java 16+", Runtime.version().feature() < 16);
107103
Pool pool = PgBuilder.pool().connectingTo(SqlConnectOptions.fromUri("postgresql:///dbname?host=/var/lib/postgresql")).build();
108104
pool.getConnection().onComplete(ctx.asyncAssertFailure(err -> {
109-
assertEquals(ConnectionFactoryBase.NATIVE_TRANSPORT_REQUIRED, err.getMessage());
105+
assertEquals(ConnectionFactoryBase.UDS_NOT_SUPPORTED, err);
110106
}));
111107
}
112108
}

vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/ConnectionFactoryBase.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2020 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -12,13 +12,13 @@
1212

1313
import io.vertx.core.Completable;
1414
import io.vertx.core.Future;
15-
import io.vertx.core.Promise;
16-
import io.vertx.core.net.NetClient;
17-
import io.vertx.core.net.NetClientOptions;
15+
import io.vertx.core.VertxException;
1816
import io.vertx.core.internal.CloseSequence;
1917
import io.vertx.core.internal.ContextInternal;
2018
import io.vertx.core.internal.PromiseInternal;
2119
import io.vertx.core.internal.VertxInternal;
20+
import io.vertx.core.net.NetClient;
21+
import io.vertx.core.net.NetClientOptions;
2222
import io.vertx.sqlclient.SqlConnectOptions;
2323
import io.vertx.sqlclient.internal.Connection;
2424
import io.vertx.sqlclient.spi.ConnectionFactory;
@@ -28,7 +28,7 @@
2828
*/
2929
public abstract class ConnectionFactoryBase<C extends SqlConnectOptions> implements ConnectionFactory<C> {
3030

31-
public static final String NATIVE_TRANSPORT_REQUIRED = "The Vertx instance must use a native transport in order to connect to connect through domain sockets";
31+
public static final VertxException UDS_NOT_SUPPORTED = new VertxException("Unix Domain Sockets are not supported, use Java 16+ or enable a native transport", true);
3232

3333
protected final VertxInternal vertx;
3434
protected final NetClient client;

0 commit comments

Comments
 (0)