문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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 .

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top