Spring Transcation注解原理解释&实践

    xiaoxiao2021-12-04  15

    每个事务都由拦截

    org.springframework.transaction.interceptor.TransactionInterceptor

    在看

    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(MethodInvocation)

    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(); } }); }

    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(Method, Class<?>, InvocationCallback)很关键

    /** * General delegate for around-advice-based subclasses, delegating to several other template * methods on this class. Able to handle {@link CallbackPreferringPlatformTransactionManager} * as well as regular {@link PlatformTransactionManager} implementations. * @param method the Method being invoked * @param targetClass the target class that we're invoking the method on * @param invocation the callback to use for proceeding with the target invocation * @return the return value of the method, if any * @throws Throwable propagated from the target invocation */ 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();//执行@Transcation标记的方法。举个例子:执行下图附件1的recharge方法 } catch (Throwable ex) { // target invocation exception completeTransactionAfterThrowing(txInfo, ex);//recharge方法出现异常,开始回滚事务 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); try { return invocation.proceedWithInvocation(); } catch (Throwable ex) { if (txAttr.rollbackOn(ex)) { // A RuntimeException: will lead to a rollback. if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } else { throw new ThrowableHolderException(ex); } } else { // A normal return value: will lead to a commit. return new ThrowableHolder(ex); } } finally { cleanupTransactionInfo(txInfo); } } }); // 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(); } } }附件1:

    --------------------------------------------------------------------------------------

    ----------------------------------分割线--------------------------------------------

    ---------------------------------------------------------------------------------------

    以上晦涩难懂,综上总结如下:

    spring的注解事务管理最重要的是org.springframework.jdbc.datasource.DataSourceTransactionManager

    <bean id="transactionManager"         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">         <property name="dataSource" ref="shopDataSource" />  </bean>

    //告诉spring采用那个transactionManager处理@Transcation注解

    <tx:annotation-driven transaction-manager="transactionManager" />

    --------------------------------------------------------------------------------------

    ----------------------------------原理简化--------------------------------------------

    ---------------------------------------------------------------------------------------

           DefaultTransactionDefinition def = new DefaultTransactionDefinition();         def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);         TransactionStatus status = transactionManager.getTransaction(def);         try {             // 1.更新订单             result = ordersWriteDao.update(orders);             Orders ordersDB = ordersWriteDao.get(orders.getId());             // 2.写订单日志             writeOrderInfo(ordersDB, sellerUser, type);             if (updateStore) {                 // 3.更新货品和商品的库存                 this.updateProductStock(orders.getId(), false);             }             transactionManager.commit(status);         } catch (Exception e) {             log.error("更新订单时出现未知异常:" + e);             transactionManager.rollback(status);             throw e;         }

    转载请注明原文地址: https://ju.6miu.com/read-680410.html

    最新回复(0)