Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
df6bc528
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
df6bc528
编写于
1月 27, 2021
作者:
Z
zyyang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
change
上级
ee236c20
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
35 addition
and
75 deletion
+35
-75
tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/ConnectionPoolDemo.java
...s/src/main/java/com/taosdata/demo/ConnectionPoolDemo.java
+31
-40
tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/common/InsertTask.java
...ls/src/main/java/com/taosdata/demo/common/InsertTask.java
+4
-35
未找到文件。
tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/ConnectionPoolDemo.java
浏览文件 @
df6bc528
package
com.taosdata.demo
;
import
com.taosdata.demo.common.InsertTask
;
import
com.taosdata.demo.pool.C3p0Builder
;
import
com.taosdata.demo.pool.DbcpBuilder
;
import
com.taosdata.demo.pool.DruidPoolBuilder
;
...
...
@@ -10,12 +11,16 @@ 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
final
String
dbName
=
"pool_test"
;
private
static
long
totalSize
=
1_000_000
l
;
private
static
int
batchSize
=
10
;
private
static
int
sleep
=
1000
;
private
static
int
poolSize
=
50
;
...
...
@@ -24,36 +29,18 @@ public class ConnectionPoolDemo {
private
static
String
poolType
=
"hikari"
;
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
public
static
void
main
(
String
[]
args
)
{
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
(
"-poolType"
.
equalsIgnoreCase
(
args
[
i
])
&&
i
<
args
.
length
-
1
)
{
poolType
=
args
[++
i
];
}
}
if
(
host
==
null
)
{
System
.
out
.
println
(
"Usage: java -jar XXX.jar "
+
"-host <hostname> "
+
"-batchSize <batchSize> "
+
"-sleep <sleep> "
+
"-poolSize <poolSize> "
+
"-tableSize <tableSize>"
+
"-poolType <c3p0| dbcp| druid| hikari>"
);
System
.
out
.
println
(
"Usage: java -jar XXX.jar -host <hostname> -poolType <c3p0| dbcp| druid| hikari>"
);
return
;
}
...
...
@@ -75,30 +62,34 @@ public class ConnectionPoolDemo {
}
logger
.
info
(
">>>>>>>>>>>>>> connection pool Type: "
+
poolType
);
init
(
dataSource
);
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
();
}
// ExecutorService executor = Executors.newFixedThreadPool(threadCount);
// while (true) {
// executor.execute(new InsertTask(dataSource, dbName, tableSize, batchSize));
// if (sleep > 0)
// TimeUnit.MILLISECONDS.sleep(sleep);
// 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();
// }
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
threadCount
);
for
(
long
i
=
0
;
i
<
totalSize
/
batchSize
;
i
++)
{
executor
.
execute
(
new
InsertTask
(
dataSource
,
dbName
,
tableSize
,
batchSize
));
// sleep few seconds
try
{
TimeUnit
.
MILLISECONDS
.
sleep
(
sleep
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
executor
.
shutdown
();
}
private
static
void
init
(
DataSource
dataSource
)
{
...
...
tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/common/InsertTask.java
浏览文件 @
df6bc528
...
...
@@ -26,52 +26,21 @@ public class InsertTask implements Runnable {
@Override
public
void
run
()
{
Connection
conn
=
null
;
Statement
stmt
=
null
;
int
affectedRows
=
0
;
long
start
=
System
.
currentTimeMillis
();
try
{
conn
=
ds
.
getConnection
();
stmt
=
conn
.
createStatement
();
try
(
Connection
conn
=
ds
.
getConnection
();
Statement
stmt
=
conn
.
createStatement
())
{
for
(
int
tb_index
=
1
;
tb_index
<=
tableSize
;
tb_index
++)
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
"insert into "
);
sb
.
append
(
dbName
);
sb
.
append
(
".t_"
);
sb
.
append
(
tb_index
);
sb
.
append
(
"(ts, temperature, humidity) values "
);
sb
.
append
(
"insert into "
).
append
(
dbName
).
append
(
".t_"
).
append
(
tb_index
).
append
(
"(ts, temperature, humidity) values "
);
for
(
int
i
=
0
;
i
<
batchSize
;
i
++)
{
sb
.
append
(
"("
);
sb
.
append
(
start
+
i
);
sb
.
append
(
", "
);
sb
.
append
(
random
.
nextFloat
()
*
30
);
sb
.
append
(
", "
);
sb
.
append
(
random
.
nextInt
(
70
));
sb
.
append
(
") "
);
sb
.
append
(
"("
).
append
(
start
+
i
).
append
(
", "
).
append
(
random
.
nextFloat
()
*
30
).
append
(
", "
).
append
(
random
.
nextInt
(
70
)).
append
(
") "
);
}
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
)
{
e
.
printStackTrace
();
}
}
if
(
conn
!=
null
)
{
try
{
conn
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
logger
.
info
(
">>> affectedRows:"
+
affectedRows
+
" TimeCost:"
+
(
System
.
currentTimeMillis
()
-
start
)
+
" ms"
);
}
logger
.
info
(
">>> affectedRows:"
+
affectedRows
+
" TimeCost:"
+
(
System
.
currentTimeMillis
()
-
start
)
+
" ms"
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录