diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java index 33d2560bcca28116db49a283f02d566bda5e3852..307451e014c59c1c3419f1a9daff4f89e8b90d46 100755 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBDriver.java @@ -132,8 +132,7 @@ public class TSDBDriver extends AbstractDriver { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PASSWORD_IS_REQUIRED); try { - TSDBJNIConnector.init((String) props.get(PROPERTY_KEY_CONFIG_DIR), (String) props.get(PROPERTY_KEY_LOCALE), - (String) props.get(PROPERTY_KEY_CHARSET), (String) props.get(PROPERTY_KEY_TIME_ZONE)); + TSDBJNIConnector.init(props); return new TSDBConnection(props, this.dbMetaData); } catch (SQLWarning sqlWarning) { sqlWarning.printStackTrace(); @@ -202,6 +201,7 @@ public class TSDBDriver extends AbstractDriver { String dbProductName = url.substring(0, beginningOfSlashes); dbProductName = dbProductName.substring(dbProductName.indexOf(":") + 1); dbProductName = dbProductName.substring(0, dbProductName.indexOf(":")); + urlProps.setProperty(TSDBDriver.PROPERTY_KEY_PRODUCT_NAME, dbProductName); // parse database name url = url.substring(beginningOfSlashes + 2); 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 ec983f84cfdadf4589c3eac5baf8a57cfdfac679..b400aeb95e263664fcce4791b8a661523b086c86 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,18 +16,21 @@ */ package com.taosdata.jdbc; +import com.alibaba.fastjson.JSONObject; import com.taosdata.jdbc.utils.TaosInfo; import java.nio.ByteBuffer; import java.sql.SQLException; import java.sql.SQLWarning; import java.util.List; +import java.util.Properties; /** * JNI connector */ public class TSDBJNIConnector { - private static volatile Boolean isInitialized = false; + private static final Object LOCK = new Object(); + private static volatile boolean isInitialized; private final TaosInfo taosInfo = TaosInfo.getInstance(); private long taos = TSDBConstants.JNI_NULL_POINTER; // Connection pointer used in C @@ -38,10 +41,23 @@ public class TSDBJNIConnector { System.loadLibrary("taos"); } - public static void init(String configDir, String locale, String charset, String timezone) throws SQLWarning { - synchronized (isInitialized) { + public static void init(Properties props) throws SQLWarning { + synchronized (LOCK) { if (!isInitialized) { + + JSONObject configJSON = new JSONObject(); + for (String key : props.stringPropertyNames()) { + configJSON.put(key, props.getProperty(key)); + } + setConfig(configJSON.toJSONString()); + + String configDir = props.getProperty(TSDBDriver.PROPERTY_KEY_CONFIG_DIR); initImp(configDir); + + String locale = props.getProperty(TSDBDriver.PROPERTY_KEY_LOCALE); + String charset = props.getProperty(TSDBDriver.PROPERTY_KEY_CHARSET); + String timezone = props.getProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE); + if (setOptions(0, locale) < 0) { throw TSDBError.createSQLWarning("Failed to set locale: " + locale + ". System default will be used."); } diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/SetConfigurationInJNITest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/SetConfigurationInJNITest.java index 986325bdfacf991b87435482407f39921763b4d5..a609b21a2c36e8e0e0238bce36a4ad4f8512fe9e 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/SetConfigurationInJNITest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/SetConfigurationInJNITest.java @@ -1,29 +1,23 @@ package com.taosdata.jdbc; -import org.junit.After; import org.junit.Test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; +import java.util.Properties; public class SetConfigurationInJNITest { private String host = "127.0.0.1"; - private String dbname = "test_jni"; - private String debugFlagJSON = "{ \"debugFlag\": \"135\"}"; - - private long maxSQLLength = 1024000; - private String maxSqlLengthJSON = "{ \"maxSQLLength\": " + maxSQLLength + "}"; + private String dbname = "test_set_config"; @Test - public void setConfigBeforeConnectIsValid() { + public void setConfigInUrl() { try { - TSDBJNIConnector.setConfig(debugFlagJSON); - - Connection conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/?user=root&password=taosdata"); + Connection conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/?user=root&password=taosdata&debugFlag=135"); Statement stmt = conn.createStatement(); stmt.execute("drop database if exists " + dbname); @@ -40,13 +34,13 @@ public class SetConfigurationInJNITest { } @Test - public void setConfigAfterConnectIsInvalid() { + public void setConfigInProperties() { try { - Connection conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/?user=root&password=taosdata"); + Properties props = new Properties(); + props.setProperty("debugFlag", "135"); + Connection conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/?user=root&password=taosdata", props); - TSDBJNIConnector.setConfig(debugFlagJSON); Statement stmt = conn.createStatement(); - stmt.execute("drop database if exists " + dbname); stmt.execute("create database if not exists " + dbname); stmt.execute("use " + dbname); diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java index b63c88a875142c29722209382c6139d890f1176c..8be6ae6b1c566abcd7ec398e7df3f5308e29e1b1 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java @@ -7,6 +7,7 @@ import java.lang.management.RuntimeMXBean; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.Properties; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -21,21 +22,22 @@ public class TSDBJNIConnectorTest { try { //change sleepSeconds when debugging with attach to process to find PID int sleepSeconds = -1; - if (sleepSeconds>0) { + if (sleepSeconds > 0) { RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean(); String jvmName = runtimeBean.getName(); long pid = Long.valueOf(jvmName.split("@")[0]); System.out.println("JVM PID = " + pid); - Thread.sleep(sleepSeconds*1000); + Thread.sleep(sleepSeconds * 1000); } - } - catch (Exception e) { + } catch (Exception e) { e.printStackTrace(); } // init - TSDBJNIConnector.init("/etc/taos", null, null, null); + Properties properties = new Properties(); + properties.setProperty(TSDBDriver.PROPERTY_KEY_CONFIG_DIR, "/etc/taos"); + TSDBJNIConnector.init(properties); // connect TSDBJNIConnector connector = new TSDBJNIConnector(); @@ -43,12 +45,12 @@ public class TSDBJNIConnectorTest { // setup String setupSqlStrs[] = {"create database if not exists d precision \"us\"", - "create table if not exists d.t(ts timestamp, f int)", - "create database if not exists d2", - "create table if not exists d2.t2(ts timestamp, f int)", - "insert into d.t values(now+100s, 100)", - "insert into d2.t2 values(now+200s, 200)" - }; + "create table if not exists d.t(ts timestamp, f int)", + "create database if not exists d2", + "create table if not exists d2.t2(ts timestamp, f int)", + "insert into d.t values(now+100s, 100)", + "insert into d2.t2 values(now+200s, 200)" + }; for (String setupSqlStr : setupSqlStrs) { long setupSql = connector.executeQuery(setupSqlStr); @@ -113,8 +115,8 @@ public class TSDBJNIConnectorTest { } // close statement connector.executeQuery("use d"); - String[] lines = new String[] {"st,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns", - "st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000ns"}; + String[] lines = new String[]{"st,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns", + "st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000ns"}; connector.insertLines(lines); // close connection