java使用注解方式添加事务_springmvc 用注解方式添加事务不生效解决方法

标签:

springmvc 事务注册有很多种方法,在此我只mark 用注解方式添加transaction不生效的解决办法。

springmvc 注解方法添加事务步骤:

1.在 spring的 root-context.xml (WEB-INF/)文件中添加事物管理:

或者

2.添加注解驱动

3.在需要添加事物管理的java类上添加@Transactional

@Servicepublic class HomeServiceImpl implementsHomeService {

@AutowiredprivateHomeDao homeDao;public static final Logger LOGGER = LoggerFactory.getLogger(HomeServiceImpl.class);/*** note:need add throw RuntimeException*/@Transactional

@Overridepublic int updateAgeNonException() throwsException {try{

Map map = new HashMap();

map.put("age", 10);

homeDao.updateAge(map);

map.put("age", 30);

homeDao.updateAge(map);

}catch(Exception e) {

LOGGER.error("debug ****", e);throw newRuntimeException();

}return 0;

}

@Overridepublic int updateAgeException() throwsException {try{

Map map = new HashMap();

map.put("age", 10);

homeDao.updateAge(map);//exception

System.out.println(2/0);

map.put("age", 30);

homeDao.updateAge(map);

}catch(Exception e) {

LOGGER.error("debug ****", e);throw newRuntimeException();

}return 0;

}public ListqueryData() {returnhomeDao.queryData();

}

}

事物添加以上3步就ok了。

启动server运行一下,看事物是否生效。一般情况下是不会生效的。

原因在于,service方法被注入了2次。解决办法:

1.在root-context.xml 中添加包扫描,扫描所有需要注入的包

2.在servlet-context.xml配置文件中,包扫描的时候排除扫描service

标签:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值