java 释放连接池连接,Tomcat JDBC连接池(释放连接)

Referring to Tomcat JBDC connection pool, I see in the standalone java example given there, one gets the connection using datasource.getConnection()which is cool. But in the finally block, it says con.close().

Question: When I implement this, it seems obvious that the con I get from datasource will be closed every time in the finally. When this is closed, will the connection pooling mechanism acquire a new connection and adds it to the pool?

I presume there should be a method call like releaseConnection() that will let the pool take its own decision whether to close it or let it be open for some other use.

I've also tried doing this ConnectionPool aPool = datasource.createPool();

But I see there is nothing like release connection on this aPool.

I think I'm missing something here?

Appreciate your help.

DataSource datasource = new DataSource();

datasource.setPoolProperties(p);

Connection con = null;

try {

con = datasource.getConnection();

Statement st = con.createStatement();

ResultSet rs = st.executeQuery("select * from user");

int cnt = 1;

while (rs.next()) {

System.out.println((cnt++)+". Host:" +rs.getString("Host")+

" User:"+rs.getString("User")+" Password:"+rs.getString("Password"));

}

rs.close();

st.close();

} finally {

if (con!=null) try {con.close();}catch (Exception ignore) {}

}

解决方案

Since you call the close() on a method obtained by the pool it is up to the pool what to do inside this method call. It does not neccessarily have to close the pooled database connection - it may do some cleanup and then add the connetion back to the pool.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值