首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ibatis事务泄漏

ibatis事务泄漏
EN

Stack Overflow用户
提问于 2012-08-17 18:46:27
回答 1查看 1.5K关注 0票数 1

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

代码语言:javascript
复制
// 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. end事务会在执行startTransaction、firstOperation和secondOperation方法的同一个事务/db连接上运行吗?或者dbcp/ibatis可以从池中选择一个不同的连接?
  3. 我可以做些什么来测试并确保所有操作都使用相同的连接,并且事务正在正常工作?
  4. (通过编辑添加)-如果我把所有的逻辑都移到道中的一个方法中,会有什么变化吗?这样做会更安全吗?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-18 14:21:32

Will this code cause database connections to be leaked?

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

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操作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12011479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档