0%

Transaction

Usage

Adding the @Transactional annotation at the class level or method level.

1
2
3
4
5
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class PayServiceImpl implements IPayService {
// 省略方法...
}

Implementation

Spring will create a Proxy class by AOP when detects the Annotated class/method runs, it will:

  1. start the transaction
  2. run the business logic
  3. commit the transaction or rollback

Exception handling

If RunTimeException thrown, the Proxy will roll back the transaction, otherwise(no exception thrown or no-RunTimeException thrown), the Proxy will commit the transaction.
But you can also rollback the transaction by rollbackFor property, for example:

1
@Transactional(propagation= Propagation.REQUIRED, rollbackFor= PayException.class)