Spring Transaction rollback
1. Problems encountered
When we have multiple database save operations in a method, the intermediate database operation errors occur. The pseudo code is as follows:
?
| 1234567 |
publicmethod() { Dao1.save(Person1); Dao1.save(Person2); Dao1.save(Person2);//假如这句发生了错误,前面的两个对象会被保存到数据库中 Dao1.save(Person2);} |
Expected situation: All database save operations are rolled back before the error occurs, without saving
Normal: The previous database operation will be executed, and all data save operations that occur after the database operation error and subsequent failures will fail. This should not be the result we want.
When this happens, we can use spring's transactions to solve this problem.
2, some basic knowledge of the anomaly
1) exception of the schema
Exception inheritance structure: Throwable for base class, error and exception inheritance Throwable,runtimeexception and IOException inheritance exception. Error and RuntimeException and their subclasses become unchecked exceptions (unchecked), and other exceptions become checked exceptions (checked).
2) Error exception
The error indicates that the program has had a very serious, unrecoverable error while running, in which case the application can only abort, such as an error in the Java Virtual machine. Error is a unchecked Exception, and the compiler does not check that the error is handled and does not catch exceptions of type error in the program. In general, you should not throw exceptions of type error in your program.
3) RuntimeException exception
Exception exceptions include runtimeexception exceptions and other non-runtimeexception exceptions.
RuntimeException is a unchecked Exception, which means that the compiler does not check whether the program handles RuntimeException and does not have to capture the runtimexception type of exception in the program. You do not have to throw the RuntimeException class in the method body declaration. When runtimeexception occurs, there is a programming error in the program, so you should find the wrong modification program instead of capturing RuntimeException.
4) Checked Exception abnormality
Checked Exception exception, which is also the most used in programming Exception, all inherited from Exception and not runtimeexception the exception is Checked Exception, In the IOException and ClassNotFoundException. The JAVA language stipulates that checked Exception must be processed, that the compiler checks for this, either declares the throw checked Exception in the method body, or captures checked Exception with a catch statement, Otherwise it cannot be compiled.
3. Example
The transaction configuration used here is as follows:
?
| 1234567 |
<!-- Jpa 事务配置 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <!-- 开启注解事务 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> |
In spring's configuration file, if the data source's Defaultautocommit is set to True, the transaction is not rolled back if the exception is caught in the method and the transaction is rolled back if the exception is not caught by itself, as in the following example
Like a record in a configuration file.
?
| 12345678910 |
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="xxx" value="xxx"/> <property name="xxx" value="xxx"/> .... <property name="defaultAutoCommit" value="true" /> </bean> |
Perhaps you will find that you have not configured this parameter, is not he will not automatically submit it? The answer is no, I am using the Com.alibaba.druid.pool.DruidDataSource as a database connection pool, the default defaultautocommit is true, you can see the following source code
So now there are two things
Scenario 1: If the exception is not manually caught in the program
?
| 12345 |
@Transactional(rollbackOn = { Exception.class}) public void test() throwsException { doDbStuff1(); doDbStuff2();//假如这个操作数据库的方法会抛出异常,现在方法doDbStuff1()对数据库的操作 会回滚。 } |
Scenario 2: If you catch an exception yourself in the program
?
| 123456789 |
@Transactional(rollbackOn = { Exception.class }) public void test() { try { doDbStuff1(); doDbStuff2();//假如这个操作数据库的方法会抛出异常,现在方法doDbStuff1()对数据库的操作 不会回滚。 } catch (Exception e) { e.printStackTrace(); } } |
Now if we need to catch the exception manually, and also want to throw an exception when you can roll back the swelling do?
Here's how it's done, manually rolling back the transaction:
?
| 12345678910 |
@Transactional(rollbackOn = { Exception.class }) public void test() { try { doDbStuff1(); doDbStuff2(); } catch (Exception e) { e.printStackTrace(); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//就是这一句了,加上之后,如果doDbStuff2()抛了异常, //doDbStuff1()是会回滚的 } } |
Thank you for reading! Thank you!
Spring Management Transaction Rollback