How to set autocommit value to false in Oracle Data Source connection?

kakoli picture kakoli · Apr 4, 2014 · Viewed 8.8k times · Source

Inserting values in Oracle 11g via JdbcTemplate-OracleDataSource injected via Spring config file. Transactions are done via @Transactional managed by DataSourceTransactionManager.

Question 1) How to set the autocommit value to false? Default value is true, I guess. Following is the code snippet :

    DataSource ds = (DataSource)ctx.getBean("dataSourceBean");
    try {
        ds.getConnection().setAutoCommit(false);
        System.out.println("Autocommit " +ds.getConnection().getAutoCommit());
    } catch (SQLException e) {          
        e.printStackTrace();
    }

The println statement is giving true only.

Also printed the getAutoCommit value in insert method of the DAO class. There also is coming as true.

Question2) For unique constraint violation, I need to rollback. In that case, DuplicateKeyException is thrown which is wrapped over SQLIntegrityConstraintViolationException. So @Transactional(rollbackFor = ?.class) must have which Exception?

Answer

Mahdi Esmaeili picture Mahdi Esmaeili · Apr 4, 2014

1.Store it in a variable

DataSource ds = (DataSource)ctx.getBean("dataSourceBean");
try {
    Connection con =ds.getConnection();
    con.setAutoCommit(false);
    System.out.println("Autocommit " +con.getAutoCommit());
} catch (SQLException e) {          
    e.printStackTrace();
}

When you call the ds.getConnection() again , you get another connection from pool

2.Change Weblogic DataSource configuration
Auto commit transactions if not explicitly committed or rolledback