LoginSignup
0
0

More than 3 years have passed since last update.

What happen of a class annotated with @Transactional

Posted at

Proxy class is created when class is annotated with @Transactional

In my notice board example application, class info.saladlam.example.spring.noticeboard.service.MessageServiceImpl is annotated with @Transactional. When bean factory processes this class, proxy instance of this class, with org.springframework.transaction.interceptor.TransactionInterceptor set is created. You may view the actual instance of MessageService from Eclipse's debugger.

eclipse_debug_messageservice.png

Function of TransactionInterceptor

  1. Send begin transaction and commit signal to PlatformTransactionManager instance
  2. Handling exception case

Below is code from method org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(Method, Class<?>, InvocationCallback).

// ...
        // If the transaction attribute is null, the method is non-transactional.
        TransactionAttributeSource tas = getTransactionAttributeSource();
        final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null);
        final org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(Method, Class<?>, InvocationCallback) tm = determineTransactionManager(txAttr);
        final String joinpointIdentification = methodIdentification(method, targetClass, txAttr);

        if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
            // Standard transaction demarcation with getTransaction and commit/rollback calls.
            TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);

            Object retVal;
            try {
                // This is an around advice: Invoke the next interceptor in the chain.
                // This will normally result in a target object being invoked.
                retVal = invocation.proceedWithInvocation();
            }
            catch (Throwable ex) {
                // target invocation exception
                completeTransactionAfterThrowing(txInfo, ex);
                throw ex;
            }
            finally {
                cleanupTransactionInfo(txInfo);
            }
            commitTransactionAfterReturning(txInfo);
            return retVal;
        }
// ...

When program executes to

retVal = invocation.proceedWithInvocation();

method of info.saladlam.example.spring.noticeboard.service.MessageServiceImpl is called.

Function of PlatformTransactionManager

In Spring Framework context there is a singleton PlatformTransactionManager instance. In this application, the implementation is org.springframework.jdbc.datasource.DataSourceTransactionManager. This class is for handle all transaction action happened.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0