且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java JDBC 连接状态

更新时间:2023-11-26 11:16:10

没什么.只需执行您的查询.如果连接已终止,那么您的 JDBC 驱动程序将重新连接(如果它支持,并且您在连接字符串中启用了它——大多数不支持它),否则您将收到异常.

Nothing. Just execute your query. If the connection has died, either your JDBC driver will reconnect (if it supports it, and you enabled it in your connection string--most don't support it) or else you'll get an exception.

如果您检查连接已建立,它可能会在您实际执行查询之前失败,因此通过检查您绝对不会获得任何好处.

If you check the connection is up, it might fall over before you actually execute your query, so you gain absolutely nothing by checking.

也就是说,很多连接池在发送连接之前通过执行诸如 SELECT 1 之类的操作来验证连接.但这只不过是执行查询,因此您不妨执行业务查询.

That said, a lot of connection pools validate a connection by doing something like SELECT 1 before handing connections out. But this is nothing more than just executing a query, so you might just as well execute your business query.