0
回答
Spring Boot Transactional
华为云实践训练营,热门技术免费实践!>>>   

Spring boot transactional NullPointerException未回滚,这个要怎么解决呀,各位大大侠?

@SpringBootApplication
@EnableAsync
@EnableScheduling
@EnableWebSecurity
@EnableTransactionManagement
@Import({WebMvcConfiguration.class})
public class BootStrap extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer {
    private static Logger logger = LoggerFactory.getLogger(BootStrap.class);

    public static void main(String[] args) throws Exception {
        logger.info("--------------------------------------------");
        logger.info("------------------application start--------------------------");
        SpringApplication.run(BootStrap.class, args);
        logger.info("-------------------application end-------------------------");
        logger.info("--------------------------------------------");
    }

    @Override
    public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
        logger.info("--------------------------------------------");
        logger.info("------------------customize start--------------------------");
//        configurableEmbeddedServletContainer.setPort(8088);
        logger.info("-------------------customize end-------------------------");
        logger.info("--------------------------------------------");
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        logger.info("--------------------------------------------");
        logger.info("------------------configure start--------------------------");
        return builder.sources(BootStrap.class);
    }
}

 

Controller类

@RequestMapping("fortunate/fortunateAuditDo" + REQUEST_SUFFIX)
    @ResponseBody
    public String fortunateAuditDo(@Valid FortunateRequest request, ModelMap modelMap) {
        hh(request, modelMap, new ProcessImplementor<FortunateRequest>() {
            @Override
            public void process(FortunateRequest request, ModelMap response) {
                FortunatePojo f = fortunateService.buildFortunate(request);
                Integer fortunate = 1 << f.getJetton();
                String s = null;
                // 制造一个NullPointerExcetpion,然而buildFortunate插入进去的数据并没有回滚
                s.equals("xx");
            }
        });
        return WebUtil.ajaxJsonReturn(modelMap);
    }

hh方法在Controller的父类中

protected <R extends RequestBase> ModelMap hh(R request, ModelMap result, Implementor<R> implementor) {
        long startTime = System.currentTimeMillis();
        try {
            setResultDefault(result, StatusEnum.PROCESSING);

            implementor.beforeProcess(request, result);

            implementor.process(request, result);

            implementor.afterProcess(request, result);

            setResultSuccess(result);
        } catch (LotteryException e) {
            setResultFail(result, e);

        } catch (Exception e) {
            logger.error("lottery ERROR={}", e);
            result.addAttribute(Constants.LOTTERY_STATUS, StatusEnum.FAIL);
            result.addAttribute(Constants.LOTTERY_CODE, ErrorCodeEnum.UNKNOWN_ERROR.getCode());
            result.addAttribute(Constants.LOTTERY_MESSAGE, e.getMessage());

        } finally {
            logger.info("执行结果lottery LOTTERY_STATUS={},LOTTERY_CODE={},LOTTERY_MESSAGE={},耗时{}", result.get(Constants.LOTTERY_STATUS)
                    , result.get(Constants.LOTTERY_CODE), result.get(Constants.LOTTERY_MESSAGE), System.currentTimeMillis() - startTime);
        }
        return result;
    }

Implementor:

public abstract class ProcessImplementor<R extends RequestBase> {

    /**
     * 前置处理钩子
     *
     * @param request
     * @param response
     */
    public void beforeProcess(R request, ModelMap response) {

    }

    /**
     * 逻辑处理
     *
     * @param request
     * @param response
     */
    @Transactional
    public abstract void process(R request, ModelMap response);

    // 在这里加入注解,然而并且没回滚,怎么解决呀?


    /**
     * 后置处理钩子
     *
     * @param request
     * @param response
     */
    public void afterProcess(R request, ModelMap response) {

    }
}

 

大大侠们,能帮忙解决下吗?

 

举报
_冢彧
发帖于8个月前 0回/196阅
顶部