【问题标题】:How to set autocommit value to false in Oracle Data Source connection?如何在 Oracle 数据源连接中将自动提交值设置为 false?
【发布时间】:2014-04-04 06:46:00
【问题描述】:

通过 Spring 配置文件注入的 JdbcTemplate-OracleDataSource 在 Oracle 11g 中插入值。 事务通过由 DataSourceTransactionManager 管理的 @Transactional 完成。

问题1)如何将自动提交值设置为false?我猜默认值为true。 以下是代码 sn -p :

    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) 必须有哪个异常?

【问题讨论】:

  • 一次只问一个问题。
  • 连接(或其代理)可能会返回 true,因为提交是自动处理的(由容器)而不是由您处理。

标签: java oracle autocommit


【解决方案1】:

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 DataSource配置
Auto commit transactions if not explicitly committed or rolledback

【讨论】:

  • @Mahdi 是的,这就是我刚刚发现的。有没有办法在 spring 配置本身中设置自动提交?像这样使用:
  • false
  • 给出错误。尝试使用“autoCommit”,同样的错误 org.springframework.beans.NotWritablePropertyException:bean 类 [oracle.jdbc.pool.OracleDataSource] 的无效属性“defaultAutoCommit”
【解决方案2】:

改为以编程方式进行,检查您的连接池是否可以在从池返回连接时将自动提交设置为 false。 DBCP 的BasicDataSource 有这样的选项

如果抛出任何 RuntimeException,Spring 将进行回滚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-30
    • 2016-04-04
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    相关资源
    最近更新 更多