diff --git a/cmake/install.inc b/cmake/install.inc index f8b3b7c3c62c16df537a941d7f14ccb74bc64217..b37cf751fbf23966671a571385b65d93bd22865d 100755 --- a/cmake/install.inc +++ b/cmake/install.inc @@ -32,7 +32,7 @@ ELSEIF (TD_WINDOWS) #INSTALL(TARGETS taos RUNTIME DESTINATION driver) #INSTALL(TARGETS shell RUNTIME DESTINATION .) IF (TD_MVN_INSTALLED) - INSTALL(FILES ${LIBRARY_OUTPUT_PATH}/taos-jdbcdriver-2.0.29.jar DESTINATION connector/jdbc) + INSTALL(FILES ${LIBRARY_OUTPUT_PATH}/taos-jdbcdriver-2.0.31.jar DESTINATION connector/jdbc) ENDIF () ELSEIF (TD_DARWIN) SET(TD_MAKE_INSTALL_SH "${TD_COMMUNITY_DIR}/packaging/tools/make_install.sh") diff --git a/src/connector/jdbc/CMakeLists.txt b/src/connector/jdbc/CMakeLists.txt index 61e976cb184d3240bdc8917a5b2e5f3182d55fcc..1db8361fafc985857987a4dfc5a041ae14c86df7 100644 --- a/src/connector/jdbc/CMakeLists.txt +++ b/src/connector/jdbc/CMakeLists.txt @@ -8,10 +8,8 @@ IF (TD_MVN_INSTALLED) ADD_CUSTOM_COMMAND(OUTPUT ${JDBC_CMD_NAME} POST_BUILD COMMAND mvn -Dmaven.test.skip=true install -f ${CMAKE_CURRENT_SOURCE_DIR}/pom.xml - COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/target/taos-jdbcdriver-2.0.29.jar ${LIBRARY_OUTPUT_PATH} + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/target/taos-jdbcdriver-2.0.31.jar ${LIBRARY_OUTPUT_PATH} COMMAND mvn -Dmaven.test.skip=true clean -f ${CMAKE_CURRENT_SOURCE_DIR}/pom.xml COMMENT "build jdbc driver") ADD_CUSTOM_TARGET(${JDBC_TARGET_NAME} ALL WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} DEPENDS ${JDBC_CMD_NAME}) -ENDIF () - - +ENDIF () \ No newline at end of file diff --git a/src/connector/jdbc/deploy-pom.xml b/src/connector/jdbc/deploy-pom.xml index 968a9bf470ddde1996467467fa24cfd2fbf355e0..8f77582d30d1d8f2b0bf637573da6bbc229b40ae 100755 --- a/src/connector/jdbc/deploy-pom.xml +++ b/src/connector/jdbc/deploy-pom.xml @@ -5,7 +5,7 @@ com.taosdata.jdbc taos-jdbcdriver - 2.0.29 + 2.0.31 jar JDBCDriver diff --git a/src/connector/jdbc/pom.xml b/src/connector/jdbc/pom.xml index ef353d1d19d37da3c1a3e9bdc4a2a378cf4edc0c..d08d5502c2caa2a0d2726ea896ad1e0708ef2f8b 100755 --- a/src/connector/jdbc/pom.xml +++ b/src/connector/jdbc/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.taosdata.jdbc taos-jdbcdriver - 2.0.29 + 2.0.31 jar JDBCDriver https://github.com/taosdata/TDengine/tree/master/src/connector/jdbc @@ -123,6 +123,7 @@ **/InvalidResultSetPointerTest.java **/RestfulConnectionTest.java **/TD4144Test.java + **/ConnectMultiTaosdByRestfulWithDifferentTokenTest.java true diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java index b810f9aeb59247ef618451b542a678d6c951ad3f..d6a02b7e3a7d6ef2d7527cd101743cc4575b43ba 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java @@ -17,16 +17,18 @@ public class RestfulConnection extends AbstractConnection { private final int port; private final String url; private volatile String database; + private final String token; /******************************************************/ private boolean isClosed; private final DatabaseMetaData metadata; - public RestfulConnection(String host, String port, Properties props, String database, String url) { + public RestfulConnection(String host, String port, Properties props, String database, String url, String token) { super(props); this.host = host; this.port = Integer.parseInt(port); this.database = database; this.url = url; + this.token = token; this.metadata = new RestfulDatabaseMetaData(url, props.getProperty(TSDBDriver.PROPERTY_KEY_USER), this); } @@ -66,6 +68,7 @@ public class RestfulConnection extends AbstractConnection { return this.metadata; } + // getters public String getHost() { return host; } @@ -81,4 +84,8 @@ public class RestfulConnection extends AbstractConnection { public String getUrl() { return url; } + + public String getToken() { + return token; + } } \ No newline at end of file diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java index a94cfa6e07c62ee9b163823aaad03bcc402bbffc..9ab67c5502e33d8f2337e3acc8d8eab425992e3a 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulDriver.java @@ -38,15 +38,11 @@ public class RestfulDriver extends AbstractDriver { String port = props.getProperty(TSDBDriver.PROPERTY_KEY_PORT, "6041"); String database = props.containsKey(TSDBDriver.PROPERTY_KEY_DBNAME) ? props.getProperty(TSDBDriver.PROPERTY_KEY_DBNAME) : null; - String loginUrl = "http://" + props.getProperty(TSDBDriver.PROPERTY_KEY_HOST) + ":" - + props.getProperty(TSDBDriver.PROPERTY_KEY_PORT) + "/rest/login/" - + props.getProperty(TSDBDriver.PROPERTY_KEY_USER) + "/" - + props.getProperty(TSDBDriver.PROPERTY_KEY_PASSWORD) + ""; + String loginUrl = "http://" + host + ":" + port + "/rest/login/" + props.getProperty(TSDBDriver.PROPERTY_KEY_USER) + "/" + props.getProperty(TSDBDriver.PROPERTY_KEY_PASSWORD) + ""; try { String user = URLEncoder.encode(props.getProperty(TSDBDriver.PROPERTY_KEY_USER), "UTF-8"); String password = URLEncoder.encode(props.getProperty(TSDBDriver.PROPERTY_KEY_PASSWORD), "UTF-8"); - loginUrl = "http://" + props.getProperty(TSDBDriver.PROPERTY_KEY_HOST) + ":" - + props.getProperty(TSDBDriver.PROPERTY_KEY_PORT) + "/rest/login/" + user + "/" + password + ""; + loginUrl = "http://" + props.getProperty(TSDBDriver.PROPERTY_KEY_HOST) + ":" + props.getProperty(TSDBDriver.PROPERTY_KEY_PORT) + "/rest/login/" + user + "/" + password + ""; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } @@ -55,12 +51,12 @@ public class RestfulDriver extends AbstractDriver { JSONObject jsonResult = JSON.parseObject(result); String status = jsonResult.getString("status"); String token = jsonResult.getString("desc"); - HttpClientPoolUtil.token = token; + if (!status.equals("succ")) { throw new SQLException(jsonResult.getString("desc")); } - RestfulConnection conn = new RestfulConnection(host, port, props, database, url); + RestfulConnection conn = new RestfulConnection(host, port, props, database, url, token); if (database != null && !database.trim().replaceAll("\\s", "").isEmpty()) { Statement stmt = conn.createStatement(); stmt.execute("use " + database); diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java index fbc3a50a27a18db069a25ea86fe37027dca42f1f..e9d193f6b412f6ab835d39f97a229f137e48cacf 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java @@ -83,7 +83,7 @@ public class RestfulStatement extends AbstractStatement { } if (SqlSyntaxValidator.isUseSql(sql)) { - HttpClientPoolUtil.execute(url, sql); + HttpClientPoolUtil.execute(url, sql, this.conn.getToken()); this.database = sql.trim().replace("use", "").trim(); this.conn.setCatalog(this.database); result = false; @@ -116,7 +116,7 @@ public class RestfulStatement extends AbstractStatement { if ("UTC".equalsIgnoreCase(timestampFormat)) url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sqlutc"; - String result = HttpClientPoolUtil.execute(url, sql); + String result = HttpClientPoolUtil.execute(url, sql, this.conn.getToken()); JSONObject resultJson = JSON.parseObject(result); if (resultJson.getString("status").equals("error")) { throw TSDBError.createSQLException(resultJson.getInteger("code"), resultJson.getString("desc")); @@ -130,7 +130,7 @@ public class RestfulStatement extends AbstractStatement { if (!SqlSyntaxValidator.isValidForExecuteUpdate(sql)) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_FOR_EXECUTE_UPDATE, "not a valid sql for executeUpdate: " + sql); - String result = HttpClientPoolUtil.execute(url, sql); + String result = HttpClientPoolUtil.execute(url, sql, this.conn.getToken()); JSONObject jsonObject = JSON.parseObject(result); if (jsonObject.getString("status").equals("error")) { throw TSDBError.createSQLException(jsonObject.getInteger("code"), jsonObject.getString("desc")); diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/HttpClientPoolUtil.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/HttpClientPoolUtil.java index 5e2440fd1d0e551690c5f17fe60b30c1d4a377dc..b1c6dae6b3543337ae253c4cf94c2aaa5a31f9f5 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/HttpClientPoolUtil.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/HttpClientPoolUtil.java @@ -20,39 +20,29 @@ import java.nio.charset.Charset; public class HttpClientPoolUtil { - public static PoolingHttpClientConnectionManager cm = null; - public static CloseableHttpClient httpClient = null; - public static String token = "cm9vdDp0YW9zZGF0YQ=="; - /** - * 默认content 类型 - */ + private static final String DEFAULT_CONTENT_TYPE = "application/json"; - /** - * 默认请求超时时间30s - */ + private static final String DEFAULT_TOKEN = "cm9vdDp0YW9zZGF0YQ=="; private static final int DEFAULT_TIME_OUT = 15000; - private static final int count = 32; - private static final int totalCount = 1000; - private static final int Http_Default_Keep_Time = 15000; + private static final int DEFAULT_MAX_PER_ROUTE = 32; + private static final int DEFAULT_MAX_TOTAL = 1000; + private static final int DEFAULT_HTTP_KEEP_TIME = 15000; + + private static PoolingHttpClientConnectionManager connectionManager; + private static CloseableHttpClient httpClient; - /** - * 初始化连接池 - */ private static synchronized void initPools() { if (httpClient == null) { - cm = new PoolingHttpClientConnectionManager(); - cm.setDefaultMaxPerRoute(count); - cm.setMaxTotal(totalCount); - httpClient = HttpClients.custom().setKeepAliveStrategy(defaultStrategy).setConnectionManager(cm).build(); + connectionManager = new PoolingHttpClientConnectionManager(); + connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE); + connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL); + httpClient = HttpClients.custom().setKeepAliveStrategy(DEFAULT_KEEP_ALIVE_STRATEGY).setConnectionManager(connectionManager).build(); } } - /** - * Http connection keepAlive 设置 - */ - private static ConnectionKeepAliveStrategy defaultStrategy = (response, context) -> { + private static ConnectionKeepAliveStrategy DEFAULT_KEEP_ALIVE_STRATEGY = (response, context) -> { HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE)); - int keepTime = Http_Default_Keep_Time * 1000; + int keepTime = DEFAULT_HTTP_KEEP_TIME * 1000; while (it.hasNext()) { HeaderElement headerElement = it.nextElement(); String param = headerElement.getName(); @@ -76,7 +66,7 @@ public class HttpClientPoolUtil { * @param data 请求数据 * @return responseBody */ - public static String execute(String uri, String data) { + public static String execute(String uri, String data, String token) { long startTime = System.currentTimeMillis(); HttpEntity httpEntity = null; HttpEntityEnclosingRequestBase method = null; diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/BatchInsertTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/BatchInsertTest.java index 472da34980d98cbe694783b120bc5533a908fa5b..e2541e8109681569d75a7384255df09d6166c34e 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/BatchInsertTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/BatchInsertTest.java @@ -28,9 +28,7 @@ public class BatchInsertTest { @Before public void before() { try { - Class.forName("com.taosdata.jdbc.TSDBDriver"); Properties properties = new Properties(); - properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host); properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); @@ -44,7 +42,7 @@ public class BatchInsertTest { String createTableSql = "create table " + stbName + "(ts timestamp, f1 int, f2 int, f3 int) tags(areaid int, loc binary(20))"; statement.executeUpdate(createTableSql); // create tables - for(int i = 0; i < numOfTables; i++) { + for (int i = 0; i < numOfTables; i++) { String loc = i % 2 == 0 ? "beijing" : "shanghai"; String createSubTalbesSql = "create table " + tablePrefix + i + " using " + stbName + " tags(" + i + ", '" + loc + "')"; statement.executeUpdate(createSubTalbesSql); diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/ConnectMultiTaosdByRestfulWithDifferentTokenTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/ConnectMultiTaosdByRestfulWithDifferentTokenTest.java new file mode 100644 index 0000000000000000000000000000000000000000..7f2d367dce4e5691603e23db8a14a4f857bb7b88 --- /dev/null +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/ConnectMultiTaosdByRestfulWithDifferentTokenTest.java @@ -0,0 +1,59 @@ +package com.taosdata.jdbc.cases; + +import com.taosdata.jdbc.TSDBDriver; +import org.junit.Before; +import org.junit.Test; + +import java.sql.*; +import java.util.Properties; + +public class ConnectMultiTaosdByRestfulWithDifferentTokenTest { + + private static String host1 = "192.168.17.156"; + private static String user1 = "root"; + private static String password1 = "tqueue"; + private Connection conn1; + private static String host2 = "192.168.17.82"; + private static String user2 = "root"; + private static String password2 = "taosdata"; + private Connection conn2; + + @Test + public void test() { + //when + executeSelectStatus(conn1); + executeSelectStatus(conn2); + executeSelectStatus(conn1); + } + + private void executeSelectStatus(Connection connection) { + try (Statement stmt = connection.createStatement()) { + ResultSet rs = stmt.executeQuery("select server_status()"); + ResultSetMetaData meta = rs.getMetaData(); + while (rs.next()) { + for (int i = 1; i <= meta.getColumnCount(); i++) { + System.out.println(meta.getColumnLabel(i) + ": " + rs.getString(i)); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Before + public void before() { + Properties properties = new Properties(); + properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); + properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); + properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); + + String url1 = "jdbc:TAOS-RS://" + host1 + ":6041/?user=" + user1 + "&password=" + password1; + String url2 = "jdbc:TAOS-RS://" + host2 + ":6041/?user=" + user2 + "&password=" + password2; + try { + conn1 = DriverManager.getConnection(url1, properties); + conn2 = DriverManager.getConnection(url2, properties); + } catch (SQLException e) { + e.printStackTrace(); + } + } +}