-
Notifications
You must be signed in to change notification settings - Fork 47
事务操作
王爵nice edited this page Apr 11, 2018
·
3 revisions
Anima 将事务操作放在一个 lambda
表达式中,让你不用关心它是怎么做的,默认情况下当某个增删改的操作遇到 RuntimeException
的时候事务会自动回滚,我们来看一个例子。
Anima.atomic(() -> {
int a = 1 / 0;
new User("apple", 666).save();
}).catchException(e -> {
e.printStackTrace();
});
这个例子中我们做了一个除 0 操作,会产生一个 ArithmeticException
它是一个运行时异常,所以下面的 save
操作不会执行,该事务会回滚。
你可以通过 catchException
的后置操作获取到该异常,然后做一些自定义的操作。
使用 Anima.open
方法返回的 Anima
对象中有一个 rollbackException
方法,你可以调用它指定回滚的异常类型。
示例代码
Contributing
Documentation
- Getting started
- Create Model
- Query DB
- Save to DB
- Updates and Deletes
- Transaction
- Integration with Spring
- Advanced Usage
Other resources
中文文档