首页 > 解决方案 > Oracle JDBC - Effect of commit() rollback() close() AutoCommit() on connection - Pending Underlying transactions

问题描述

Dao Snippet

Connection connection = null;

try{
        connection.setAutoCommit(false);
        connection = session.getConnection();  // session here is HttpSession
        Prepared Statement ...
        executeUpdate(); // Step 1
        insert();   // Step 2
        executeUpdate();   Step 3
        connection.commit();
}catch(SQLException e){
       connection.rollback();
}finally{
       connection.setAutoCommit(true);
}

After finishing above step, User clicks on logout

try {
            // Rollback all pending transactions
            connection = session.getConnection();  // session here is HttpSession
            connection.rollback();
        } catch (Exception e) {
            logger.error(e);
        } finally {
            try {
                connection.close();
            } catch (SQLException e) {
                logger.error(e);
            }
        }

I am using Java JDBC with Oracle.

Question

  1. Will Step 1/2/3 execute sequentially at database level, let's say Step 1 takes 2 minutes, Step 2 takes 1 minute and Step 3 just few seconds.

  2. Let's say above Dao method completes and underlying db is still executing updates and immediately user clicks on logout and logout code gets called which try to rollback() any pending transaction and close the DB Connection also.

Will it wait to complete the underlying transaction's or will rollback.

Will close() have any effect on uncommitted transactions

  1. Is it really required to set AutoCommit(true) in finally, i see locks in my database tables unless i call this statement. Ideally commit() or rollback() should make the DB back to its original state.

标签: oraclejdbccommitrollback

解决方案


推荐阅读