论坛首页 Java企业应用论坛

写了一个简单的数据库连接池,请帮忙看一下

浏览 16263 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-03-12  
javaso 写道
实现DataSource

正解。。。。
0 请登录后投票
   发表时间:2011-03-12  
wing5jface 写道
如果使用的某一个数据库连接未归还,超过一定时间,没有相关的线程代码监控?

是是!!忘了!多谢
0 请登录后投票
   发表时间:2011-03-12  
javaso 写道
实现DataSource

多谢指点!!
0 请登录后投票
   发表时间:2011-03-12  
yy59750901 写道
javaso 写道
实现DataSource

多谢指点!!

连接池,首先可以控制资源个数,以后避免创建。
用完的资源,可以再放到池中使用。

支持多个线程并发获取资源。
池中满了,并且所有资源都处于忙的状态,
某个线程获取不用资源时,应该锁住线程,重试直到超时未知。

此外,某个资源,长期空闲,应该被回收掉。
定时检查资源是否已经不可用(connection isClosed),从池中去掉。

我一时能想到的,大概就这么多吧。
0 请登录后投票
   发表时间:2011-03-12  
客户端调用close方法会发生什么现象?returnConn是什么时候调用呢?

数据库连接池的核心之一是客户端透明,当客户端调用close方法时返还Connection对象
0 请登录后投票
   发表时间:2011-03-12   最后修改:2011-03-12
支持一下 羊驼吧 大家记住啊  lz是可爱的 羊驼驼
0 请登录后投票
   发表时间:2011-03-13  
whaosoft 写道
支持一下 羊驼吧 大家记住啊  lz是可爱的 羊驼驼

我靠  你个大便男
0 请登录后投票
   发表时间:2011-03-13  
yy59750901 写道
whaosoft 写道
支持一下 羊驼吧 大家记住啊  lz是可爱的 羊驼驼

我靠  你个大便男

我刚写了个简单的连接池。
等我花点时间写点原理性的东西,一起发出来。
0 请登录后投票
   发表时间:2011-03-13  
13个并发线程竞争9个资源。

      ********      ====>   delete database.
      ********      ====>   init DataSource and create database.
main->request connection at 1299985865796
main->create new connection...list.size()=0 this.maxPoolSize=10
      ********      ====>   create table.
      ********      ====>   ********** start TEST threads.
