In applications, transaction management is a very important concept. A transaction is a sequence of operations that either all succeed or all fail. In Spring Boot, transaction annotations can be used to manage transactions. When using transaction annotations, a very important concept is transaction propagation behavior.
What is Transaction Propagation Behavior
Transaction propagation behavior refers to what transaction behavior occurs when one method calls another method. In Spring Boot, transaction propagation behavior is managed by the Spring framework. The Spring framework uses some rules to decide how to handle transactions in different situations.
Transaction Annotations in Spring Boot
Spring Boot provides an @Transactional
annotation to control transactions. This annotation can be placed on the method or on the class. If placed on a class, all methods will inherit this annotation.
@Transactional
Annotations have some very important properties, including propagation
and isolation
. propagation
Attributes are used to specify transaction propagation behavior, and isolation
attributes are used to specify the isolation level of transactions.
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public void doSomething() {
// ...
}
In the above code, propagation
the attribute specifies the behavior of transaction propagation Propagation.REQUIRED
, which means that if there is already a transaction, then use this transaction; if there is no current transaction, then create a new transaction. isolation
The attribute specifies the isolation level of the transaction Isolation.READ_COMMITTED
, which means reading committed data.
Transaction Principles in Spring Boot
In Spring Boot, transactions are managed by the Spring framework. The Spring framework uses AOP (aspect-oriented programming) technology to implement transaction management. Specifically, the Spring framework implements transaction management through a proxy mechanism.
When a method is @Transactional
annotated, Spring creates a proxy object for the method. When this method is called, it actually calls the method of the proxy object. The proxy object will start a transaction before the method is executed, and decide whether to commit the transaction or roll back the transaction according to the execution result after the method is executed.
The Spring framework uses an TransactionInterceptor
interceptor to implement transaction management. This interceptor will intercept before and after method execution, and decide whether to open a new transaction or use an existing transaction according to the propagation behavior and abnormal conditions of the transaction, and decide whether to commit the transaction or return it according to the execution result after the method is executed. Get out of business.
Transaction usage in Spring Boot
In Spring Boot, we can use @Transactional
annotations to control transactions. When using @Transactional
annotations, you need to pay attention to the following points:
@Transactional
Annotations can only be placed on public methods.- Method must be non-static.
@Transactional
Transactions will not work if the annotated method is called in the same class . This is because Spring implements transaction management through proxies, and proxies can only take effect when a method is called.- If multiple annotated methods exist in a class at the same time
@Transactional
, then these methods will share the same transaction. - When using
@Transactional
annotations, be sure to ensure that the annotations are configured correctly, otherwise the transaction may not be rolled back or the rollback may be incomplete.
Here is an @Transactional
example using annotations:
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
@Transactional(rollbackFor = Exception.class)
public void addUser(User user) {
userRepository.save(user);
// ...
## 代码解读
在上面的代码中,我们定义了一个`UserService`类,这个类使用了`@Transactional`注解来控制事务。在`addUser`方法中,我们首先调用了`userRepository.save(user)`方法来保存用户信息。由于这个方法上使用了`@Transactional`注解,因此在调用`userRepository.save(user)`方法之前,Spring框架会开启一个事务。如果在`userRepository.save(user)`方法执行期间发生了异常,那么这个事务将会被回滚。
`@Transactional`注解还有一些其他的属性,包括`readOnly`、`timeout`和`noRollbackFor`等。这些属性可以帮助我们更好地控制事务的行为。例如,`readOnly`属性可以指定事务是否为只读事务;`timeout`属性可以指定事务超时时间;`noRollbackFor`属性可以指定哪些异常不应该回滚事务。
```java
@Transactional(readOnly = true, timeout = 30, noRollbackFor = IllegalArgumentException.class)
public void doSomething() {
// ...
}
In the above code, we have used @Transactional
the annotation's readOnly
, timeout
and noRollbackFor
attributes. This method is specified as a read-only transaction with a timeout of 30 seconds. If an exception occurs during method execution IllegalArgumentException
, the transaction will not be rolled back.
Summarize
In Spring Boot, transaction management is a very important concept. We can use transaction annotations to control the behavior of transactions. When using transaction annotations, we need to pay attention to transaction propagation behavior, transaction isolation level and other configuration properties. The Spring framework uses AOP technology to implement transaction management, specifically, through a proxy mechanism. When using transaction annotations, we need to ensure that the annotations are configured correctly, otherwise the transaction may not be rolled back or the rollback may be incomplete.