如果未显式提交或回滚,则自动提交事务
我们使用 Weblogic 服务器,并在连接到 Oracle 10g 时始终将 autoCommit 设置为“false”。
我想知道 Weblogic 中是否有一个设置,如果未从应用程序代码中显式调用提交或回滚,它将自动提交事务。我听说 Websphere 中也有类似的设置。
we use Weblogic server and always set autoCommit to 'false' when getting Connection to Oracle 10g.
I want to know if there is a setting in Weblogic wherein it will automatically Commit transactions if a Commit or Rollback is not explicitly called from within application code. I heard similar setting exists in Websphere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您没有使用容器管理或 Bean 管理的事务。或者,就此而言,您只是从
DataSource
检索连接,然后禁用autocommit
,而无需初始建立事务上下文;这意味着您正在使用 JDBC 事务(依赖于底层数据库的事务管理器)。当您使用容器或 Bean 管理的事务时,您将不再需要担心事务中使用的
Connection
的autocommit
属性,因为容器将确保 <在将Connection
返回到应用程序之前,将 code>autocommit 属性设置为 false。如果您需要使用容器管理的事务,则需要使用 EJB。任何与 EJB 关联的事务都将自动提交,除非抛出 RuntimeException 或 ApplicationException。
如果您需要使用 Bean 管理或编程事务,则必须使用
UserTransaction
API。如果您使用像 Hibernate 这样负责建立连接的 ORM 框架,那么您应该记住,是 Hibernate 负责关闭 Connection 的自动提交属性。在大多数情况下,它会关闭该属性。
如果您打算使用 JDBC 事务,尽管有更好的 JTA 事务替代方案,那么您可以尝试从管理控制台或数据源的 JDBC 配置文件中设置驱动程序的
defaultAutoCommit
属性。 JDBC 配置文件的片段如下所示:在管理控制台中,您可以在数据源配置的属性文本区域中添加
defaultAutoCommit=false
属性:It looks like you are not using either Container-managed or Bean-managed transactions. Or, for that matter, you are merely retrieving a connection from a
DataSource
and then disablingautocommit
, without the initial establishment a transaction context; this implies that you are using JDBC transactions (that rely on the transaction manager of the underlying database).When you use Container or Bean managed transactions, you will no longer have to worry about the
autocommit
property of aConnection
used in a transaction, as the container will ensure that theautocommit
property is set to false, before returning theConnection
to the application.If you need to use Container-managed transactions, you'll need to use EJBs. Any transaction associated with an EJB will commit automatically, unless a
RuntimeException
or anApplicationException
is thrown.If you need to use Bean-managed or programmatic transactions, you will have to use the
UserTransaction
API.If you are using an ORM framework like Hibernate that is responsible for establishing connections, then you ought to remember that it is Hibernate that is responsible for switching off the autocommit property of the Connection. and in most cases, it would switch off the property.
If you intend to use JDBC transactions, despite the better alternative of JTA transactions, then you could attempt to set the
defaultAutoCommit
property for the driver, from either Admin Console, or in the JDBC configuration file of the Datasource. The snippet of the JDBC configuration file is shown below:In the Administration Console, you may add the
defaultAutoCommit=false
property in the Properties textarea of the DataSource configuration:当您调用
connection.close()
方法时,App Server上连接池内配置的连接并没有真正关闭,它们实际上返回到连接池中,并可供应用程序使用下一个请求对象。不确定数据源连接池是否会跟踪返回到池的连接上是否有未提交的更改并对其执行自动提交或回滚?The connections configured inside of a connection pool on the App Server are not really closed when you call the
connection.close()
method, they are actually returned back to the connection pool, and can be used by the next requesting object. Not sure if the DataSource connection pools will track if there are uncommitted changes on a connection being returned to the pool and perform an auto commit or rollback on it?将 autoCommit 设置为 false 是正确的做法。
我所知道的所有 RDBMS 都会在最后提交事务,除非显式回滚。您看到不同的行为吗?如果您怀疑出现问题,一种选择是打开数据库服务器中的日志记录,您可以在其中看到提交请求。恐怕我不知道如何在 Oracle 中做到这一点。
登录应用程序服务器可能没有用,因为它也可能不会发出显式提交
Setting autoCommit to false is the right thing to do.
All RDBMS that I know of commit the transaction at the end unless explicitly rolled back. Do you see a different behavior? If you suspect something is wrong, one option is to turn on logging in the database server, where you would be able to see the commit request. I am afraid I don't know how to do it in Oracle.
Logging in app server may not be useful because it too may not be issuing explicit commit