Servlet中jdbc应用高级篇(得到和返回连接 )


  二、得到和返回连接 

  DBConnectionManager提供getConnection()方法和freeConnection方法,这些方法有客户程序使用。所有的方法以连接池名字所参数,并调用特定的连接池对象。 

public Connection getConnection(String name) { 

DBConnectionPool pool = (DBConnectionPool) pools.get(name); 

if (pool != null) { 

return pool.getConnection(); 



return null; 





public Connection getConnection(String name, long time) { 

DBConnectionPool pool = (DBConnectionPool) pools.get(name); 

if (pool != null) { 

return pool.getConnection(time); 



return null; 



public void freeConnection(String name, Connection con) { 

DBConnectionPool pool = (DBConnectionPool) pools.get(name); 

if (pool != null) { 

pool.freeConnection(con); 





  三、关闭 

  最后,由一个release()方法,用来完好地关闭连接池。每个DBConnectionManager客户必须调用getInstance()方法引用。有一个计数器跟踪客户的数量。方法release()在客户关闭时调用,技术器减1。当最后一个客户释放,DBConnectionManager关闭所有的连接池。 

List 11-14 

public synchronized void release() { 

// Wait until called by the last client 

if (--clients != 0) { 

return; 





Enumeration allPools = pools.elements(); 

while (allPools.hasMoreElements()) { 

DBConnectionPool pool = (DBConnectionPool) allPools.nextElement(); 

pool.release(); 



Enumeration allDrivers = drivers.elements(); 

while (allDrivers.hasMoreElements()) { 

Driver driver = (Driver) allDrivers.nextElement(); 

try { 

DriverManager.deregisterDriver(driver); 

log("Deregistered JDBC driver " + driver.getClass().getName()); 



catch (SQLException e) { 

log(e, "Can not deregister JDBC driver: " + 

driver.getClass().getName()); 







当所有连接池关闭,所有jdbc驱动程序也被注销
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值