通过 Spring 配置文件注入(inject)的 JdbcTemplate-OracleDataSource 在 Oracle 11g 中插入值。 事务是通过 DataSourceTransactionManager 管理的 @Transactional 完成的。
问题1)如何将autocommit值设置为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)对于违反唯一约束的情况,我需要回滚。
在这种情况下,将引发包裹在 SQLIntegrityConstraintViolationException 上的 DuplicateKeyException
。
所以
@Transactional(rollbackFor = ?.class) 必须有哪个异常?
最佳答案
1.将其存储在变量中
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();
}
当你再次调用 ds.getConnection() 时,你会从池中获得另一个连接
2.更改Weblogic数据源配置
Auto commit transactions if not explicitly committed or rolledback
关于java - 如何在 Oracle 数据源连接中将自动提交值设置为 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22855484/