From 72700141768fbb66130b59ba1aa9a54283e89606 Mon Sep 17 00:00:00 2001 From: zyyang Date: Fri, 29 Jan 2021 14:02:01 +0800 Subject: [PATCH] change --- .../com/taosdata/jdbc/TSDBJNIConnector.java | 20 +++++---- .../com/taosdata/jdbc/utils/TaosInfo.java | 44 +++++++++++++++++-- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java index 178bbc483f..9b387b1959 100755 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java @@ -16,20 +16,22 @@ package com.taosdata.jdbc; import com.taosdata.jdbc.utils.TaosInfo; +import javax.management.MBeanServer; +import javax.management.ObjectName; +import java.lang.management.ManagementFactory; import java.sql.SQLException; import java.sql.SQLWarning; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; public class TSDBJNIConnector { private static volatile Boolean isInitialized = false; - private static AtomicInteger open_count = new AtomicInteger(); - private static AtomicInteger close_count = new AtomicInteger(); + private TaosInfo taosInfo = TaosInfo.getInstance(); static { System.loadLibrary("taos"); System.out.println("java.library.path:" + System.getProperty("java.library.path")); + } /** @@ -107,9 +109,9 @@ public class TSDBJNIConnector { throw new SQLException(TSDBConstants.WrapErrMsg(this.getErrMsg(0L)), "", this.getErrCode(0l)); } // invoke connectImp only here - int open = open_count.incrementAndGet(); - int close = close_count.get(); - System.out.println("open_count: " + open + ", close_count: " + close + ", connection_count: " + (open - close)); + int open = taosInfo.getOpen_count().incrementAndGet(); + int close = taosInfo.getClose_count().get(); + System.out.println("open_count: " + open + ", close_count: " + close + ", connection_count: " + (taosInfo.getConnectionCount())); return true; } @@ -274,9 +276,9 @@ public class TSDBJNIConnector { throw new SQLException("Undefined error code returned by TDengine when closing a connection"); } // invoke closeConnectionImpl only here - int open = open_count.get(); - int close = close_count.incrementAndGet(); - System.out.println("open_count: " + open + ", close_count: " + close + ", connection_count: " + (open - close)); + int open = taosInfo.getOpen_count().get(); + int close = taosInfo.getClose_count().incrementAndGet(); + System.out.println("open_count: " + open + ", close_count: " + close + ", connection_count: " + (taosInfo.getConnectionCount())); } private native int closeConnectionImp(long connection); diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/TaosInfo.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/TaosInfo.java index 7d36568465..83ac7ed809 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/TaosInfo.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/TaosInfo.java @@ -1,19 +1,55 @@ package com.taosdata.jdbc.utils; +import javax.management.*; +import java.lang.management.ManagementFactory; import java.util.concurrent.atomic.AtomicInteger; public class TaosInfo implements TaosInfoMBean { - public AtomicInteger conn = new AtomicInteger(); - public AtomicInteger stmt = new AtomicInteger(); + private static volatile TaosInfo instance; + private AtomicInteger open_count = new AtomicInteger(); + private AtomicInteger close_count = new AtomicInteger(); + + static { + try { + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); + ObjectName name = new ObjectName("TaosInfoMBean:name=TaosInfo"); + server.registerMBean(TaosInfo.getInstance(), name); + } catch (MalformedObjectNameException | InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) { + e.printStackTrace(); + } + } @Override public int getConnectionCount() { - return conn.get(); + return open_count.get() - close_count.get(); } @Override public int getStatementCount() { - return stmt.get(); + return 0; + } + + public AtomicInteger getOpen_count() { + return open_count; + } + + public AtomicInteger getClose_count() { + return close_count; } + + private TaosInfo() { + } + + public static TaosInfo getInstance() { + if (instance == null) { + synchronized (TaosInfo.class) { + if (instance == null) { + instance = new TaosInfo(); + } + } + } + return instance; + } + } -- GitLab