Interview question: Spring declarative transaction

There are the following code scenarios, Athe a1methods of the class are not marked with @Transactionalannotations, and the a2methods are marked with @Transactionalannotations, then a1call the a2method in the method, will the transaction start at this time?

Does not start a transaction. a1The method is Athe native method of the target class . When calling a1, the method directly enters the target class Ato call. The Aonly a2native methods in the target class are marked with @Transactionalannotations a1. Called here a2is the directly executed a2native method, and is not called by creating a proxy object. , So TransactionInterceptorthe invokemethod that does not enter , does not open the transaction.

At this time, if the a1method is marked with @Transactionalannotations, the a2method is not marked with @Transactionalannotations, but a1the access modifier of protectedthe a1method is , a2will calling the method in the method start the transaction?

Will not open the transaction. @TransactionalThe working mechanism is based on AOP, and AOP is implemented using a dynamic proxy. The dynamic proxy is either JDK or Cglib. If it is a JDK dynamic proxy method, according to the above analysis, we can know that the target method of the target class is defined in the interface, that is, it must be a publicmodified method to be proxied. If it is the Cglib method, the proxy class is a subclass of the target class, which can theoretically be proxied publicand protectedmethods, but when Spring can determine whether transaction enhancement can be applied to the current target class, it traverses the publicmethod of the target class , so the Cglib method also Only valid for publicmethods.

How is declarative transaction processing implemented in the Spring framework?

When the Spring container initializes each singleton bean, it will traverse all the BeanPostProcessorimplementation classes in the container and execute its postProcessAfterInitializationmethods. When executing AbstractAutoProxyCreatorthe postProcessAfterInitializationmethods of the class, it will traverse all the aspects in the container to find the aspects that match the currently instantiated bean. Here you will get the transaction attribute aspect, find the @Transactionalannotation and its attribute value, and then create a proxy object based on the obtained aspect. The default is to use the JDK dynamic proxy to create the proxy. If the target class is an interface, use the JDK dynamic proxy, otherwise use Cglib. In the process of creating the proxy, the interceptor corresponding to the current target method will be obtained. At this time, an TransactionInterceptorinstance will be obtained . In its invokemethod, the transaction is started and rolled back. When the transaction operation is required, Spring will call the target class. Before the target method starts the transaction, calls the exception to roll back the transaction, and the transaction is submitted after the call is completed. Whether a new transaction needs to be started is determined based @Transactionalon the parameter value configured on the annotation. If you need to start a new transaction, get a Connectionconnection, and then change the connection's auto-commit transaction falseto manual submission. When the target method of the target class is called, the completeTransactionAfterThrowingmethod will be entered if an exception occurs .

Can you tell me the principle of its realization?

If the class Amarked on the @Transactionalannotation, Spring container will startup, the class Ato create a proxy class B, class Aall the publicmethods in the proxy class will Bhave a corresponding proxy method, call the class Aone of the publicmethods will enter the corresponding proxy method Processing in

If only the class A's bmethods (using publicmodified) marked on the @Transactionalannotation, Spring container will startup, the class is Ato create a proxy class B, but only for the class Ais bto create a proxy methods, call the class A's bmethod will enter the corresponding proxy Processed in the method, call Aother publicmethods of the class, or enter Athe method of the class for processing. Before entering a method in the proxy class, the method TransactionInterceptorin the class will be executed first to invokecomplete the logic of the entire transaction processing, such as whether to open a new transaction, monitor whether the transaction needs to be rolled back during the execution of the target method, and submit the transaction after the target method is completed Wait.

Does the Spring framework implement transaction rollback, does it perform transaction rollback operations for all types of exceptions?

Spring does not perform transaction rollback operations on all types of exceptions. By default, it only performs transaction rollback operations on Unchecked Exception( Errorand RuntimeException).

Published 94 original articles · liked 0 · visits 722

Guess you like

Origin blog.csdn.net/qq_46578181/article/details/105458054