There are the following code scenarios, A
the a1
methods of the class are not marked with @Transactional
annotations, and the a2
methods are marked with @Transactional
annotations, then a1
call the a2
method in the method, will the transaction start at this time?
Does not start a transaction. a1
The method is A
the native method of the target class . When calling a1
, the method directly enters the target class A
to call. The A
only a2
native methods in the target class are marked with @Transactional
annotations a1
. Called here a2
is the directly executed a2
native method, and is not called by creating a proxy object. , So TransactionInterceptor
the invoke
method that does not enter , does not open the transaction.
At this time, if the a1
method is marked with @Transactional
annotations, the a2
method is not marked with @Transactional
annotations, but a1
the access modifier of protected
the a1
method is , a2
will calling the method in the method start the transaction?
Will not open the transaction. @Transactional
The 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 public
modified 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 public
and protected
methods, but when Spring can determine whether transaction enhancement can be applied to the current target class, it traverses the public
method of the target class , so the Cglib method also Only valid for public
methods.
How is declarative transaction processing implemented in the Spring framework?
When the Spring container initializes each singleton bean, it will traverse all the BeanPostProcessor
implementation classes in the container and execute its postProcessAfterInitialization
methods. When executing AbstractAutoProxyCreator
the postProcessAfterInitialization
methods 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 @Transactional
annotation 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 TransactionInterceptor
instance will be obtained . In its invoke
method, 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 @Transactional
on the parameter value configured on the annotation. If you need to start a new transaction, get a Connection
connection, and then change the connection's auto-commit transaction false
to manual submission. When the target method of the target class is called, the completeTransactionAfterThrowing
method will be entered if an exception occurs .
Can you tell me the principle of its realization?
If the class A
marked on the @Transactional
annotation, Spring container will startup, the class A
to create a proxy class B
, class A
all the public
methods in the proxy class will B
have a corresponding proxy method, call the class A
one of the public
methods will enter the corresponding proxy method Processing in
If only the class A
's b
methods (using public
modified) marked on the @Transactional
annotation, Spring container will startup, the class is A
to create a proxy class B
, but only for the class A
is b
to create a proxy methods, call the class A
's b
method will enter the corresponding proxy Processed in the method, call A
other public
methods of the class, or enter A
the method of the class for processing. Before entering a method in the proxy class, the method TransactionInterceptor
in the class will be executed first to invoke
complete 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
( Error
and RuntimeException
).