标签:postgresql thread 多线程 线程安全 c3p0
ETL工具完成的差不多了,今天遇到一个问题,就是给C3P0配置了maxPoolSize为10,目的是想让整个应用同时获得的最大的Connection个数为10,但是在测试应用的这一部分之后,发现PostgreSQL端的链接远远超过10个。因为工具是多线程的,所以就想,是不是多线程的问题,查了一下Connection的个数,也确实是10*线程个数。于是做了一个测试:
将maxPoolSize配置为5,运行下面的程序:
ComboPooledDataSource cpds = new ComboPooledDataSource("postgres");
for (int i = 0; i < 10; i++) {
cpds.getConnection();}
ComboPooledDataSource cpds2 = new ComboPooledDataSource("postgres");
for (int i = 0; i < 10; i++) {
cpds2.getConnection();}
maxPoolSize配置为5,一共想要获取20个链接,这时查看PostgreSQL Server端的连接数为5,正式预期的结果。
在运行下面的程序:
for (int i = 0; i < 2; i++) {
new Thread(new Runnable() {
@Override
public void run() {
ComboPooledDataSource cpds = new ComboPooledDataSource("postgres");
for (int i = 0; i < 10; i++) {
try {
cpds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "Thread" + i).start();
}PostgreSQL Server端的连接数=maxPoolSize*numThread;
底层的原因大概是因为C3P0不是为多线程设计的,在底层JDBC也不是线程安全的。具体的原因,有机会在具体分析。
标签:postgresql thread 多线程 线程安全 c3p0
原文地址:http://blog.csdn.net/xichenguan/article/details/40475391