java 如果未明确提交或回滚,则自动提交事务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7525605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:20:14  来源:igfitidea点击:

Auto commit transactions if not explicitly committed or rolledback

javaconnectionweblogic

提问by Rohit

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

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

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.

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

回答by Vineet Reynolds

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 DataSourceand 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).

看起来您没有使用容器管理或 Bean 管理的事务。或者,就此而言,您只是从 a 检索连接DataSource然后禁用autocommit,而没有初始建立事务上下文;这意味着您正在使用 JDBC 事务(依赖于底层数据库的事务管理器)。

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

当您使用容器或 Bean 管理的事务时,您将不再需要担心事务中使用的autocommit属性Connection,因为容器将确保在将autocommit属性返回Connection给应用程序之前将该属性设置为 false 。

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 RuntimeExceptionor an ApplicationExceptionis thrown. If you need to use Bean-managed or programmatic transactions, you will have to use the UserTransactionAPI.

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

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.

如果您使用的是像 Hibernate 这样负责建立连接的 ORM 框架,那么您应该记住是 Hibernate 负责关闭 Connection 的自动提交属性。在大多数情况下,它会关闭该属性。

If you intend to use JDBC transactions, despite the better alternative of JTA transactions, then you could attempt to set the defaultAutoCommitproperty 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:

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

<?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>
    ...

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

在管理控制台中,您可以defaultAutoCommit=false在数据源配置的属性文本区域中添加属性:

Set defaultAutocommit for JDBC Datasource in Weblogic

在 Weblogic 中为 JDBC 数据源设置 defaultAutocommit

回答by someone

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?

调用该connection.close()方法时,App Server 上的连接池中配置的连接并没有真正关闭,它们实际上是返回到连接池中,可供下一个请求对象使用。不确定 DataSource 连接池是否会跟踪返回到池的连接是否有未提交的更改并对其执行自动提交或回滚?

回答by Miserable Variable

Setting autoCommit to false is the right thing to do.

将 autoCommit 设置为 false 是正确的做法。

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.

除非明确回滚,否则我知道的所有 RDBMS 都会在最后提交事务。你看到不同的行为了吗?如果您怀疑有问题,一种选择是打开数据库服务器的日志记录,您可以在其中看到提交请求。恐怕我不知道如何在 Oracle 中做到这一点。

Logging in app server may not be useful because it too may not be issuing explicit commit

登录应用服务器可能没有用,因为它也可能不会发出显式提交