Spring's two common transaction propagation REQUIRED and REQUIRES_NEW

I watched the video of the online training organization and talked about the transaction communication mechanism. It was in the cloud, and the TM was wrong. It was a mess. I read the textbook and there is no practical example. Do a Mao training! ! !

There are two ways to implement transactions : one is programmatic and declarative. The programming style is to develop your own control, such as your own rollback and commit, which is actually a transaction interface for calling the database.
Declarative is @Transational to open the transaction, which is a kind of AOP implementation that generates proxy objects, submits and rolls back on methods. RuntimeException and Error are rolled back by default, and you can also specify the exception type to roll back.

Transaction isolation level (from low to high):
read uncommitted (one transaction is not committed, another transaction can be read)
read committed (one transaction is committed, another transaction can be read)
-the default repeatable of oracle in spring read (repeatability)-the default level of mysql in spring is
serializable (serializable)


Question 1: What happens if @Transactional is not added in spring?

The Controller has two services. The second service has an insert operation. After inserting, the following code reports an error, then the database has submitted the data, resulting in inconsistent transactions.

Solution: Just add @Transactional to the method in the second service, so that the operation in the second service will be rolled back.

but? The first service is still submitted! !

Solution: If the first service also adds Transational.

Question 2: As a result, the first service is still submitted! ! ! Why? Personally understand that because there are two different transactions in the two services, the first service that did not report an error will be submitted.

Solution: @Transactional is also added to the controller to incorporate these two services into one transaction.

Question 3: There is @Transactional in the controller, service1 does not have @Transactional, and service2 adds @Transactional. At this time, service2 reports an error. Will service1 be submitted?

Will not submit, because there is @Transactional on the controller, both service1 and service2 will be included in the transaction.

Question 4: There is @Transactional in the controller. If you want service1 to submit and service2 reports an error and rollback, what should I do?

controller加@Transactional,service1加@Transactional(propagation = Propagation.REQUIRES_NEW),servcie2加@Transactional

Two common propagation attributes in spring :

1. REQUIRED If there is a current transaction, use the current transaction, if there is no current transaction, create a new transaction

2. REQUIRES_NEW If there is a transaction currently, suspend the current transaction and start a new transaction. After the new transaction is executed, wake up the previously suspended transaction and continue execution. If there is no current transaction, create a new transaction.

Spring is REQUIRED by default, which means that it either succeeds together or fails together.

REQUIRES_NEW (teacher training institutions were wrong, really pit !!!) my understanding is to open a new a separate transaction that is not currently being given within @Transational, will be submitted.

Add @Transactional annotation to Controller method

MyService: REQUIRES_NEW starts a new transaction

MyService2: The default transaction propagation mechanism

Mapper

http://127.0.0.1:8080/in/0

Result: myService inserts data successfully, myService2 rolls back

note:

1. @Transactional should also be added to the controller layer, otherwise it will not work

 

 

 

Reference materials: https://blog.csdn.net/qianxiaopeng/article/details/82427689

Guess you like

Origin blog.csdn.net/x18094/article/details/115290933