test0->request connection at 1299985873546
test1->request connection at 1299985873546
test2->request connection at 1299985873546
test3->request connection at 1299985873546
test4->request connection at 1299985873546
test5->request connection at 1299985873546
test0->create new connection...list.size()=1 this.maxPoolSize=10
test7->request connection at 1299985873546
test6->request connection at 1299985873546
test9->request connection at 1299985873546
test8->request connection at 1299985873546
test12->request connection at 1299985873546
test11->request connection at 1299985873546
test10->request connection at 1299985873546
test10->create new connection...list.size()=2 this.maxPoolSize=10
test11->create new connection...list.size()=3 this.maxPoolSize=10
test12->create new connection...list.size()=4 this.maxPoolSize=10
test8->create new connection...list.size()=5 this.maxPoolSize=10
test9->create new connection...list.size()=6 this.maxPoolSize=10
test6->create new connection...list.size()=7 this.maxPoolSize=10
test7->create new connection...list.size()=8 this.maxPoolSize=10
test5->create new connection...list.size()=9 this.maxPoolSize=10
test2->All connectios is busy, waiting for 50ms, 10
test3->All connectios is busy, waiting for 50ms, 10
test1->All connectios is busy, waiting for 50ms, 10
test4->All connectios is busy, waiting for 50ms, 10
test2->94,1000
test1->94,1000
test2->Iterator for connection...list.size()=10 this.maxPoolSize=10
test3->94,1000
test3->Iterator for connection...list.size()=10 this.maxPoolSize=10
test4->94,1000
test1->Iterator for connection...list.size()=10 this.maxPoolSize=10
test4->Iterator for connection...list.size()=10 this.maxPoolSize=10
test4->All connectios is busy, waiting for 50ms, 10
test2->All connectios is busy, waiting for 50ms, 10
test3->All connectios is busy, waiting for 50ms, 10
test1->All connectios is busy, waiting for 50ms, 10
test4->157,1000
test1->157,1000
test1->Iterator for connection...list.size()=10 this.maxPoolSize=10
test1->All connectios is busy, waiting for 50ms, 10
test3->157,1000
test2->157,1000
test2->Iterator for connection...list.size()=10 this.maxPoolSize=10
test2->All connectios is busy, waiting for 50ms, 10
test3->Iterator for connection...list.size()=10 this.maxPoolSize=10
test3->All connectios is busy, waiting for 50ms, 10
test4->Iterator for connection...list.size()=10 this.maxPoolSize=10
test4->All connectios is busy, waiting for 50ms, 10
test2->219,1000
test4->219,1000
test3->219,1000
test1->219,1000
test1->Iterator for connection...list.size()=10 this.maxPoolSize=10
test3->Iterator for connection...list.size()=10 this.maxPoolSize=10
test4->Iterator for connection...list.size()=10 this.maxPoolSize=10
test2->Iterator for connection...list.size()=10 this.maxPoolSize=10
test4->All connectios is busy, waiting for 50ms, 10
test3->All connectios is busy, waiting for 50ms, 10
test1->All connectios is busy, waiting for 50ms, 10
test2->All connectios is busy, waiting for 50ms, 10
test1->282,1000
test1->Iterator for connection...list.size()=10 this.maxPoolSize=10
test4->282,1000
test1->All connectios is busy, waiting for 50ms, 10
test2->282,1000
test2->Iterator for connection...list.size()=10 this.maxPoolSize=10
test3->282,1000
test2->All connectios is busy, waiting for 50ms, 10
test4->Iterator for connection...list.size()=10 this.maxPoolSize=10
test4->All connectios is busy, waiting for 50ms, 10
test3->Iterator for connection...list.size()=10 this.maxPoolSize=10
test3->All connectios is busy, waiting for 50ms, 10
org.qsoft.qpool.QpoolConnection@1bc16f0 to free.
test5  ==>  done. spend time: 313
org.qsoft.qpool.QpoolConnection@18622f3 to free.
test8  ==>  done. spend time: 313
org.qsoft.qpool.QpoolConnection@92dcdb to free.
org.qsoft.qpool.QpoolConnection@5eb489 to free.
org.qsoft.qpool.QpoolConnection@160877b to free.
test12  ==>  done. spend time: 313
test6  ==>  done. spend time: 313
org.qsoft.qpool.QpoolConnection@3a1ec6 to free.
org.qsoft.qpool.QpoolConnection@1a6684f to free.
org.qsoft.qpool.QpoolConnection@1e1be92 to free.
org.qsoft.qpool.QpoolConnection@1e1be92 to free.
test0  ==>  done. spend time: 313
test9  ==>  done. spend time: 313
test11  ==>  done. spend time: 313
test10  ==>  done. spend time: 313
test7  ==>  done. spend time: 313
test1->344,1000
test1->Iterator for connection...list.size()=10 this.maxPoolSize=10
test3->344,1000
test2->344,1000
test4->344,1000
test2->Iterator for connection...list.size()=10 this.maxPoolSize=10
test3->Iterator for connection...list.size()=10 this.maxPoolSize=10
test1->get free connection:org.qsoft.qpool.QpoolConnection@1e1be92
test4->Iterator for connection...list.size()=10 this.maxPoolSize=10
test3->get free connection:org.qsoft.qpool.QpoolConnection@160877b
test4->get free connection:org.qsoft.qpool.QpoolConnection@3a1ec6
test2->get free connection:org.qsoft.qpool.QpoolConnection@5eb489
org.qsoft.qpool.QpoolConnection@3a1ec6 to free.
test4  ==>  done. spend time: 360
org.qsoft.qpool.QpoolConnection@160877b to free.
test3  ==>  done. spend time: 360
org.qsoft.qpool.QpoolConnection@5eb489 to free.
test2  ==>  done. spend time: 360
org.qsoft.qpool.QpoolConnection@1e1be92 to free.
test1  ==>  done. spend time: 360
      ********      ====>   count = 26
org.qsoft.qpool.QpoolConnection@1c7980c to free.
0 请登录后投票
   发表时间:2011-03-13  
实现DataSource接口就好了.
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics