简体   繁体   English

JDBC连接如何实现?

[英]How are JDBC connections implemented?

I have a project in which I used HikariCP for JDBC connection pooling. 我有一个项目,在其中使用HikariCP进行JDBC连接池。 And HikariCP works just great for my needs. 而且HikariCP可以很好地满足我的需求。 It also logs the stats of the pool like below. 它还如下记录池的统计信息。

2014-12-03 10:16:08 DEBUG HikariPool:559 - Before cleanup pool stats loginPool (total=8, inUse=0, avail=8, waiting=0)
  2014-12-03 10:16:08 DEBUG HikariPool:559 - After cleanup pool stats loginPool (total=7, inUse=1, avail=7, waiting=0)

Just for experimental purposes I closed all the MySQL connections for the configured database using MySQL Workbench . 出于实验目的,我使用MySQL Workbench关闭了已配置数据库的所有MySQL连接。 But, still I see HikariCP logging the stats like before though there are no actual connections to the database. 但是,尽管没有到数据库的实际连接,我仍然看到HikariCP像以前一样记录了统计信息。 When there was a request for connection it immediately established the connections(initial 8), so everything works great. 当有连接请求时,它立即建立了连接(初始8),因此一切正常。

So, my question is how does these connections are managed or implemented? 因此,我的问题是如何管理或实现这些连接? I think the reason why HikariCP logs stats, as if there were connections, is because it has valid in memory references to connections, which are actually non existent(with database). 我认为HikariCP记录统计数据(好像有连接)的原因是因为它在内存中具有对连接的有效引用,而这些连接实际上是不存在的(使用数据库)。

Is my understanding correct? 我的理解正确吗?

The connection pool created 8 connections at startup. 连接池在启动时创建了8个连接。 You say you disconnected them using the workbench. 您说您使用工作台断开了它们的连接。 Most connection pools won't know the connection is disconnected until it gets used. 大多数连接池在使用之前都不知道连接已断开。

Your assumption is correct. 您的假设是正确的。 You manually killed the connections but the pool has a handle to 8 sockets which it assumes are connected. 您手动终止了连接,但是该池有一个假定已连接的8个套接字的句柄。 Given time your connection pool may have checked the connections for validity and attempted to reconnect them. 给定时间,您的连接池可能已经检查了连接的有效性,并试图重新连接它们。 I can't speak for HikariCP but this is what modern connections pools do. 我不能代表HikariCP,但这就是现代连接池的作用。

When you close the connections using MySQL Workbench, you are closing them on the server end. 当使用MySQL Workbench关闭连接时,就是在服务器端关闭它们。 On the JDBC (client) side, the previously established connections will remain in existence until the client code attempts to use them. 在JDBC(客户端)端,以前建立的连接将一直存在,直到客户端代码尝试使用它们为止。 At that point, they will be found to be "broken"; 到那时,它们会被发现“损坏”; ie the client will get exceptions when it tries to use them. 即客户端尝试使用它们时将获得异常。

The client-side JDBC Connection objects only get closed or recycled when they are returned to the connection pool by your Java application code. 客户端JDBC连接对象仅在Java应用程序代码将它们返回到连接池时才关闭或回收。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM