• spring 事务处理


    Spring事务处理主要分以下三个主要的过程:

    (1).读取配置的事务处理属性,并转化为Spring事务处理所需要的内部数据结构

    (2).事务配置属性与线程绑定

    (3).交由PlatformTransactionManager的具体实现类来实现,如DataSourceTransactionManager和HibernateTransactionManager

     

    2。TransactionProxyFactoryBean

    为Spring的事务处理的实现做准备工作,包括配置AOP的拦截器、通知器

    发生事务时,首先通过getObject方法向Spring事务管理容器索取被被管理方法的事务属性,触发调用事务拦截器的拦截方法进行事务处理。

    Spring的AOP代理通过invoke回调方法对切入点方法进行拦截处理

    public class TransactionInterceptor extends TransactionAspectSupport implements MethodInterceptor, Serializable {
        @Override
        public Object invoke(final MethodInvocation invocation) throws Throwable {
            // Work out the target class: may be {@code null}.
            // The TransactionAttributeSource should be passed the target class
            // as well as the method, which may be from an interface.
            Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);
    
            // Adapt to TransactionAspectSupport's invokeWithinTransaction...
            return invokeWithinTransaction(invocation.getMethod(), targetClass, new InvocationCallback() {
                @Override
                public Object proceedWithInvocation() throws Throwable {
                    return invocation.proceed();
                }
            });
        }
    invokeWithinTransaction

    protected Object invokeWithinTransaction(Method method, Class<?> targetClass, final InvocationCallback invocation)
                throws Throwable {
    
            // If the transaction attribute is null, the method is non-transactional.
            final TransactionAttribute txAttr = getTransactionAttributeSource().getTransactionAttribute(method, targetClass);
            final PlatformTransactionManager tm = determineTransactionManager(txAttr);
            final String joinpointIdentification = methodIdentification(method, targetClass);
    
            if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
                // Standard transaction demarcation with getTransaction and commit/rollback calls.
                TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);
                Object retVal = null;
                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;
            }
    
            else {
                // It's a CallbackPreferringPlatformTransactionManager: pass a TransactionCallback in.
                try {
                    Object result = ((CallbackPreferringPlatformTransactionManager) tm).execute(txAttr,
                            new TransactionCallback<Object>() {
                                @Override
                                public Object doInTransaction(TransactionStatus status) {
                                    TransactionInfo txInfo = prepareTransactionInfo(tm, txAttr, joinpointIdentification, status);
                                    //...省略
                            });
    
                    // Check result: It might indicate a Throwable to rethrow.
                    if (result instanceof ThrowableHolder) {
                        throw ((ThrowableHolder) result).getThrowable();
                    }
                    else {
                        return result;
                    }
                }
                catch (ThrowableHolderException ex) {
                    throw ex.getCause();
                }
            }
        }
  • 相关阅读:
    learnVUEnote
    DOUAudioStreamer 中kqueue的应用
    OpenGL ES 2.0 渲染管线 学习笔记
    顶点着色器 学习笔记
    XCode 8.3 Automatically manage signing 问题
    【暴力】【几何】Codeforces Round #431 (Div. 2) B. Tell Your World
    标题
    【枚举】【gcd】Codeforces Round #432 (Div. 2) D. Arpa and a list of numbers
    【预处理】Codeforces Round #433 853B. Jury Meeting
    【构造】【规律】Codeforces Round #431 (Div. 2) C. From Y to Y
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/7523729.html
Copyright © 2020-2023  润新知