连接关闭时活动事务的行为? [英] Behaviour of active transaction on connection close?

查看:103
本文介绍了连接关闭时活动事务的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果调用close方法并且存在活动事务,那么活动事务会发生什么?它们会被提交还是回滚?

If the close method is called and there is an active transaction, what will happen to active transactions? Will they be commited or rolled back?

推荐答案

添加到另一个答案,我测试了Oracle和SQL Server上的行为,我正在使用的数据库。

Adding to the other answer, I tested the behavior on Oracle and SQL Server, the databases I'm currently working with.

MSSQL 回滚交易。这是你直觉所期望的。

MSSQL rolls back the transaction. This is what you'd intuitively expect.

另一方面是Oracle,提交交易。这在他们的 JDBC指南中有记录:

Oracle on the other side, commits the transaction. This is documented in their JDBC Guide:

如果禁用自动提交模式并关闭连接而未显式提交或回滚上次更改,则运行隐式COMMIT操作。

当然,JDBC规范让你可以自由选择,但我个人认为隐含地提交事务是一个糟糕的设计选择。作为一个参数,考虑一个线程的用例,该线程忙于处理长事务并且不响应关闭请求。当应用程序最终关闭连接池时,这将关闭连接,提交未完成的事务!

Sure, the JDBC spec gives you freedom to go either way, but I personally think that implicitly committing the transaction is a poor design choice. As an argument, consider the use case of a thread which is busy working on a long-ish transaction and is not responsive to a shutdown request. When the application eventually closes the connection pool, this will in turn close the connection, committing the incomplete transaction!

这个故事的寓意是连接池实现必须始终调用在手动提交模式下关闭连接之前的rollback()。但是,实现连接池时不会想到这一点。例如,请参阅来自DBCP的PooledConnectionImpl

The moral of this story is that connection pool implementations must always call rollback() before closing a connection in manual commit mode. However this is not something that just comes to mind when implementing a connection pool. As an example, see PooledConnectionImpl from DBCP

这篇关于连接关闭时活动事务的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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