通过 Spring 配置文件注入的 JdbcTemplate-OracleDataSource 在 Oracle 11g 中插入值。事务通过由 DataSourceTransactionManager 管理的 @Transactional 完成。
问题1)如何将自动提交值设置为false?我猜默认值为true。以下是代码片段:
DataSource ds = (DataSource)ctx.getBean("dataSourceBean");
try {
ds.getConnection().setAutoCommit(false);
System.out.println("Autocommit " +ds.getConnection().getAutoCommit());
} catch (SQLException e) {
e.printStackTrace();
}
声明只是println
给予。true
还在 DAO 类的 insert 方法中打印了 getAutoCommit 值。也有成真。
问题2)对于违反唯一约束,我需要回滚。在这种情况下,DuplicateKeyException
抛出包裹在 SQLIntegrityConstraintViolationException 上的异常。所以@Transactional(rollbackFor = ?.class) 必须有哪个异常?