Tomcat JDBC连接池(释放连接) [英] Tomcat JDBC connection pool (releasing connection)

查看:788
本文介绍了Tomcat JDBC连接池(释放连接)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考 Tomcat JBDC连接池,在给出的独立Java示例中,使用datasource.getConnection()获取连接很酷.但是在finally块中,它显示为con.close().

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().

问题:当我实现此功能时,很明显,从数据源获得的con最终将在每次关闭.关闭此连接池后,连接池机制是否会获取新的连接并将其添加到池中?

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?

我认为应该有一个像releaseConnection()这样的方法调用,它将使池自行决定是否关闭它或将其打开以供其他用途.

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.

我也尝试过这样做ConnectionPool aPool = datasource.createPool(); 但是我看到在aPool上没有什么类似于释放连接.

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.

来自 Tomcat JBDC连接池的代码段:

Code snippet from Tomcat JBDC connection pool:

            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) {}
            }

推荐答案

由于您在池获取的方法上调用close(),因此取决于池在此方法调用内执行的操作.它不必关闭池化的数据库连接-可以进行一些清理,然后将连接添加回池中.

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.

在池中关闭JDBC连接

这篇关于Tomcat JDBC连接池(释放连接)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