数据库事务遇到的一点坑

  • Post author:
  • Post category:其他


数据库操作回滚 失败 spring-boot jpa oracle

现场

 public String update(MenuApi menuApi){
        final Optional<MenuApi> byId = menuApiCodeRepistory.findById(menuApi.getMenuId());
        doUpdate(menuApi, byId);
        return  "success";
    }
    @Transactional
    public void doUpdate(MenuApi menuApi, Optional<MenuApi> byId) {
        byId.get().setApiCode(menuApi.getApiCode());
        menuApiCodeRepistory.saveAndFlush(byId.get());
        int a = 1/0;
    }

sql log

2020-05-30 15:33:36.901  INFO 3630 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 8 ms
Hibernate: select menuapi0_.menu_id as menu_id1_0_0_, menuapi0_.api_code as api_code2_0_0_ from menu_api menuapi0_ where menuapi0_.menu_id=?
Hibernate: update menu_api set api_code=? where menu_id=?
2020-05-30 15:33:37.222 ERROR 3630 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ArithmeticException: / by zero] with root cause

java.lang.ArithmeticException: / by zero
	at com.example.oracledemo.service.DemoService.doUpdate(DemoService.java:43) ~[classes/:na]
	at com.example.oracledemo.service.DemoService.update(DemoService.java:36) ~[classes/:na]

解决办法

@Transactional 放在update 方法上面

链接:https://pan.baidu.com/s/1tafvF4fiiJxbYlUmLjGV_Q 密码:giqd



版权声明:本文为zc888168原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。