Java Spring @Transactional method not rolling back as expected

后端 未结 3 2099
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 00:09

Below is a quick outline of what I\'m trying to do. I want to push a record to two different tables in the database from one method call. If anything fails, I want everythin

3条回答
  •  甜味超标
    2020-12-29 00:44

    Just because jdk parses aop annotation not only with the method, also parse annotation with the target class. For example, you have method A with @transactional, and method B which calls method A but without @transactional, When you invoke the method B with reflection, Spring AOP will check the B method with the target class has any annotations. So if your calling method in this class is not with the @transactional, it will not parse any other method in this method. At last, show you the source code: org.springframework.aop.framework.jdkDynamicAopProxy.class

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        ......
        // Get the interception chain for this method.
        List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
    
        // Check whether we have any advice. If we don't, we can fallback on direct
        // reflective invocation of the target, and avoid creating a MethodInvocation.
        if (chain.isEmpty()) {
        // We can skip creating a MethodInvocation: just invoke the target directly
        // Note that the final invoker must be an InvokerInterceptor so we know it does
        // nothing but a reflective operation on the target, and no hot swapping orfancy proxying.
            retVal = AopUtils.invokeJoinpointUsingReflection(target, method, args);
        }
        else {
        // We need to create a method invocation...
            invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
        // Proceed to the joinpoint through the interceptor chain.
        retVal = invocation.proceed();
        }
    }
    
        

    提交回复
    热议问题