JDBC连接关闭与中止 [英] JDBC Connection close vs abort

查看:536
本文介绍了JDBC连接关闭与中止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了这个问题(我该怎么称呼java.sql.Connection :: abort?),这使我想到了另一个问题.

I asked this question (How do I call java.sql.Connection::abort?) and it led me to another question.

使用

java.sql.Connection conn = ... ;

两者之间有什么区别

conn.close();

conn.abort(...);

?

推荐答案

您可以使用Connection.close()进行正常,同步,关闭的连接.另一方面,abort方法用于突然终止可能卡住的连接.

You use Connection.close() for a normal, synchronous, close of the connection. The abort method on the other hand is for abruptly terminating a connection that may be stuck.

在大多数情况下,您将需要使用close(),但是close()有时无法及时完成,例如,如果当前连接繁忙(例如执行长时间运行的查询或更新,或者等待),它可能会阻塞锁定).

In most cases you will need to use close(), but close() can sometimes not complete in time, for example it could block if the connection is currently busy (eg executing a long running query or update, or maybe waiting for a lock).

abort方法适用于这种情况:驱动程序将立即将连接标记为已关闭(希望),该方法返回,然后驱动程序可以使用提供的Executor异步执行必要的清理工作(例如确保被卡住的语句被中止,清理其他资源等).

The abort method is for that situation: the driver will mark the connection as closed (hopefully) immediately, the method returns, and the driver can then use the provided Executor to asynchronously perform the necessary cleanup work (eg making sure the statement that is stuck gets aborted, cleaning up other resources, etc).

定义此方法时,我尚未加入JSR-221(JDBC规范)专家组,但据我所知,此方法的主要目标用户不是太多的应用程序代码,而是连接池,事务管理器和其他连接管理代码,它们可能想要强行终止使用时间过长或卡住"的连接.

I hadn't joined the JSR-221 (JDBC specification) Expert Group yet when this method was defined, but as far as I'm aware, the primary intended users for this method is not so much application code, but connection pools, transaction managers and other connection management code that may want to forcibly end connections that are in use too long or 'stuck'.

也就是说,应用程序代码也可以使用abort.它可能比close更快(取决于实现方式),但是在异步清理期间您不会收到有关问题的通知,并且可能会中止当前正在进行的操作.

That said, application code can use abort as well. It may be faster than close (depending on the implementation), but you won't get notified of problems during the asynchronous clean up, and you may abort current operations in progress.

但是请记住,abort被认为是连接的突然终止,因此它可能不如关闭而优雅,并且可能导致未指定的行为.另外,我不确定与普通close()相比,驱动程序对它的支持程度如何.

However keep in mind, an abort is considered an abrupt termination of the connection, so it may be less graceful than a close, and it could lead to unspecified behaviour. Also, I'm not sure how well it is supported in drivers compared to a normal close().

这篇关于JDBC连接关闭与中止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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