假设我的应用程序使用的是ibatis 2和spring 1,我有一个调用道方法的外部类。如果运行以下代码,会发生什么情况:
// 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(); }发布于 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操作。
https://stackoverflow.com/questions/12011479
复制相似问题