【问题标题】:ibatis transaction leakageibatis交易泄露
【发布时间】:2012-08-17 18:46:27
【问题描述】:

假设我有一个使用 ibatis 2 和 spring 1 的应用程序。我有一个调用 dao 方法的外部类。如果运行以下代码会发生什么:

// external class

public void doSomething() {
    try {
        daoLayer.startTransaction();
        daoLayer.firstOperation();
        daoLayer.secondOperation();
    } finally {
        daoLayer.endTransaction();
    }
}

// dao layer which extends from org.springframework.orm.ibatis.support.SqlMapClientDaoSupport.SqlMapClientDaoSupport

public void startTransaction() { sqlMap.startTransaction(); }
public void firstOperation() { sqlMap.update("someQuery"); }
public void secondOperation() { sqlMap.update("someOtherQuery"); }
public void endTransaction() { sqlMap.endTransaction(); }
  1. 此代码会导致数据库连接泄露吗?
  2. 结束事务是否会在执行 startTransaction、firstOperation 和 secondOperation 方法的同一事务/数据库连接上运行?或者 dbcp/ibatis 可能会从池中选择不同的连接?
  3. 我可以做些什么来测试并确保所有操作都使用相同的连接并且事务正常工作?
  4. (通过编辑添加)- 如果我将所有逻辑都移到 dao 中的单个方法中,会有什么变化吗?这样交易会更安全吗?

【问题讨论】:

    标签: spring transactions ibatis apache-commons-dbcp


    【解决方案1】:

    Will this code cause database connections to be leaked?

    没有。但是要提交数据,您需要在daoLayer.secondOperation() 之后包含sqlMap.commitTransaction()。

    Will end transaction be run on the same transaction/db connection which executed the startTransaction, firstOperation, and secondOperation methods? Or might dbcp/ibatis pick a different connection out of the pool?

    是的 .. 如果正在打开一个事务,您需要关闭它,否则数据库服务器不会关闭该事务,它将根据服务器设置过期。

    What could I do to test and make sure that the same connection is used for all the operations and that the transaction is working correctly?

    在 java 中:检查日志以获取连接 ID。您还可以检查数据库服务器以获取分配的连接 ID。

    Would anything change if I moved all my logic into a single method in the dao? Would that be more transaction safe?

    不管你在单个事务中是否有单个或多个 DAO 操作。

    【讨论】:

      猜你喜欢
      • 2014-10-25
      • 2012-06-02
      • 1970-01-01
      • 2015-12-17
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      相关资源
      最近更新 更多