From a77f8242625fac3ac9905327f321e8f43abe3475 Mon Sep 17 00:00:00 2001 From: Zhiyu Yang <69311263+zyyang-taosdata@users.noreply.github.com> Date: Wed, 25 Aug 2021 14:07:52 +0800 Subject: [PATCH] [TD-6004]: JDBC-RSETful support specify dbname in http url (#7573) --- .../taosdata/jdbc/rs/RestfulConnection.java | 2 +- .../taosdata/jdbc/rs/RestfulStatement.java | 13 ++- .../MultiConnectionWithDifferentDbTest.java | 101 ++++++++++++++++++ .../jdbc/rs/DatabaseSpecifiedTest.java | 69 ++++++++++++ 4 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/MultiConnectionWithDifferentDbTest.java create mode 100644 src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/DatabaseSpecifiedTest.java 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 12a0ab57e2..e818736096 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 @@ -18,7 +18,7 @@ public class RestfulConnection extends AbstractConnection { private final String url; private final String database; private final String token; - /******************************************************/ + private boolean isClosed; private final DatabaseMetaData metadata; 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 a88dc411f3..21c76f73b2 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 @@ -88,17 +88,24 @@ public class RestfulStatement extends AbstractStatement { } private String getUrl() throws SQLException { + String dbname = conn.getClientInfo(TSDBDriver.PROPERTY_KEY_DBNAME); + if (dbname == null || dbname.trim().isEmpty()) { + dbname = ""; + } else { + dbname = "/" + dbname.toLowerCase(); + } TimestampFormat timestampFormat = TimestampFormat.valueOf(conn.getClientInfo(TSDBDriver.PROPERTY_KEY_TIMESTAMP_FORMAT).trim().toUpperCase()); String url; + switch (timestampFormat) { case TIMESTAMP: - url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sqlt"; + url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sqlt" + dbname; break; case UTC: - url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sqlutc"; + url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sqlutc" + dbname; break; default: - url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sql"; + url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sql" + dbname; } return url; } diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/MultiConnectionWithDifferentDbTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/MultiConnectionWithDifferentDbTest.java new file mode 100644 index 0000000000..18a2c32aca --- /dev/null +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/MultiConnectionWithDifferentDbTest.java @@ -0,0 +1,101 @@ +package com.taosdata.jdbc.cases; + +import org.junit.Before; +import org.junit.Test; + +import java.sql.*; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class MultiConnectionWithDifferentDbTest { + + private static String host = "127.0.0.1"; + private static String db1 = "db1"; + private static String db2 = "db2"; + + private long ts; + + @Test + public void test() { + List threads = IntStream.range(1, 3).mapToObj(i -> new Thread(new Runnable() { + @Override + public void run() { + for (int j = 0; j < 10; j++) { + queryDb(); + try { + TimeUnit.SECONDS.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + private void queryDb() { + String url = "jdbc:TAOS-RS://" + host + ":6041/db" + i + "?user=root&password=taosdata"; + try (Connection connection = DriverManager.getConnection(url)) { + Statement stmt = connection.createStatement(); + + ResultSet rs = stmt.executeQuery("select * from weather"); + assertNotNull(rs); + rs.next(); + long actual = rs.getTimestamp("ts").getTime(); + assertEquals(ts, actual); + + int f1 = rs.getInt("f1"); + assertEquals(i, f1); + + String loc = i == 1 ? "beijing" : "shanghai"; + String loc_actual = rs.getString("loc"); + assertEquals(loc, loc_actual); + + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + }, "thread-" + i)).collect(Collectors.toList()); + + threads.forEach(Thread::start); + + for (Thread t : threads) { + try { + t.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + } + + @Before + public void before() { + ts = System.currentTimeMillis(); + + try { + Connection conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata"); + + Statement stmt = conn.createStatement(); + stmt.execute("drop database if exists " + db1); + stmt.execute("create database if not exists " + db1); + stmt.execute("use " + db1); + stmt.execute("create table weather(ts timestamp, f1 int) tags(loc nchar(10))"); + stmt.execute("insert into t1 using weather tags('beijing') values(" + ts + ", 1)"); + + stmt.execute("drop database if exists " + db2); + stmt.execute("create database if not exists " + db2); + stmt.execute("use " + db2); + stmt.execute("create table weather(ts timestamp, f1 int) tags(loc nchar(10))"); + stmt.execute("insert into t1 using weather tags('shanghai') values(" + ts + ", 2)"); + + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + +} diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/DatabaseSpecifiedTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/DatabaseSpecifiedTest.java new file mode 100644 index 0000000000..9fe51e7203 --- /dev/null +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/DatabaseSpecifiedTest.java @@ -0,0 +1,69 @@ +package com.taosdata.jdbc.rs; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.sql.*; + +import static org.junit.Assert.*; + +public class DatabaseSpecifiedTest { + + private static String host = "127.0.0.1"; + private static String dbname = "test_db_spec"; + + private Connection connection; + private long ts; + + @Test + public void test() throws SQLException { + // when + connection = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/" + dbname + "?user=root&password=taosdata"); + try (Statement stmt = connection.createStatement();) { + ResultSet rs = stmt.executeQuery("select * from weather"); + + //then + assertNotNull(rs); + rs.next(); + long now = rs.getTimestamp("ts").getTime(); + assertEquals(ts, now); + int f1 = rs.getInt(2); + assertEquals(1, f1); + String loc = rs.getString("loc"); + assertEquals("beijing", loc); + } + connection.close(); + } + + @Before + public void before() { + ts = System.currentTimeMillis(); + try { + Connection connection = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata"); + Statement stmt = connection.createStatement(); + + stmt.execute("drop database if exists " + dbname); + stmt.execute("create database if not exists " + dbname); + stmt.execute("use " + dbname); + stmt.execute("create table weather(ts timestamp, f1 int) tags(loc nchar(10))"); + stmt.execute("insert into t1 using weather tags('beijing') values( " + ts + ", 1)"); + + stmt.close(); + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @After + public void after() { + try { + if (connection != null) + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + +} -- GitLab