java mybatis事务回滚_springmvc、MyBatis事务管理回滚失败、单元测试回滚却成功

在Spring MVC和MyBatis环境中,事务回滚出现异常,业务逻辑层的事务无法正常回滚,即使在单元测试中能够成功。问题出现在控制层使用SpringUtil手动获取bean,而非依赖注入,导致事务管理失效。探讨了是否需要检查数据库操作返回值来判断事务是否成功的问题。
摘要由CSDN通过智能技术生成

gistfile1.txt

spring配置文档

applicationContext.xml

业务逻辑层

@Transactional

public int updateByID(Base base){

if(basePicMapper.updateByID(base.getBasePic())<=0){

throw new RuntimeException();

}

int i=2/0;

if(baseMapper.updateByID(base)<=0){

throw new RuntimeException();

}

return 1;

}

控制层

@RequestMapping("updateTextPic")

public String updateTextPic(Base model, HttpServletRequest request, HttpServletResponse response) {

Base base=baseService.selectByID(model.getId());

base.setContent(model.getContent());

base.getBasePic().setLink(model.getBasePic().getLink());

baseService.updateByID(base);

request.setAttribute("rdtPath","/admin/list/base");

return "/redirect_page";

}

这样没有回滚

一个声明bean实例的工具类

public class SpringUtil{

public Object getBean(String beanName){

ApplicationContext xtx=new ClassPathXmlApplicationContext("applicationContext.xml");

return xtx.getBean(beanName);

}

}

单元测试就可以回滚

applicationContext.xml配置bean

单元测试代码

public class MenutypeServiceTest {

MenuTypeService menutypeService;

@Before

public void setUp() throws Exception {

menutypeService =(MenuTypeService) (new SpringUtil()).getBean("menuTypeServices");

}

@After

public void tearDown() throws Exception {

}

@Test

public void selectAll() throws Exception {

System.out.println(menutypeService);

}

}

通过控制台调用不能回滚,通过单元测试调用就能回滚。为什么?试过在控制层里用SpringUtil从配置文档获取一个bean实例而不是自动注入,依然无法回滚。

另一个问题,需不需要判断插入、修改的语句返回值是否大于0?为什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值