diff --git a/src/main/java/com/aerospike/jdbc/AerospikeDriver.java b/src/main/java/com/aerospike/jdbc/AerospikeDriver.java index b677332..6bb5849 100644 --- a/src/main/java/com/aerospike/jdbc/AerospikeDriver.java +++ b/src/main/java/com/aerospike/jdbc/AerospikeDriver.java @@ -22,7 +22,7 @@ public class AerospikeDriver implements Driver { static { try { - java.sql.DriverManager.registerDriver(new AerospikeDriver()); + java.sql.DriverManager.registerDriver(new AerospikeDriver(), new AerospikeDriverAction()); } catch (SQLException e) { throw new ExceptionInInitializerError("Can not register AerospikeDriver"); } @@ -48,8 +48,9 @@ public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) { allInfo.add(new DriverPropertyInfo(kv[0], kv.length > 1 ? kv[1] : null)); }); } - allInfo.addAll(info.entrySet().stream().map(e -> new DriverPropertyInfo((String) e.getKey(), - (String) e.getValue())).collect(toList())); + allInfo.addAll(info.entrySet().stream() + .map(e -> new DriverPropertyInfo((String) e.getKey(), (String) e.getValue())) + .collect(toList())); return allInfo.toArray(new DriverPropertyInfo[0]); } diff --git a/src/main/java/com/aerospike/jdbc/AerospikeDriverAction.java b/src/main/java/com/aerospike/jdbc/AerospikeDriverAction.java new file mode 100644 index 0000000..2a9e06a --- /dev/null +++ b/src/main/java/com/aerospike/jdbc/AerospikeDriverAction.java @@ -0,0 +1,17 @@ +package com.aerospike.jdbc; + +import com.aerospike.jdbc.async.EventLoopProvider; + +import java.sql.DriverAction; +import java.util.logging.Logger; + +public class AerospikeDriverAction implements DriverAction { + + private static final Logger logger = Logger.getLogger(AerospikeDriverAction.class.getName()); + + @Override + public void deregister() { + logger.info("Deregister AerospikeDriver"); + EventLoopProvider.close(); + } +} diff --git a/src/main/java/com/aerospike/jdbc/async/EventLoopProvider.java b/src/main/java/com/aerospike/jdbc/async/EventLoopProvider.java index 8b29fff..8994aff 100644 --- a/src/main/java/com/aerospike/jdbc/async/EventLoopProvider.java +++ b/src/main/java/com/aerospike/jdbc/async/EventLoopProvider.java @@ -6,11 +6,14 @@ import com.aerospike.client.async.NettyEventLoops; import io.netty.channel.nio.NioEventLoopGroup; +import java.util.logging.Logger; + import static java.util.Objects.requireNonNull; public final class EventLoopProvider { - private static EventLoops eventLoops; + private static final Logger logger = Logger.getLogger(EventLoopProvider.class.getName()); + private static volatile EventLoops eventLoops; private EventLoopProvider() { } @@ -25,11 +28,28 @@ public static EventLoops getEventLoops() { return eventLoops; } + public static synchronized void close() { + if (null != eventLoops) { + logger.info(() -> "Close eventLoops"); + eventLoops.close(); + eventLoops = null; + } + } + private static void initEventLoops() { if (null == eventLoops) { - int nThreads = Math.max(2, Runtime.getRuntime().availableProcessors()); - eventLoops = new NettyEventLoops(new EventPolicy(), new NioEventLoopGroup(nThreads)); - requireNonNull(eventLoops.get(0)); + synchronized (EventLoopProvider.class) { + if (null == eventLoops) { + logger.info(() -> "Init eventLoops"); + int nThreads = Math.max(2, Runtime.getRuntime().availableProcessors()); + EventLoops nettyEventLoops = new NettyEventLoops( + new EventPolicy(), + new NioEventLoopGroup(nThreads) + ); + requireNonNull(nettyEventLoops.get(0)); + eventLoops = nettyEventLoops; + } + } } } }