From 9bfa0c8eb8633c004df7f0bf38108e66d6d6d32e Mon Sep 17 00:00:00 2001 From: zyyang Date: Wed, 11 Nov 2020 11:51:13 +0800 Subject: [PATCH] [TD-2052]: common connection pool demos --- src/connector/jdbc/deploy-pom.xml | 3 +- tests/examples/JDBC/connectionPools/pom.xml | 51 ++++++++++ .../com/taosdata/demo/ConnectionPoolDemo.java | 96 +++++++++++++++++++ .../com/taosdata/demo/common/InsertTask.java | 67 +++++++++++++ .../com/taosdata/demo/pool/DbcpBuilder.java | 21 ++++ .../taosdata/demo/pool/DruidPoolBuilder.java | 31 ++++++ .../taosdata/demo/pool/HikariCpBuilder.java | 22 +++++ .../src/main/resources/app.properties | 1 + .../src/main/resources/log4j.properties | 21 ++++ 9 files changed, 311 insertions(+), 2 deletions(-) create mode 100644 tests/examples/JDBC/connectionPools/pom.xml create mode 100644 tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/ConnectionPoolDemo.java create mode 100644 tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/common/InsertTask.java create mode 100644 tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/pool/DbcpBuilder.java create mode 100644 tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/pool/DruidPoolBuilder.java create mode 100644 tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/pool/HikariCpBuilder.java create mode 100644 tests/examples/JDBC/connectionPools/src/main/resources/app.properties create mode 100644 tests/examples/JDBC/connectionPools/src/main/resources/log4j.properties diff --git a/src/connector/jdbc/deploy-pom.xml b/src/connector/jdbc/deploy-pom.xml index 893290868d..51db837c7b 100755 --- a/src/connector/jdbc/deploy-pom.xml +++ b/src/connector/jdbc/deploy-pom.xml @@ -5,14 +5,13 @@ com.taosdata.jdbc taos-jdbcdriver - 2.0.9 + 2.0.10 jar JDBCDriver https://github.com/taosdata/TDengine/tree/master/src/connector/jdbc TDengine JDBC Driver - GNU AFFERO GENERAL PUBLIC LICENSE Version 3 diff --git a/tests/examples/JDBC/connectionPools/pom.xml b/tests/examples/JDBC/connectionPools/pom.xml new file mode 100644 index 0000000000..f0c0d45ac6 --- /dev/null +++ b/tests/examples/JDBC/connectionPools/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + com.taosdata.demo + connectionPools + 1.0-SNAPSHOT + + + + + com.taosdata.jdbc + taos-jdbcdriver + 2.0.10 + + + + + com.alibaba + druid + 1.1.17 + + + + com.zaxxer + HikariCP + 3.2.0 + + + + commons-pool + commons-pool + 1.5.4 + + + commons-dbcp + commons-dbcp + 1.4 + + + + + log4j + log4j + 1.2.17 + + + + \ No newline at end of file diff --git a/tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/ConnectionPoolDemo.java b/tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/ConnectionPoolDemo.java new file mode 100644 index 0000000000..bb40ab1b15 --- /dev/null +++ b/tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/ConnectionPoolDemo.java @@ -0,0 +1,96 @@ +package com.taosdata.demo; + +import com.taosdata.demo.common.InsertTask; +import com.taosdata.demo.pool.DbcpBuilder; +import com.taosdata.demo.pool.DruidPoolBuilder; +import com.taosdata.demo.pool.HikariCpBuilder; +import org.apache.log4j.Logger; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +public class ConnectionPoolDemo { + + private static Logger logger = Logger.getLogger(DruidPoolBuilder.class); + + private static int batchSize = 10; + private static int sleep = 1000; + private static int poolSize = 50; + private static int tableSize = 1000; + private static int threadCount = 50; + private static final String dbName = "pool_test"; + + public static void main(String[] args) throws InterruptedException { + String host = null; + + for (int i = 0; i < args.length; i++) { + if ("-host".equalsIgnoreCase(args[i]) && i < args.length - 1) { + host = args[++i]; + } + if ("-batchSize".equalsIgnoreCase(args[i]) && i < args.length - 1) { + batchSize = Integer.parseInt(args[++i]); + } + if ("-sleep".equalsIgnoreCase(args[i]) && i < args.length - 1) { + sleep = Integer.parseInt(args[++i]); + } + if ("-poolSize".equalsIgnoreCase(args[i]) && i < args.length - 1) { + poolSize = Integer.parseInt(args[++i]); + } + if ("-tableSize".equalsIgnoreCase(args[i]) && i < args.length - 1) { + tableSize = Integer.parseInt(args[++i]); + } + } + if (host == null) { + System.out.println("Usage: java -jar XXX.jar " + + "-host " + + "-batchSize " + + "-sleep " + + "-poolSize " + + "-tableSize "); + return; + } + +// DataSource dataSource = DbcpBuilder.getDataSource(host, poolSize); +// DataSource dataSource = DruidPoolBuilder.getDataSource(host, poolSize); + DataSource dataSource = HikariCpBuilder.getDataSource(host, poolSize); + + init(dataSource); + + ExecutorService executor = Executors.newFixedThreadPool(threadCount); + while (true) { + executor.execute(new InsertTask(dataSource, dbName, tableSize, batchSize)); + if (sleep > 0) + TimeUnit.MILLISECONDS.sleep(sleep); + } + } + + private static void init(DataSource dataSource) { + try (Connection conn = dataSource.getConnection()) { + execute(conn, "drop database if exists " + dbName + ""); + execute(conn, "create database if not exists " + dbName + ""); + execute(conn, "use " + dbName + ""); + execute(conn, "create table weather(ts timestamp, temperature float, humidity int) tags(location nchar(64), groupId int)"); + for (int tb_ind = 1; tb_ind <= tableSize; tb_ind++) { + execute(conn, "create table t_" + tb_ind + " using weather tags('beijing'," + (tb_ind + 1) + ")"); + } + System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> init finished."); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + private static void execute(Connection con, String sql) { + try (Statement stmt = con.createStatement()) { + stmt.executeUpdate(sql); + logger.info("SQL >>> " + sql); + } catch (SQLException e) { + e.printStackTrace(); + } + } + +} diff --git a/tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/common/InsertTask.java b/tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/common/InsertTask.java new file mode 100644 index 0000000000..f8a1056eb4 --- /dev/null +++ b/tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/common/InsertTask.java @@ -0,0 +1,67 @@ +package com.taosdata.demo.common; + +import org.apache.log4j.Logger; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Random; + +public class InsertTask implements Runnable { + private final Random random = new Random(System.currentTimeMillis()); + private static Logger logger = Logger.getLogger(InsertTask.class); + + private final DataSource ds; + private final int batchSize; + private final String dbName; + private final int tableSize; + + public InsertTask(DataSource ds, String dbName, int tableSize, int batchSize) { + this.ds = ds; + this.dbName = dbName; + this.tableSize = tableSize; + this.batchSize = batchSize; + } + + @Override + public void run() { + Connection conn = null; + Statement stmt = null; + int affectedRows = 0; + + long start = System.currentTimeMillis(); + try { + conn = ds.getConnection(); + stmt = conn.createStatement(); + + for (int tb_index = 1; tb_index <= tableSize; tb_index++) { + StringBuilder sb = new StringBuilder(); + sb.append("insert into " + dbName + ".t_" + tb_index + "(ts, temperature, humidity) values "); + for (int i = 0; i < batchSize; i++) { + sb.append("(" + (start + i) + ", " + (random.nextFloat() * 30) + ", " + (random.nextInt(70)) + ") "); + } + logger.info("SQL >>> " + sb.toString()); + affectedRows += stmt.executeUpdate(sb.toString()); + } + + } catch (SQLException e) { + e.printStackTrace(); + } finally { + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + } + } + if (conn != null) { + try { + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + logger.info(">>> affectedRows:" + affectedRows + " TimeCost:" + (System.currentTimeMillis() - start) + " ms"); + } + } +} diff --git a/tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/pool/DbcpBuilder.java b/tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/pool/DbcpBuilder.java new file mode 100644 index 0000000000..3c34a32532 --- /dev/null +++ b/tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/pool/DbcpBuilder.java @@ -0,0 +1,21 @@ +package com.taosdata.demo.pool; + +import org.apache.commons.dbcp.BasicDataSource; + +import javax.sql.DataSource; + +public class DbcpBuilder { + + public static DataSource getDataSource(String host, int poolSize) { + BasicDataSource ds = new BasicDataSource(); + ds.setDriverClassName("com.taosdata.jdbc.TSDBDriver"); + ds.setUrl("jdbc:TAOS://" + host + ":6030"); + ds.setUsername("root"); + ds.setPassword("taosdata"); + + ds.setMaxActive(poolSize); + ds.setMinIdle(poolSize); + ds.setInitialSize(poolSize); + return ds; + } +} diff --git a/tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/pool/DruidPoolBuilder.java b/tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/pool/DruidPoolBuilder.java new file mode 100644 index 0000000000..e5dc14c6a5 --- /dev/null +++ b/tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/pool/DruidPoolBuilder.java @@ -0,0 +1,31 @@ +package com.taosdata.demo.pool; + +import com.alibaba.druid.pool.DruidDataSource; + +import javax.sql.DataSource; + +public class DruidPoolBuilder { + + public static DataSource getDataSource(String host, int poolSize) { + final String url = "jdbc:TAOS://" + host + ":6030"; + + DruidDataSource dataSource = new DruidDataSource(); + dataSource.setUrl(url); + dataSource.setDriverClassName("com.taosdata.jdbc.TSDBDriver"); + dataSource.setUsername("root"); + dataSource.setPassword("taosdata"); + + //初始连接数,默认0 + dataSource.setInitialSize(poolSize); + //最大连接数,默认8 + dataSource.setMaxActive(poolSize); + //最小闲置数 + dataSource.setMinIdle(poolSize); + //获取连接的最大等待时间,单位毫秒 + dataSource.setMaxWait(2000); + + return dataSource; + } + + +} diff --git a/tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/pool/HikariCpBuilder.java b/tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/pool/HikariCpBuilder.java new file mode 100644 index 0000000000..87f1f4ad2c --- /dev/null +++ b/tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/pool/HikariCpBuilder.java @@ -0,0 +1,22 @@ +package com.taosdata.demo.pool; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; + +import javax.sql.DataSource; + +public class HikariCpBuilder { + + public static DataSource getDataSource(String host, int poolSize) { + HikariConfig config = new HikariConfig(); + config.setDriverClassName("com.taosdata.jdbc.TSDBDriver"); + config.setJdbcUrl("jdbc:TAOS://" + host + ":6030"); + config.setUsername("root"); + config.setPassword("taosdata"); + + config.setMaximumPoolSize(poolSize); + config.setMinimumIdle(poolSize); + HikariDataSource ds = new HikariDataSource(config); + return ds; + } +} diff --git a/tests/examples/JDBC/connectionPools/src/main/resources/app.properties b/tests/examples/JDBC/connectionPools/src/main/resources/app.properties new file mode 100644 index 0000000000..4673b60c46 --- /dev/null +++ b/tests/examples/JDBC/connectionPools/src/main/resources/app.properties @@ -0,0 +1 @@ +app.insertBatchSize=50 \ No newline at end of file diff --git a/tests/examples/JDBC/connectionPools/src/main/resources/log4j.properties b/tests/examples/JDBC/connectionPools/src/main/resources/log4j.properties new file mode 100644 index 0000000000..1299357be3 --- /dev/null +++ b/tests/examples/JDBC/connectionPools/src/main/resources/log4j.properties @@ -0,0 +1,21 @@ +### 设置### +log4j.rootLogger=debug,stdout,DebugLog,ErrorLog +### 输出信息到控制抬 ### +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target=System.out +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n +### 输出DEBUG 级别以上的日志到=logs/debug.log +log4j.appender.DebugLog=org.apache.log4j.DailyRollingFileAppender +log4j.appender.DebugLog.File=logs/debug.log +log4j.appender.DebugLog.Append=true +log4j.appender.DebugLog.Threshold=DEBUG +log4j.appender.DebugLog.layout=org.apache.log4j.PatternLayout +log4j.appender.DebugLog.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n +### 输出ERROR 级别以上的日志到=logs/error.log +log4j.appender.ErrorLog=org.apache.log4j.DailyRollingFileAppender +log4j.appender.ErrorLog.File=logs/error.log +log4j.appender.ErrorLog.Append=true +log4j.appender.ErrorLog.Threshold=ERROR +log4j.appender.ErrorLog.layout=org.apache.log4j.PatternLayout +log4j.appender.ErrorLog.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n \ No newline at end of file -- GitLab