如果未显式提交或回滚,则自动提交事务 [英] Auto commit transactions if not explicitly committed or rolledback

查看:320
本文介绍了如果未显式提交或回滚,则自动提交事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用Weblogic服务器,并且在连接到Oracle 10g时始终将autoCommit设置为'false'。

we use Weblogic server and always set autoCommit to 'false' when getting Connection to Oracle 10g.

我想知道Weblogic中是否有一个设置,如果没有从应用程序代码中显式调用Commit或Rollback,它将自动提交事务。我听说过类似的设置存在于Websphere。

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.

推荐答案

看起来你不使用容器管理或Bean管理的事务。或者,您只需从 DataSource 检索连接,然后禁用 autocommit ,而无需初始建立事务上下文;这意味着您正在使用JDBC事务(依赖于基础数据库的事务管理器)。

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 disabling autocommit, 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).

当您使用Container或Bean托管事务时,您将不再有担心事务中使用的连接 autocommit 属性,因为容器将确保 autocommit 属性设置为false,然后将连接返回到应用程序。

When you use Container or Bean managed transactions, you will no longer have to worry about the autocommit property of a Connection used in a transaction, as the container will ensure that the autocommit property is set to false, before returning the Connection to the application.

如果您需要使用容器管理的事务,您需要使用EJB。与EJB关联的任何事务将自动提交,除非抛出 RuntimeException ApplicationException
如果您需要使用Bean管理或程序化事务,则必须使用 UserTransaction API。

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 an ApplicationException is thrown. If you need to use Bean-managed or programmatic transactions, you will have to use the UserTransaction API.

如果您使用的是ORM框架像Hibernate负责建立连接,那么你应该记住,它是Hibernate负责关闭connection的autocommit属性。在大多数情况下,它会关闭该属性。

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.

如果您打算使用JDBC事务,尽管有更好的替代JTA事务,然后您可以尝试设置 defaultAutoCommit 驱动程序的属性,从管理控制台或在数据源的JDBC配置文件中。 JDBC配置文件的代码段如下所示:

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:

<?xml version='1.0' encoding='UTF-8'?>
<jdbc-data-source xmlns="http://xmlns.oracle.com/weblogic/jdbc-data-source" xmlns:sec="http://xmlns.oracle.com/weblogic/security" xmlns:wls="http://xmlns.oracle.com/weblogic/security/wls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/jdbc-data-source http://xmlns.oracle.com/weblogic/jdbc-data-source/1.0/jdbc-data-source.xsd">
  <name>JDBC Data Source-Oracle</name>
  <jdbc-driver-params>
    <url>jdbc:oracle:thin:@localhost:1521:oracle</url>
    <driver-name>oracle.jdbc.OracleDriver</driver-name>
    <properties>
      <property>
        <name>user</name>
        <value>app</value>
      </property>
      <!-- Disable autocommit for connections-->
      <property>
        <name>defaultAutoCommit</name>
        <value>false</value>
      </property>
    </properties>
    ...

在管理控制台中,您可以添加 defaultAutoCommit = false 属性。

In the Administration Console, you may add the defaultAutoCommit=false property in the Properties textarea of the DataSource configuration:

这篇关于如果未显式提交或回滚,则自动提交事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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