AOP transaction management issue srping of custom cut is causing transaction control failure

applicationContext.xml:

<! - The method of recording the call time -> 
    < the bean ID = "methodExecuteTime" class = "com.common.aspect.Aspect"  /> 

    <! - the Spring config the AOP (com.customer.service * * * (.. ..)) several wildcards meanings: * a first: wild 
        any type of the second return value *: wild any class under the third packet com.customer.service *: wild packet com.customer the method of any of any class under .service 
        fourth ..: wild methods may have zero or more parameters -> 
    < AOP: AspectJ the autoproxy- Proxy-target-class = "to true" EXPOSE-Proxy = "to true " /> 
    < AOP: config Proxy-target-class =" to true " > 
        < AOP:pointcut id="serviceMethods"expression The = "Execution (com.customer.service .. * * * (..).)"  /> 
        < AOP: Advisor the advice-REF = "txAdvice" the pointcut-REF = "serviceMethods" Order = "2"  /> 

        < ! - here is configured such that the transaction rollback failure temporarily comment out control -> 
        <-! DB Service log -> 
        < AOP: Aspect the above mentioned id = "logThrsysServiceMethodExecuteTime" ref = "methodExecuteTime"  > 
            < AOP: pointcut the above mentioned id = " thrsysServiceMethods " expression The =" Execution (com.customer.service .. * *. * (..)) "  /> 
            <aop:around method="methodExecuteTime" pointcut-ref="thrsysServiceMethods" />
        </aop:aspect>
    </aop:config>

There were multiple operations to update the database in the process of an update at the beginning, in order to test the validity of the transaction is rolled back, placing some null pointer exception code in the code and found that the transaction was not rolled back.

Comment out <! - Here configured such that a control transaction failure rolls back the temporary commented -> contents of the normal transaction rollback. Remember when repeated several times to verify the transaction and not because of abnormal rollback, so just comment out the following section .
But then I do not know what, let go of the comments can be properly rolled back the transaction. What reason did not find a cause like this. (I do not know whether the problem was detected in time, or later changed because of what the cause, specifically in this record).

package com.common.aspect;

import com.common.util.SensitiveParamUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

@Slf4j
public class Aspect {

    private final static SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public Object methodExecuteTime (ProceedingJoinPoint joinPoint) throws Throwable {
        Object object;
        Date startDate = new Date();
        Signature signature = joinPoint.getSignature();
        MethodSignature ms = (MethodSignature)signature;
        Method method = ms.getMethod();
        Class<?>[] paramTypes = method.getParameterTypes();
        Object[] args = joinPoint.getArgs();
        //访问目标方法的参数:
        log.info("{} 调用时间:{}",signature.toString(), sdf.format(startDate));
        long start = System.currentTimeMillis();
        object = joinPoint.proceed();
        long end = System.currentTimeMillis();
        String time = formatExecuteTime(end - start);
        log.info("{} 执行时间:{}",signature.toString(), time);
        new Thread(() -> {
            for (int i = 0; i < paramTypes.length; i++) {
                args[i] = SensitiveParamUtils.getJson(args[i]);
            }
            log.info("{} 方法入参{}", signature.toString(), Arrays.toString(args));
        }).start();

        return object;
    }

    private String formatExecuteTime(long executeTime) {
        long min = (executeTime % 3600000) / 60000;
        long sec = (executeTime % 60000) / 1000;
        long msec = executeTime % 10000;
        StringBuilder sb = new StringBuilder();
        if (min > 0) {
            sb.append(min).append("m ");
        }
        if (sec > 0) {
            sb.append(sec).append("s ");
        }
        sb.append(msec).append("ms");
        return sb.toString();
    }
}

Unit test, to write a custom service, the validity of transaction rollback added null pointer exception code is detected in the service.

Guess you like

Origin www.cnblogs.com/super-chao/p/11275454.html