In Spring, transactions can be divided into declarative transactions and programmatic transactions. So let’s talk about these two aspects from these two aspects.
Declarative transaction rollback:
1. Basic annotation configuration
Automatic rollback is realized through @Transactional annotation, and the default is effective for RuntimeException and Error
@Transactional public void transferMoney(Account from, Account to, double amount) { (amount); // Deduct money (amount); // deposit // If a RuntimeException is thrown here, the transaction will automatically roll back}
2. Specify the rollback exception type
The rollback range can be extended to the detected exception by using the rollbackFor parameter annotated by @Transactional
@Transactional(rollbackFor = {, }) public void processOrder(Order order) throws CustomException { // Roll back when SQLException or BusinessException is thrown}
You need to display the type of exception declared to be checked, otherwise it will not roll
3. Do not roll back special scenarios
Use the noRollbackFor parameter to exclude specific exceptions
@Transactional(noRollbackFor = {}) public void validateData(Data data) { // ValidationException will not trigger rollback}
Programming transaction rollback:
1. Use TransactionTemplate to implement programming transactions
@Autowired private TransactionTemplate transactionTemplate; public void batchProcess() { (status -> { try { // Business operations... if (errorCondition) (); return result; } catch (Exception e) { (); throw e; } }); }
2. Use PlatformTransactionManager to implement programming transactions
Directly operate the transaction manager to achieve full control
@Autowired private PlatformTransactionManager transactionManager; public void complexOperation() { TransactionStatus status = (new DefaultTransactionDefinition()); try { // Business operations... (status); } catch (Exception e) { (status); throw new ServiceException("Operation failed", e); } }
Exception rollbacks related to propagation behavior of nested transactions:
1. Nested transactions (PROPAGATION_NESTED)
The child transaction is the save point of the parent transaction and can be partially rolled back.
@Transactional(propagation = ) public void parentMethod() { // Main business logic childMethod(); // Nested subtransactions} @Transactional(propagation = ) public void childMethod() { // Sub-transaction operation, only rollback operation failed}
2. Transmission behavior control
Control transaction boundaries through propagation policies:
REQUIRES_NEW: Always create new transactions
MANDATORY: Parent transaction must exist
NOT_SUPPORTED: Non-transactional execution
Configure global rollback rules through xml:
<tx:advice > <tx:attributes> <tx:method name="save*" rollback-for=""/> <tx:method name="update*" no-rollback-for="BusinessWarning"/> </tx:attributes> </tx:advice>
This is the end of this article about the implementation of transaction rollback through the Spring level. For more related Spring transaction rollback content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!