Isautautocommit (true) is needed after conn.commit ()

Received db ( conn) connection from pool.

Assume autocommitTRUE for this connection.

Now conn.setautocommit(false)installed;

after a few updates to the instructions and finally conn.commit()/conn.rollback().

Now I need to explicitly do the code setautocommit(true)to return to the previous connection state?

OR commit()\rollback()will install setautocommit(true)essentially?

+5
source share
2 answers

It depends on where you got this connection. If you created the connection yourself, there is no need to restore the automatic commit state.

, , , , , .

commit() auto commit. , JDBC commit() . commit() , , ( , rollback() , ).

[.]. , . dbcp , , , c3p0 , . , .

, , false , , . :

public <T> T withTransaction( TxCallback<T> closure ) throws Exception {
    Connection conn = getConnection();
    try {
        boolean autoCommit = conn.getAutoCommit();
        conn.setAutoCommit(false);

        T result = closure.call(conn); // Business code

        conn.commit();
        conn.setAutoCommit(autoCommit);
    } catch( Exception e ) {
        conn.rollback();
    } finally {
        conn.close();
    }
}

, -.

+7

conn.setAutoCommit(AutoCommit); finally

0

All Articles