java - 如何在 Oracle 数据源连接中将自动提交值设置为 false?

标签 java oracle autocommit

通过 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/

相关文章:

java - 出错后如何执行RollBack?

java - 如何在android中使用另一个应用程序打开文件

java.lang.ClassCastException : org. apache.tomcat.dbcp.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper 无法转换为 oracle.jdbc.OracleConnection

java - MyBatis - jdbcTypeForNull 甲骨文

java - MySQL 查询使用公共(public)字符串方法返回字符串

sql - Oracle RegExp_Like 用于两个重复数字

java - 组合使用 MySQL 和 BoneCP 时 jdbc executeBatch 挂起

PHP 用来自 Oracle 的数据填充现有 .xlsb 文件的值

oracle - HikariCP:为 Oracle 11g 设置 maxLifetime 时应考虑哪些数据库级别超时

git - 如何为网站设置混合 (S)FTP + Git 工作流程