Transactions with Spring

Regarding spring transactions, here is purely a record:
in most projects, transactions are handed over to Spring for management, which should be a common practice.
1. Spring's transaction management can't manage the private, static, and protected methods of the class. Because Spring's transactions are based on AOP transactions that are dynamically proxied by interfaces. The methods of an interface must be public.
   Supplement: As long as the outermost method is handed over to Spring's transaction management, then calling private and other methods in this method can be managed by Spring's transaction. (This is only executed in the current thread according to this).
2. There are two classes: classA and classB, classA contains methodA1 and methodA2, and classB contains methodB1 and methodB2.
   Scenario 1:
   If classA.methodA1 (classA.methodA2()) is not managed by Spring's transaction in methodA1, even if methodA2 can be managed by Spring's transaction, the transaction will not be started. This is the reason for AOP.
   Scenario 2:
   If classA.methodA1 (classB.methodB1()) is not managed by Spring's transaction in methodA1, the methodB1 method of classB is called. If methodB1 is configured, it can be managed by Spring's transaction. Then Spring's transaction will manage the methodB1 method.
The above is not managed by Spring transactions, so it is naturally given to the database.

Analyze the following situation:
First, Spring's transaction configuration is do*, start*, server* will be managed by Spring transaction.
How many transactions will be opened in the following situations? When the code goes to startXXX(), it will start the transaction, and then start a thread. In this case, because it is not in the same thread, the transaction will not be passed on. doM1 is private, and the spring transaction will not be opened, so the transaction will be handed over to MySQL, and the SQL statement executed in doM1 will be committed.
1)
   public void startXXX() {
       new Thread() {
          private void doM1() {}
          public void server1.serverM2() {}
          public void server2.serverM3() {}
       }
   }
server1 and server2 are managed by spring.
server1.serverM2 will start a new transaction, and server2.serverM3 will also start a new transaction. Of course, the same transaction is used in the respective methods of server1 and server2.
2)
   public void startXXX() {
       new Thread() {
          private void doM1() {}
          public void doM2() {}
          public void doM3() {}
       }
   }
If there is a server call in doM2(), a new transaction will be opened, and doM3() will also open a new transaction. Of course, the same transaction is used in the respective methods of doM2 and doM3.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326647636&siteId=291194637