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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16413471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:53:23  来源:igfitidea点击:

Tomcat JDBC connection pool (releasing connection)

javajdbctomcat7connection-pooling

提问by Harsh ..

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

参考Tomcat JBDC 连接池,我在那里给出的独立 java 示例中看到,使用datasource.getConnection()它来获取连接很酷。但是在 finally 块中,它说con.close().

Question: When I implement this, it seems obvious that the conI 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?

问题:当我实现这一点时,似乎很明显con我从数据源中获得的每次都会在 finally 中关闭。当它关闭时,连接池机制是否会获取一个新连接并将其添加到池中?

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.

我认为应该有一个这样的方法调用releaseConnection(),让池自己决定是关闭它还是让它打开以供其他用途。

I've also tried doing this ConnectionPool aPool = datasource.createPool();But I see there is nothing like release connection on this aPool.

我也试过这样做ConnectionPool aPool = datasource.createPool();但我看到没有什么比这个释放连接aPool

I think I'm missing something here? Appreciate your help.

我想我在这里遗漏了什么?感谢你的帮助。

Code snippet from Tomcat JBDC connection pool:

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

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

采纳答案by mschenk74

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.

由于您对池获得的方法调用 close() ,因此取决于池在此方法调用中要做什么。它不必关闭池化的数据库连接 - 它可能会进行一些清理,然后将连接添加回池中。

This is already answered in Closing JDBC Connections in Pool

这已在Closing JDBC Connections in Pool 中得到解答

回答by Harsh ..

OK, my bad, that I did not see the implementation of DataSource. It extends DataSourceProxythat internally creates a pool before returning a Connectionbased on the PoolProperties

好吧,我的错,我没有看到DataSource. 它扩展DataSourceProxy了内部创建一个池,然后Connection根据PoolProperties

I understand, its upto this DataSource to handle the connections, even though I close the conin finally, DataSource may take necessary action.

我明白,由这个 DataSource 来处理连接,即使我con最后关闭了,DataSource 可能会采取必要的行动。

Do add a comment/reply if anybody thinks otherwise.

如果有人不这么认为,请添加评论/回复。

回答by Sam Rad

That example only shows how to create and use a data source. For connection pool on Tomcat you may configure JNDI.

该示例仅显示how to create and use a data source. 对于 Tomcat 上的连接池,您可以配置JNDI.

// Sample
public static Connection getConnectionFromPool() {
    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDB");
    return ds.getConnection();
    ...

Quote from How connection pooling works in Java and JDBC:

引用自连接池如何在 Java 和 JDBC 中工作

A connection pool operates by performing the work of creating connections ahead of time, In the case of a JDBC connection pool, a pool of Connection objects is created at the time the application server (or some other server) starts. These objects are then managed by a pool manager that disperses connections as they are requested by clients and returns them to the pool when it determines the client is finished with the Connection object. A great deal of housekeeping is involved in managing these connections.

When the connection pool server starts, it creates a predetermined number of Connection objects. A client application would then perform a JNDI lookup to retrieve a reference to a DataSource object that implements the ConnectionPoolDataSource interface. The client application would not need make any special provisions to use the pooled data source; the code would be no different from code written for a nonpooled DataSource.

连接池通过提前执行创建连接的工作来运行。在JDBC 连接池的情况下,Connection 对象池是在应用程序服务器(或某些其他服务器)启动时创建的。然后,这些对象由池管理器管理,该管理器在客户端请求连接时分散连接,并在确定客户端完成 Connection 对象时将它们返回到池中。管理这些连接涉及大量的内务处理。

当连接池服务器启动时,它会创建预定数量的 Connection 对象。然后,客户端应用程序将执行 JNDI 查找以检索对实现 ConnectionPoolDataSource 接口的 DataSource 对象的引用。客户端应用程序不需要为使用汇集的数据源做任何特殊规定;该代码与为非池化数据源编写的代码没有区别。