分类: Java
2013-04-18 16:20:42
Atomicity: 原子性
Consistency: 一致性
Isolation: 隔离性
Durability: 耐久性
iBatis中的事务分为:
Automatic Transaction
Local Transaction
Global Transaction
Custom Transaction
Automatic Transaction:
默认情况下iBatis认为每个statement都是一个事务
比如
public void runStatementsUsingAutomaticTransactions()
{
SqlMapClient sqlMapClient =
SqlMapClientConfig.getSqlMapClient();
Person p = (Person)
sqlMapClient.queryForObject("getPerson", // 第一个事务
new Integer(9));
p.setLastName("Smith");
sqlMapClient.update("updatePerson", p); //第二个事务
}
Local Transaction:
如果你想将几个statement作为一个事务来处理,可以用
startTransaction,endTransaction来划分事务
比如
public void runStatementsUsingLocalTransactions() {
SqlMapClient sqlMapClient =
SqlMapClientConfig.getSqlMapClient();
try {
sqlMapClient.startTransaction(); //事务开始
Person p =
(Person)sqlMapClient.queryForObject
("getPerson", new Integer(9));
p.setLastName("Smith");
sqlMapClient.update("updatePerson", p);
Department d =
(Department)sqlMapClient.queryForObject
("getDept", new Integer(3));
p.setDepartment(d);
sqlMapClient.update("updatePersonDept", p);
sqlMapClient.commitTransaction(); //提交事务,从开始到这里是一个事务
} finally {
sqlMapClient.endTransaction(); //事务结束
}
}