提交 e3b2d4e9 编写于 作者: Z zyyang

change

上级 cd2fc0f4
...@@ -132,8 +132,7 @@ public class TSDBDriver extends AbstractDriver { ...@@ -132,8 +132,7 @@ public class TSDBDriver extends AbstractDriver {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PASSWORD_IS_REQUIRED); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PASSWORD_IS_REQUIRED);
try { try {
TSDBJNIConnector.init((String) props.get(PROPERTY_KEY_CONFIG_DIR), (String) props.get(PROPERTY_KEY_LOCALE), TSDBJNIConnector.init(props);
(String) props.get(PROPERTY_KEY_CHARSET), (String) props.get(PROPERTY_KEY_TIME_ZONE));
return new TSDBConnection(props, this.dbMetaData); return new TSDBConnection(props, this.dbMetaData);
} catch (SQLWarning sqlWarning) { } catch (SQLWarning sqlWarning) {
sqlWarning.printStackTrace(); sqlWarning.printStackTrace();
...@@ -202,6 +201,7 @@ public class TSDBDriver extends AbstractDriver { ...@@ -202,6 +201,7 @@ public class TSDBDriver extends AbstractDriver {
String dbProductName = url.substring(0, beginningOfSlashes); String dbProductName = url.substring(0, beginningOfSlashes);
dbProductName = dbProductName.substring(dbProductName.indexOf(":") + 1); dbProductName = dbProductName.substring(dbProductName.indexOf(":") + 1);
dbProductName = dbProductName.substring(0, dbProductName.indexOf(":")); dbProductName = dbProductName.substring(0, dbProductName.indexOf(":"));
urlProps.setProperty(TSDBDriver.PROPERTY_KEY_PRODUCT_NAME, dbProductName);
// parse database name // parse database name
url = url.substring(beginningOfSlashes + 2); url = url.substring(beginningOfSlashes + 2);
......
...@@ -16,18 +16,21 @@ ...@@ -16,18 +16,21 @@
*/ */
package com.taosdata.jdbc; package com.taosdata.jdbc;
import com.alibaba.fastjson.JSONObject;
import com.taosdata.jdbc.utils.TaosInfo; import com.taosdata.jdbc.utils.TaosInfo;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.SQLWarning; import java.sql.SQLWarning;
import java.util.List; import java.util.List;
import java.util.Properties;
/** /**
* JNI connector * JNI connector
*/ */
public class TSDBJNIConnector { 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 final TaosInfo taosInfo = TaosInfo.getInstance();
private long taos = TSDBConstants.JNI_NULL_POINTER; // Connection pointer used in C private long taos = TSDBConstants.JNI_NULL_POINTER; // Connection pointer used in C
...@@ -38,10 +41,23 @@ public class TSDBJNIConnector { ...@@ -38,10 +41,23 @@ public class TSDBJNIConnector {
System.loadLibrary("taos"); System.loadLibrary("taos");
} }
public static void init(String configDir, String locale, String charset, String timezone) throws SQLWarning { public static void init(Properties props) throws SQLWarning {
synchronized (isInitialized) { synchronized (LOCK) {
if (!isInitialized) { 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); 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) { if (setOptions(0, locale) < 0) {
throw TSDBError.createSQLWarning("Failed to set locale: " + locale + ". System default will be used."); throw TSDBError.createSQLWarning("Failed to set locale: " + locale + ". System default will be used.");
} }
......
package com.taosdata.jdbc; package com.taosdata.jdbc;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.Properties;
public class SetConfigurationInJNITest { public class SetConfigurationInJNITest {
private String host = "127.0.0.1"; private String host = "127.0.0.1";
private String dbname = "test_jni"; private String dbname = "test_set_config";
private String debugFlagJSON = "{ \"debugFlag\": \"135\"}";
private long maxSQLLength = 1024000;
private String maxSqlLengthJSON = "{ \"maxSQLLength\": " + maxSQLLength + "}";
@Test @Test
public void setConfigBeforeConnectIsValid() { public void setConfigInUrl() {
try { try {
TSDBJNIConnector.setConfig(debugFlagJSON); Connection conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/?user=root&password=taosdata&debugFlag=135");
Connection conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/?user=root&password=taosdata");
Statement stmt = conn.createStatement(); Statement stmt = conn.createStatement();
stmt.execute("drop database if exists " + dbname); stmt.execute("drop database if exists " + dbname);
...@@ -40,13 +34,13 @@ public class SetConfigurationInJNITest { ...@@ -40,13 +34,13 @@ public class SetConfigurationInJNITest {
} }
@Test @Test
public void setConfigAfterConnectIsInvalid() { public void setConfigInProperties() {
try { 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(); Statement stmt = conn.createStatement();
stmt.execute("drop database if exists " + dbname); stmt.execute("drop database if exists " + dbname);
stmt.execute("create database if not exists " + dbname); stmt.execute("create database if not exists " + dbname);
stmt.execute("use " + dbname); stmt.execute("use " + dbname);
......
...@@ -7,6 +7,7 @@ import java.lang.management.RuntimeMXBean; ...@@ -7,6 +7,7 @@ import java.lang.management.RuntimeMXBean;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
...@@ -21,21 +22,22 @@ public class TSDBJNIConnectorTest { ...@@ -21,21 +22,22 @@ public class TSDBJNIConnectorTest {
try { try {
//change sleepSeconds when debugging with attach to process to find PID //change sleepSeconds when debugging with attach to process to find PID
int sleepSeconds = -1; int sleepSeconds = -1;
if (sleepSeconds>0) { if (sleepSeconds > 0) {
RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean(); RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
String jvmName = runtimeBean.getName(); String jvmName = runtimeBean.getName();
long pid = Long.valueOf(jvmName.split("@")[0]); long pid = Long.valueOf(jvmName.split("@")[0]);
System.out.println("JVM PID = " + pid); System.out.println("JVM PID = " + pid);
Thread.sleep(sleepSeconds*1000); Thread.sleep(sleepSeconds * 1000);
} }
} } catch (Exception e) {
catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
// init // 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 // connect
TSDBJNIConnector connector = new TSDBJNIConnector(); TSDBJNIConnector connector = new TSDBJNIConnector();
...@@ -43,12 +45,12 @@ public class TSDBJNIConnectorTest { ...@@ -43,12 +45,12 @@ public class TSDBJNIConnectorTest {
// setup // setup
String setupSqlStrs[] = {"create database if not exists d precision \"us\"", String setupSqlStrs[] = {"create database if not exists d precision \"us\"",
"create table if not exists d.t(ts timestamp, f int)", "create table if not exists d.t(ts timestamp, f int)",
"create database if not exists d2", "create database if not exists d2",
"create table if not exists d2.t2(ts timestamp, f int)", "create table if not exists d2.t2(ts timestamp, f int)",
"insert into d.t values(now+100s, 100)", "insert into d.t values(now+100s, 100)",
"insert into d2.t2 values(now+200s, 200)" "insert into d2.t2 values(now+200s, 200)"
}; };
for (String setupSqlStr : setupSqlStrs) { for (String setupSqlStr : setupSqlStrs) {
long setupSql = connector.executeQuery(setupSqlStr); long setupSql = connector.executeQuery(setupSqlStr);
...@@ -113,8 +115,8 @@ public class TSDBJNIConnectorTest { ...@@ -113,8 +115,8 @@ public class TSDBJNIConnectorTest {
} }
// close statement // close statement
connector.executeQuery("use d"); 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", 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"}; "st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000ns"};
connector.insertLines(lines); connector.insertLines(lines);
// close connection // close connection
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册