Auto commit transactions if not explicitly committ

2019-05-21 17:48发布

问题:

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.

回答1:

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

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.

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.

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:

<?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=false property in the Properties textarea of the DataSource configuration:



回答2:

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?



回答3:

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