zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
  • 相关阅读:
    图像连通域标记算法研究
    介绍给大家学习Silverlight的网址!
    (1)Silverlight开发工具Microsoft Expression Blend 2 之“Hello SilverLight”
    (4)Silverlight开发工具Microsoft Expression Blend 2 之“Combine切割组合效果”
    (3下)Silverlight开发工具Microsoft Expression Blend 2 之“States和Object面板简单“按钮””
    (2)Silverlight开发工具Microsoft Expression Blend 2 之“左侧ToolBox”
    SilverLight 2正式版已经发布,Silverlight Toolkit即将和大家见面
    什么是RIA??未来发展方向!
    Microsoft SQL Server Database Publishing Wizard 使用
    (3上)Silverlight开发工具Microsoft Expression Blend 2 之“States和Object面板简单“按钮””
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/7523729.html
Copyright © 2011-2022 走看看