Frage

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?

War es hilfreich?

Lösung

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

Andere Tipps

Instead doing programmatically, check your connection pool has the option to set auto commit to false when returning connection from the pool. DBCP's BasicDataSource has such option

Spring will do rollback if any RuntimeException is thrown .

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top