introduce
Springboot makes it easy to use database transactions by adding the @Transactional annotation to a method. Spring provides rich functional support for transaction management. Spring transaction management comes in both programmatic and declarative forms. Programmatic transactions refer to transactions implemented by code; Declarative transactions are AOP based and decouple concrete business logic from transactional understanding. Declarative transaction management keeps the business code logic untainted, so declarative transactions are used more often in practice.
Use the Transactional annotation
The Transactional annotation is very simple to use, simply identifying the Transactional annotation on the corresponding method.
A simple example demonstrates the use of the Transaction annotation
@Transactional
@Override
public void addUser() {
User user1 = new User();
user1.setUsername("happyjava1");
user1.setPassword("123456");
userRepo.save(user1);
int i = 1 / 0;
User user2 = new User();
user2.setUsername("happyjava2");
user2.setPassword("123456");
userRepo.save(user2);
}
Copy the code
Here we save two users and throw an exception in a 1/0 fashion. Test by a test method:
@Test
public void testTx() {
userService.addUser();
}
Copy the code
If the database does not have a record of user1, the thing is working.
The execution result is an expected error.
The database also does not have the field jappyJavA1. Show that the Transactional annotation works.
The Transactional annotation configures exceptions that do not roll back through noRollbackFor
The Transactional annotation can configure not to roll back when an exception occurs. For example, I added the following attribute on the Transaction annotation to specify ArithmeticException that the Transaction does not roll back.
@Transactional(noRollbackFor = ArithmeticException.class)
Copy the code
Run the test method again.
The error is still reported, but if you look at the database, user1 is already inserted into the database.
The propagation behavior of a transaction is configured through the Propagation attribute
Propagation behavior of transactions. The default value is Propagation.REQUIRED. Additional transaction propagation behaviors can be manually specified as follows:
- Propagation.REQUIRED
If a transaction currently exists, join the transaction, if not, create a new transaction.
- Propagation.SUPPORTS
If a transaction exists, join the transaction. If no transaction currently exists, it continues in a non-transactional manner.
- Propagation.MANDATORY
If a transaction exists, join the transaction. If no transaction currently exists, an exception is thrown.
- Propagation.REQUIRES_NEW
Re-create a new transaction and suspend the current transaction if one exists.
- Propagation.NOT_SUPPORTED
Runs in a non-transactional manner, suspending the current transaction if one exists.
- Propagation.NEVER
Runs in a non-transactional manner, throwing an exception if a transaction currently exists.
- Propagation.NESTED
If not, create a new transaction; If so, other transactions are nested within the current transaction.
Configure the isolation level of a transaction through the Isolation property
The Isolation level of the transaction, which defaults to isolation.default, depends on the Isolation level of the database. You can customize the isolation level as follows:
- Isolation.DEFAULT
- Isolation.READ_UNCOMMITTED
- Isolation.READ_COMMITTED
- Isolation.REPEATABLE_READ
- Isolation.SERIALIZABLE
Transaction propagation behavior and isolation level are abstract things that will not be discussed in this article as part of the SpringBoot tutorial, but will be discussed in a later article when there is time.
The timeout attribute
Timeout period for a transaction. Default value is -1. If the time limit is set and the transaction has not completed after the time limit is exceeded, the transaction is automatically rolled back.
conclusion
This article simply shows how SpringBoot can quickly configure transaction management using the Transactional annotation. Database transactions are a complex thing, and if you want to explain them clearly, they should not be covered in an article about Springboot, which will be covered in a separate article about transactions.