Home >Java >javaTutorial >How to use Java @Transactional to specify rollback conditions
Type | Description | |
---|---|---|
String | Optional qualification Descriptor, specifying the transaction manager used | |
enum: Propagation | Optional transaction propagation behavior settings | |
enum: Isolation | Optional transaction isolation level setting | |
boolean | Read-write or read-only transaction, default read-write | |
int (in seconds granularity) | Transaction timeout setting | |
Array of Class objects, must inherit from Throwable | Array of exception classes that cause transaction rollback | |
Array of class names, must inherit from Throwable | Array of exception class names that cause transaction rollback | |
Class object array, must inherit from Throwable | Exception class array that will not cause transaction rollback | |
Class name array, must Inherited from Throwable | Number of exception class names that will not cause transaction rollback |
when using @Transaction to process transactions. The transaction will be rolled back only when exceptions and unchecked exceptions occur. That is, when an instance of RuntimeException or its subclass is thrown, Checked exceptions thrown from the transaction method will not be marked for transaction rollback.
@Transactional(rollbackOn=Exception.class)
If the exception is try-catch , the transaction will not be rolled back. If you want the transaction to be rolled back, you must throw
@Slf4j @Service public class MemberService { @Autowired private MemberMapper memberMapper; @Transactional public Integer insert(MemberEntity memberEntity) { Integer insertResult = 0; try { insertResult = memberMapper.save(memberEntity); log.info("insertResult:{}", insertResult); int result = 1 / memberEntity.getAge(); } catch (Exception e) { log.error("errorMsg:{}", e.getMessage()); //回滚 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); } return insertResult; } }as shown in the red code , if you don't write it, the transaction will not be rolled back. Because the transaction is caught by catch, the transaction can only be rolled back manually.
The above is the detailed content of How to use Java @Transactional to specify rollback conditions. For more information, please follow other related articles on the PHP Chinese website!