Skip to content

Transaction

王爵nice edited this page Apr 11, 2018 · 2 revisions

Anima puts the transaction operation in a lambda expression, so that you don't care how it is done. By default, the transaction will automatically roll back when a certain add/delete operation encounters RuntimeException. Let's see one example.

Anima.atomic(() -> {
    int a = 1 / 0;
    new User("apple", 666).save();
}).catchException(e -> {
    e.printStackTrace();
});

In this example we do a divide by 0 operation, which produces an ArithmeticException which is a runtime exception, so the following save operation will not be executed and the transaction will be rolled back. You can get this exception with the catchException post operation and do some custom actions.

Modify the transaction rollback exception type

Using Anima.open method returns the Anima objects have a rollbackException method, you can call it specified rollback Exception type.

Example Code