事务,Transaction
- MyBatis里默认开启了事务,在执行insert, update, delete之后,要记得commit ,形如,
try (SqlSession session = sqlSessionFactory.openSession())
{
session.insert (…)
session.update(…)
session.commit(true);
}
- 在官方文档里找到 Transaction Control Methods
void commit()
void commit(boolean force) //强制提交
void rollback()
void rollback(boolean force)
- 通常情况下,不需要调用rollback() ,官方文档描述如下:
Most of the time you won't have to call rollback(), as MyBatis will do that for you if you don't call commit
不commit(),也就自动rollback()了。
- 关于commit() 与 commit( force)
commit( true ) 确保被操作执行 ,官方文档描述如下:
By default MyBatis does not actually commit unless it detects that the database has been changed by a call to insert, update or delete. If you've somehow made changes without calling these methods, then you can pass true into the commit and rollback methods to guarantee that it will be committed.