Spring things, inject things in the service layer, perform two insert operations, do you need to throw an exception yourself in the service method and then specify rollback? Or will an insert be rolled back without throwing an exception if it fails?
The proxy dao implementation of mybatis is used here. Will there be a prompt or exception thrown when the insert fails?
Use
@Transaction(rollbackFor=Throwable.class)
to annotate the Service class or a specific method. It should be noted that the @Transaction annotation only captures and rolls back RuntimeException by default. That is to say, if you throw Exception exceptions will not be rolled back, so remember to add rollbackFor=Throwable.class to specify that all exceptions will be rolled backI agree with @Positive Energy Frontline’s answer, and add that if you perform multiple operations in a method, you need to consider the propagation scope of the transaction. Of course, the default is propagation = Propagation.REQUIRED, which means: If there is no transaction currently, create a new one A transaction, if it already exists in a transaction, add it to this transaction.
If a compilation exception is thrown, can you catch and handle it yourself? Since you use the spring framework and catch and handle the exception yourself, the framework will not help you roll back. The premise is that you configure the transaction rollback in the spring configuration file. The roll strategy agrees with what was said on the first floor that the default operation is exception rollback. Transactions are added to the service layer. Your two insert methods do not have transactions themselves, so there is no need to consider propagation behavior.
After reading the previous two answers, I will add something more. Transactions in Spring are by default interface-based JDK proxies. So for the two insert methods called in your Service layer, if insert is also a service layer method, it must be an interface method. If it is a private method, the private method will not be rolled back. Also pay attention to transaction issues in multi-threaded situations.