提交 7e75e5ca 编写于 作者: Z zyyang

change

上级 36236474
...@@ -13,6 +13,7 @@ import java.sql.SQLException; ...@@ -13,6 +13,7 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class ConnectionPoolDemo { public class ConnectionPoolDemo {
...@@ -22,9 +23,9 @@ public class ConnectionPoolDemo { ...@@ -22,9 +23,9 @@ public class ConnectionPoolDemo {
private static int batchSize = 10; private static int batchSize = 10;
private static int sleep = 1000; private static int sleep = 1000;
private static int poolSize = 50; private static int connectionPoolSize = 50;
private static int tableSize = 1000; private static int tableSize = 1000;
private static int threadCount = 50; private static int threadPoolSize = 50;
private static String poolType = "hikari"; private static String poolType = "hikari";
...@@ -40,8 +41,8 @@ public class ConnectionPoolDemo { ...@@ -40,8 +41,8 @@ public class ConnectionPoolDemo {
if ("-sleep".equalsIgnoreCase(args[i]) && i < args.length - 1) { if ("-sleep".equalsIgnoreCase(args[i]) && i < args.length - 1) {
sleep = Integer.parseInt(args[++i]); sleep = Integer.parseInt(args[++i]);
} }
if ("-poolSize".equalsIgnoreCase(args[i]) && i < args.length - 1) { if ("-connectPoolSize".equalsIgnoreCase(args[i]) && i < args.length - 1) {
poolSize = Integer.parseInt(args[++i]); connectionPoolSize = Integer.parseInt(args[++i]);
} }
if ("-tableSize".equalsIgnoreCase(args[i]) && i < args.length - 1) { if ("-tableSize".equalsIgnoreCase(args[i]) && i < args.length - 1) {
tableSize = Integer.parseInt(args[++i]); tableSize = Integer.parseInt(args[++i]);
...@@ -49,14 +50,18 @@ public class ConnectionPoolDemo { ...@@ -49,14 +50,18 @@ public class ConnectionPoolDemo {
if ("-poolType".equalsIgnoreCase(args[i]) && i < args.length - 1) { if ("-poolType".equalsIgnoreCase(args[i]) && i < args.length - 1) {
poolType = args[++i]; poolType = args[++i];
} }
if ("-threadPoolSize".equalsIgnoreCase(args[i]) && i < args.length - 1) {
threadPoolSize = Integer.parseInt(args[++i]);
}
} }
if (host == null) { if (host == null) {
System.out.println("Usage: java -jar XXX.jar " + System.out.println("Usage: java -jar XXX.jar " +
"-host <hostname> " + "-host <hostname> " +
"-batchSize <batchSize> " + "-batchSize <batchSize> " +
"-sleep <sleep> " + "-sleep <sleep> " +
"-poolSize <poolSize> " + "-connectionPoolSize <connectionPoolSize> " +
"-tableSize <tableSize>" + "-tableSize <tableSize>" +
"-threadPoolSize <threadPoolSize>" +
"-poolType <c3p0| dbcp| druid| hikari>"); "-poolType <c3p0| dbcp| druid| hikari>");
return; return;
} }
...@@ -64,33 +69,34 @@ public class ConnectionPoolDemo { ...@@ -64,33 +69,34 @@ public class ConnectionPoolDemo {
DataSource dataSource; DataSource dataSource;
switch (poolType) { switch (poolType) {
case "c3p0": case "c3p0":
dataSource = C3p0Builder.getDataSource(host, poolSize); dataSource = C3p0Builder.getDataSource(host, connectionPoolSize);
break; break;
case "dbcp": case "dbcp":
dataSource = DbcpBuilder.getDataSource(host, poolSize); dataSource = DbcpBuilder.getDataSource(host, connectionPoolSize);
break; break;
case "druid": case "druid":
dataSource = DruidPoolBuilder.getDataSource(host, poolSize); dataSource = DruidPoolBuilder.getDataSource(host, connectionPoolSize);
break; break;
case "hikari": case "hikari":
default: default:
dataSource = HikariCpBuilder.getDataSource(host, poolSize); dataSource = HikariCpBuilder.getDataSource(host, connectionPoolSize);
poolType = "hikari"; poolType = "hikari";
} }
logger.info(">>>>>>>>>>>>>> connection pool Type: " + poolType); logger.info(">>>>>>>>>>>>>> connection pool Type: " + poolType);
init(dataSource); init(dataSource);
ExecutorService executor = Executors.newFixedThreadPool(threadCount); ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(threadPoolSize);
while (true) { while (true) {
executor.execute(new InsertTask(dataSource, dbName, tableSize, batchSize)); executor.execute(new InsertTask(dataSource, dbName, tableSize, batchSize));
logger.info("thread pool size : " + executor.getPoolSize() + ", active pool size: " + executor.getActiveCount());
if (sleep > 0) if (sleep > 0)
TimeUnit.MILLISECONDS.sleep(sleep); TimeUnit.MILLISECONDS.sleep(sleep);
} }
} }
private static void init(DataSource dataSource) { private static void init(DataSource dataSource) {
try (Connection conn = dataSource.getConnection()) { try (Connection conn = dataSource.getConnection()) {
execute(conn, "drop database if exists " + dbName + ""); execute(conn, "drop database if exists " + dbName + "");
execute(conn, "create database if not exists " + dbName + ""); execute(conn, "create database if not exists " + dbName + "");
......
...@@ -16,6 +16,13 @@ public class HikariCpBuilder { ...@@ -16,6 +16,13 @@ public class HikariCpBuilder {
config.setMaximumPoolSize(poolSize); config.setMaximumPoolSize(poolSize);
config.setMinimumIdle(poolSize); config.setMinimumIdle(poolSize);
config.setMinimumIdle(3);
config.setMaximumPoolSize(500);
config.setMaxLifetime(2000000);
config.setConnectionTimeout(30000);
config.setIdleTimeout(30000);
HikariDataSource ds = new HikariDataSource(config); HikariDataSource ds = new HikariDataSource(config);
return ds; return ds;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册