ConnectionPoolDemo.java 4.2 KB
Newer Older
Z
change  
zyyang 已提交
1
package com.taosdata.example;
2

Z
change  
zyyang 已提交
3 4 5 6
import com.taosdata.example.pool.C3p0Builder;
import com.taosdata.example.pool.DbcpBuilder;
import com.taosdata.example.pool.DruidPoolBuilder;
import com.taosdata.example.pool.HikariCpBuilder;
7 8 9 10 11 12 13 14 15 16
import org.apache.log4j.Logger;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectionPoolDemo {

    private static Logger logger = Logger.getLogger(DruidPoolBuilder.class);
17
    private static final String dbName = "pool_test";
18

Z
change  
zyyang 已提交
19
    private static long totalSize = 1_000_000l;
20 21 22 23 24
    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;
25 26
    private static String poolType = "hikari";

27

Z
change  
zyyang 已提交
28
    public static void main(String[] args) {
29 30 31 32 33
        String host = null;
        for (int i = 0; i < args.length; i++) {
            if ("-host".equalsIgnoreCase(args[i]) && i < args.length - 1) {
                host = args[++i];
            }
34 35 36
            if ("-poolType".equalsIgnoreCase(args[i]) && i < args.length - 1) {
                poolType = args[++i];
            }
37 38
        }
        if (host == null) {
Z
change  
zyyang 已提交
39
            System.out.println("Usage: java -jar XXX.jar -host <hostname> -poolType <c3p0| dbcp| druid| hikari>");
40 41 42
            return;
        }

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
        DataSource dataSource;
        switch (poolType) {
            case "c3p0":
                dataSource = C3p0Builder.getDataSource(host, poolSize);
                break;
            case "dbcp":
                dataSource = DbcpBuilder.getDataSource(host, poolSize);
                break;
            case "druid":
                dataSource = DruidPoolBuilder.getDataSource(host, poolSize);
                break;
            case "hikari":
            default:
                dataSource = HikariCpBuilder.getDataSource(host, poolSize);
                poolType = "hikari";
        }

        logger.info(">>>>>>>>>>>>>> connection pool Type: " + poolType);
61 62
        init(dataSource);

Z
change  
zyyang 已提交
63 64 65 66 67 68 69 70 71 72 73 74
        try {
            Connection connection = dataSource.getConnection();
            Statement statement = connection.createStatement();
            String sql = "insert into " + dbName + ".t_1 values('2020-01-01 00:00:00.000',12.12,111)";
            int affectRows = statement.executeUpdate(sql);
            System.out.println("affectRows >>> " + affectRows);
            affectRows = statement.executeUpdate(sql);
            System.out.println("affectRows >>> " + affectRows);
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
Z
change  
zyyang 已提交
75
        }
Z
change  
zyyang 已提交
76 77 78 79 80 81 82 83 84 85 86 87

//        ExecutorService executor = Executors.newFixedThreadPool(threadCount);
//        for (long i = 0; i < totalSize / batchSize / tableSize; i++) {
//            executor.execute(new InsertTask(dataSource, dbName, tableSize, batchSize));
//            // sleep few seconds
//            try {
//                TimeUnit.MILLISECONDS.sleep(sleep);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//        executor.shutdown();
Z
change  
zyyang 已提交
88

89 90 91 92 93 94 95 96 97 98 99
    }

    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) + ")");
            }
100
            logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>> init finished.");
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        } 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();
        }
    }

}