fix: scan3 bug

This commit is contained in:
Shuaiqiang Chang 2020-06-15 16:57:04 +08:00
parent 21cdf62293
commit bfe2a9aac1
11 changed files with 50 additions and 30 deletions

View File

@ -527,8 +527,9 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData {
public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types)
throws SQLException { throws SQLException {
if (conn != null && !conn.isClosed()) { Statement stmt = null;
Statement stmt = conn.createStatement(); if (null != conn && !conn.isClosed()) {
stmt = conn.createStatement();
if (catalog == null || catalog.length() < 1) { if (catalog == null || catalog.length() < 1) {
catalog = conn.getCatalog(); catalog = conn.getCatalog();
} }

View File

@ -19,7 +19,7 @@ import java.sql.SQLWarning;
import java.util.List; import java.util.List;
public class TSDBJNIConnector { public class TSDBJNIConnector {
static volatile Boolean isInitialized = false; private static volatile Boolean isInitialized = false;
static { static {
System.loadLibrary("taos"); System.loadLibrary("taos");

View File

@ -171,8 +171,7 @@ public class TSDBSubscribe {
state = 1; state = 1;
try { try {
TSDBResultSet resultSet = consume(subscription); callBack.invoke(consume(subscription));
callBack.invoke(resultSet);
} catch (Exception e) { } catch (Exception e) {
this.cancel(); this.cancel();
throw new RuntimeException(e); throw new RuntimeException(e);

View File

@ -3,10 +3,11 @@ import org.apache.commons.lang3.StringUtils;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties; import java.util.Properties;
public class TestAsyncTSDBSubscribe { public class TestAsyncTSDBSubscribe {
public static void main(String[] args) { public static void main(String[] args) throws SQLException {
String usage = "java -cp taos-jdbcdriver-1.0.3_dev-dist.jar com.taosdata.jdbc.TSDBSubscribe -db dbName -topic topicName " + String usage = "java -cp taos-jdbcdriver-1.0.3_dev-dist.jar com.taosdata.jdbc.TSDBSubscribe -db dbName -topic topicName " +
"-tname tableName -h host"; "-tname tableName -h host";
if (args.length < 2) { if (args.length < 2) {
@ -38,7 +39,6 @@ public class TestAsyncTSDBSubscribe {
} }
Connection connection = null; Connection connection = null;
TSDBSubscribe subscribe = null;
long subscribId = 0; long subscribId = 0;
try { try {
Class.forName("com.taosdata.jdbc.TSDBDriver"); Class.forName("com.taosdata.jdbc.TSDBDriver");
@ -46,7 +46,7 @@ public class TestAsyncTSDBSubscribe {
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host); properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host);
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/" + dbName + "?user=root&password=taosdata", properties); connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/" + dbName + "?user=root&password=taosdata", properties);
String rawSql = "select * from " + tName + ";"; String rawSql = "select * from " + tName + ";";
subscribe = ((TSDBConnection) connection).createSubscribe(); TSDBSubscribe subscribe = ((TSDBConnection) connection).createSubscribe();
subscribId = subscribe.subscribe(topic, rawSql, false, 1000, new CallBack("first")); subscribId = subscribe.subscribe(topic, rawSql, false, 1000, new CallBack("first"));
long subscribId2 = subscribe.subscribe("test", rawSql, false, 1000, new CallBack("second")); long subscribId2 = subscribe.subscribe("test", rawSql, false, 1000, new CallBack("second"));
int a = 0; int a = 0;
@ -55,6 +55,9 @@ public class TestAsyncTSDBSubscribe {
System.err.println("cancel subscribe"); System.err.println("cancel subscribe");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
if (null != connection && !connection.isClosed()) {
connection.close();
}
} }
} }

View File

@ -6,19 +6,20 @@ import java.util.Properties;
public class TestPreparedStatement { public class TestPreparedStatement {
public static void main(String[] args) { public static void main(String[] args) throws SQLException {
Connection connection = null;
try { try {
Class.forName("com.taosdata.jdbc.TSDBDriver"); Class.forName("com.taosdata.jdbc.TSDBDriver");
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, "localhost"); properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, "localhost");
Connection connection = DriverManager.getConnection("jdbc:TAOS://localhost:0/?user=root&password=taosdata", properties); connection = DriverManager.getConnection("jdbc:TAOS://localhost:0/?user=root&password=taosdata", properties);
String rawSql = "select * from test.log0601"; String rawSql = "select * from test.log0601";
// String[] params = new String[]{"ts", "c1"}; // String[] params = new String[]{"ts", "c1"};
PreparedStatement pstmt = (TSDBPreparedStatement) connection.prepareStatement(rawSql); PreparedStatement pstmt = (TSDBPreparedStatement) connection.prepareStatement(rawSql);
ResultSet resSet = pstmt.executeQuery(); ResultSet resSet = pstmt.executeQuery();
while(resSet.next()) { while(resSet.next()) {
for (int i = 1; i <= resSet.getMetaData().getColumnCount(); i++) { for (int i = 1; i <= resSet.getMetaData().getColumnCount(); i++) {
System.out.printf("%d: %s\n", i, resSet.getString(i)); System.out.printf("%d: %s \n", i, resSet.getString(i));
} }
} }
resSet.close(); resSet.close();
@ -27,7 +28,9 @@ public class TestPreparedStatement {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
if (null != connection) {
connection.close();
}
} }
} }
} }

View File

@ -1,29 +1,33 @@
import com.taosdata.jdbc.TSDBDriver; import com.taosdata.jdbc.TSDBDriver;
import java.sql.Connection; import java.sql.*;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Properties; import java.util.Properties;
public class TestTSDBDatabaseMetaData { public class TestTSDBDatabaseMetaData {
public static void main(String[] args) { public static void main(String[] args) throws SQLException {
Connection connection = null;
DatabaseMetaData dbMetaData = null;
ResultSet resSet = null;
try { try {
Class.forName("com.taosdata.jdbc.TSDBDriver"); Class.forName("com.taosdata.jdbc.TSDBDriver");
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, "192.168.1.114"); properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, "localhost");
Connection connection = DriverManager.getConnection("jdbc:TAOS://192.168.1.114:0/?user=root&password=taosdata", properties); connection = DriverManager.getConnection("jdbc:TAOS://localhost:0/?user=root&password=taosdata", properties);
DatabaseMetaData dbMetaData = connection.getMetaData(); dbMetaData = connection.getMetaData();
ResultSet resSet = dbMetaData.getCatalogs(); resSet = dbMetaData.getCatalogs();
while(resSet.next()) { while(resSet.next()) {
for (int i = 1; i <= resSet.getMetaData().getColumnCount(); i++) { for (int i = 1; i <= resSet.getMetaData().getColumnCount(); i++) {
System.out.printf("dbMetaData.getCatalogs(%d) = %s\n", i, resSet.getString(i)); System.out.printf("dbMetaData.getCatalogs(%d) = %s\n", i, resSet.getString(i));
} }
} }
resSet.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
if (null != connection) {
connection.close();
}
} }
} }
} }

View File

@ -53,9 +53,10 @@ public class TestTSDBSubscribe {
subscribe = ((TSDBConnection) connection).createSubscribe(); subscribe = ((TSDBConnection) connection).createSubscribe();
subscribId = subscribe.subscribe(topic, rawSql, false, 1000); subscribId = subscribe.subscribe(topic, rawSql, false, 1000);
int a = 0; int a = 0;
TSDBResultSet resSet = null;
while (true) { while (true) {
Thread.sleep(900); Thread.sleep(900);
TSDBResultSet resSet = subscribe.consume(subscribId); resSet = subscribe.consume(subscribId);
while (resSet.next()) { while (resSet.next()) {
for (int i = 1; i <= resSet.getMetaData().getColumnCount(); i++) { for (int i = 1; i <= resSet.getMetaData().getColumnCount(); i++) {

View File

@ -37,7 +37,7 @@ public class AsyncSubscribeTest {
statement.executeUpdate("create database if not exists " + dbName); statement.executeUpdate("create database if not exists " + dbName);
statement.executeUpdate("create table if not exists " + dbName + "." + tName + " (ts timestamp, k int, v int)"); statement.executeUpdate("create table if not exists " + dbName + "." + tName + " (ts timestamp, k int, v int)");
long ts = System.currentTimeMillis(); long ts = System.currentTimeMillis();
for (int i = 0; i < 5; i++) { for (int i = 0; i < 2; i++) {
ts += i; ts += i;
statement.executeUpdate("insert into \" + dbName + \".\" + tName + \" values (" + ts + ", " + (100 + i) + ", " + i + ")"); statement.executeUpdate("insert into \" + dbName + \".\" + tName + \" values (" + ts + ", " + (100 + i) + ", " + i + ")");
} }

View File

@ -1,5 +1,6 @@
package com.taosdata.jdbc; package com.taosdata.jdbc;
import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -41,6 +42,7 @@ public class DatabaseMetaDataTest {
System.out.printf("%d: %s\n", i, resultSet.getString(i)); System.out.printf("%d: %s\n", i, resultSet.getString(i));
} }
} }
resultSet.close();
databaseMetaData.isWrapperFor(null); databaseMetaData.isWrapperFor(null);
databaseMetaData.allProceduresAreCallable(); databaseMetaData.allProceduresAreCallable();
databaseMetaData.allTablesAreSelectable(); databaseMetaData.allTablesAreSelectable();
@ -228,4 +230,11 @@ public class DatabaseMetaDataTest {
databaseMetaData.generatedKeyAlwaysReturned(); databaseMetaData.generatedKeyAlwaysReturned();
} }
@AfterClass
public static void close() throws SQLException {
statement.executeUpdate("drop database " + dbName);
statement.close();
connection.close();
}
} }

View File

@ -55,7 +55,7 @@ public class PreparedStatementTest {
@Test @Test
public void testPreparedStatement() throws SQLException { public void testPreparedStatement() throws SQLException {
long ts = System.currentTimeMillis(); long ts = System.currentTimeMillis() + 20000;
PreparedStatement saveStatement = connection PreparedStatement saveStatement = connection
.prepareStatement("insert into " + dbName + "." + tName + " values (" + ts + ", 1)"); .prepareStatement("insert into " + dbName + "." + tName + " values (" + ts + ", 1)");
@ -70,7 +70,7 @@ public class PreparedStatementTest {
TSDBPreparedStatement saveStatement = (TSDBPreparedStatement) connection TSDBPreparedStatement saveStatement = (TSDBPreparedStatement) connection
.prepareStatement("insert into " + dbName + "." + tName + " values (?, ?)"); .prepareStatement("insert into " + dbName + "." + tName + " values (?, ?)");
saveStatement.setObject(1, ts + 100); saveStatement.setObject(1, ts + 10000);
saveStatement.setObject(2, 3); saveStatement.setObject(2, 3);
int rows = saveStatement.executeUpdate(); int rows = saveStatement.executeUpdate();
assertEquals(1, rows); assertEquals(1, rows);

View File

@ -20,7 +20,7 @@ public class SubscribeTest {
String host = "localhost"; String host = "localhost";
String topic = "test"; String topic = "test";
// @Before @Before
public void createDatabase() throws SQLException { public void createDatabase() throws SQLException {
try { try {
Class.forName("com.taosdata.jdbc.TSDBDriver"); Class.forName("com.taosdata.jdbc.TSDBDriver");
@ -36,13 +36,13 @@ public class SubscribeTest {
statement.executeUpdate("create database if not exists " + dbName); statement.executeUpdate("create database if not exists " + dbName);
statement.executeUpdate("create table if not exists " + dbName + "." + tName + " (ts timestamp, k int, v int)"); statement.executeUpdate("create table if not exists " + dbName + "." + tName + " (ts timestamp, k int, v int)");
long ts = System.currentTimeMillis(); long ts = System.currentTimeMillis();
for (int i = 0; i < 5; i++) { for (int i = 0; i < 2; i++) {
ts += i; ts += i;
statement.executeUpdate("insert into \" + dbName + \".\" + tName + \" values (" + ts + ", " + (100 + i) + ", " + i + ")"); statement.executeUpdate("insert into \" + dbName + \".\" + tName + \" values (" + ts + ", " + (100 + i) + ", " + i + ")");
} }
} }
// @Test @Test
public void subscribe() throws Exception { public void subscribe() throws Exception {
TSDBSubscribe subscribe = null; TSDBSubscribe subscribe = null;
long subscribId = 0; long subscribId = 0;
@ -68,7 +68,7 @@ public class SubscribeTest {
} }
resSet.close(); resSet.close();
a++; a++;
if (a >= 3) { if (a >= 2) {
break; break;
} }
} }
@ -81,7 +81,7 @@ public class SubscribeTest {
} }
} }
// @After @After
public void close() throws Exception { public void close() throws Exception {
statement.executeQuery("drop database " + dbName); statement.executeQuery("drop database " + dbName);
statement.close(); statement.close();