Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
慢慢CG
TDengine
提交
11f1e781
T
TDengine
项目概览
慢慢CG
/
TDengine
与 Fork 源项目一致
Fork自
taosdata / TDengine
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
11f1e781
编写于
11月 11, 2020
作者:
Z
zyyang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[TD-2052]<feature>: common java connection pools examples
上级
9bfa0c8e
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
89 addition
and
14 deletion
+89
-14
tests/examples/JDBC/connectionPools/README-cn.md
tests/examples/JDBC/connectionPools/README-cn.md
+12
-0
tests/examples/JDBC/connectionPools/pom.xml
tests/examples/JDBC/connectionPools/pom.xml
+7
-2
tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/ConnectionPoolDemo.java
...s/src/main/java/com/taosdata/demo/ConnectionPoolDemo.java
+28
-7
tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/common/InsertTask.java
...ls/src/main/java/com/taosdata/demo/common/InsertTask.java
+14
-4
tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/pool/C3p0Builder.java
...ols/src/main/java/com/taosdata/demo/pool/C3p0Builder.java
+28
-0
tests/examples/JDBC/connectionPools/src/main/resources/app.properties
...es/JDBC/connectionPools/src/main/resources/app.properties
+0
-1
未找到文件。
tests/examples/JDBC/connectionPools/README-cn.md
0 → 100644
浏览文件 @
11f1e781
这个example中,我们适配了java常见的连接池:
*
c3p0
*
dbcp
*
druid
*
HikariCP
如何运行这个例子:
```
shell script
```
tests/examples/JDBC/connectionPools/pom.xml
浏览文件 @
11f1e781
...
...
@@ -8,12 +8,11 @@
<artifactId>
connectionPools
</artifactId>
<version>
1.0-SNAPSHOT
</version>
<dependencies>
<dependency>
<groupId>
com.taosdata.jdbc
</groupId>
<artifactId>
taos-jdbcdriver
</artifactId>
<version>
2.0.1
0
</version>
<version>
2.0.1
1
</version>
</dependency>
<!-- druid -->
...
...
@@ -39,6 +38,12 @@
<artifactId>
commons-dbcp
</artifactId>
<version>
1.4
</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>
com.mchange
</groupId>
<artifactId>
c3p0
</artifactId>
<version>
0.9.5.2
</version>
</dependency>
<!-- log4j -->
<dependency>
...
...
tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/ConnectionPoolDemo.java
浏览文件 @
11f1e781
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
;
import
com.taosdata.demo.pool.HikariCpBuilder
;
...
...
@@ -17,17 +18,18 @@ 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
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"
;
private
static
String
poolType
=
"hikari"
;
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
];
...
...
@@ -44,6 +46,9 @@ public class ConnectionPoolDemo {
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 "
+
...
...
@@ -51,13 +56,29 @@ public class ConnectionPoolDemo {
"-batchSize <batchSize> "
+
"-sleep <sleep> "
+
"-poolSize <poolSize> "
+
"-tableSize <tableSize>"
);
"-tableSize <tableSize>"
+
"-poolType <c3p0| dbcp| druid| hikari>"
);
return
;
}
// DataSource dataSource = DbcpBuilder.getDataSource(host, poolSize);
// DataSource dataSource = DruidPoolBuilder.getDataSource(host, poolSize);
DataSource
dataSource
=
HikariCpBuilder
.
getDataSource
(
host
,
poolSize
);
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
);
init
(
dataSource
);
...
...
@@ -78,7 +99,7 @@ public class ConnectionPoolDemo {
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."
);
logger
.
info
(
">>>>>>>>>>>>>>>>>>>>>>>>>>>> init finished."
);
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
...
...
tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/common/InsertTask.java
浏览文件 @
11f1e781
...
...
@@ -10,7 +10,7 @@ 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
static
final
Logger
logger
=
Logger
.
getLogger
(
InsertTask
.
class
);
private
final
DataSource
ds
;
private
final
int
batchSize
;
...
...
@@ -37,14 +37,23 @@ public class InsertTask implements Runnable {
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 "
);
sb
.
append
(
"insert into "
);
sb
.
append
(
dbName
);
sb
.
append
(
".t_"
);
sb
.
append
(
tb_index
);
sb
.
append
(
"(ts, temperature, humidity) values "
);
for
(
int
i
=
0
;
i
<
batchSize
;
i
++)
{
sb
.
append
(
"("
+
(
start
+
i
)
+
", "
+
(
random
.
nextFloat
()
*
30
)
+
", "
+
(
random
.
nextInt
(
70
))
+
") "
);
sb
.
append
(
"("
);
sb
.
append
(
start
+
i
);
sb
.
append
(
", "
);
sb
.
append
(
random
.
nextFloat
()
*
30
);
sb
.
append
(
", "
);
sb
.
append
(
random
.
nextInt
(
70
));
sb
.
append
(
") "
);
}
logger
.
info
(
"SQL >>> "
+
sb
.
toString
());
affectedRows
+=
stmt
.
executeUpdate
(
sb
.
toString
());
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
finally
{
...
...
@@ -52,6 +61,7 @@ public class InsertTask implements Runnable {
try
{
stmt
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
if
(
conn
!=
null
)
{
...
...
tests/examples/JDBC/connectionPools/src/main/java/com/taosdata/demo/pool/C3p0Builder.java
0 → 100644
浏览文件 @
11f1e781
package
com.taosdata.demo.pool
;
import
com.mchange.v2.c3p0.ComboPooledDataSource
;
import
org.apache.commons.dbcp.BasicDataSource
;
import
javax.sql.DataSource
;
import
java.beans.PropertyVetoException
;
public
class
C3p0Builder
{
public
static
DataSource
getDataSource
(
String
host
,
int
poolSize
)
{
ComboPooledDataSource
ds
=
new
ComboPooledDataSource
();
try
{
ds
.
setDriverClass
(
"com.taosdata.jdbc.TSDBDriver"
);
}
catch
(
PropertyVetoException
e
)
{
e
.
printStackTrace
();
}
ds
.
setJdbcUrl
(
"jdbc:TAOS://"
+
host
+
":6030"
);
ds
.
setUser
(
"root"
);
ds
.
setPassword
(
"taosdata"
);
ds
.
setMinPoolSize
(
poolSize
);
ds
.
setMaxPoolSize
(
poolSize
);
ds
.
setAcquireIncrement
(
5
);
return
ds
;
}
}
tests/examples/JDBC/connectionPools/src/main/resources/app.properties
已删除
100644 → 0
浏览文件 @
9bfa0c8e
app.insertBatchSize
=
50
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录