springmvc 用注解方式添加事务不生效解决方法
springmvc 事务注册有很多种方法,在此我只mark 用注解方式添加transaction不生效的解决办法。
springmvc 注解方法添加事务步骤:
1.在 spring的 root-context.xml (WEB-INF/)文件中添加事物管理:
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="mysqlDataSource">
</bean>
或者
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="mysqlDataSource"/>
</bean>
2.添加注解驱动
<tx:annotation-driven transaction-manager="txManager"/>
3.在需要添加事物管理的java类上添加@Transactional
@Service
public class HomeServiceImpl implements HomeService {
@Autowired
private HomeDao homeDao; public static final Logger LOGGER = LoggerFactory.getLogger(HomeServiceImpl.class);
/**
* note:need add throw RuntimeException
*/
@Transactional
@Override
public int updateAgeNonException() throws Exception {
try {
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("age", 10);
homeDao.updateAge(map);
map.put("age", 30);
homeDao.updateAge(map);
} catch (Exception e) {
LOGGER.error("debug ****", e);
throw new RuntimeException();
}
return 0;
}
@Override
public int updateAgeException() throws Exception {
try {
Map<String,Integer> map = new HashMap<String,Integer>();
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 new RuntimeException();
}
return 0;
}
public List<String> queryData() {
return homeDao.queryData();
}
}
事物添加以上3步就ok了。
启动server运行一下,看事物是否生效。一般情况下是不会生效的。
原因在于,service方法被注入了2次。解决办法:
1.在root-context.xml 中添加包扫描,扫描所有需要注入的包
<context:component-scan base-package="com.ck.fm.*"></context:component-scan>
2.在servlet-context.xml配置文件中,包扫描的时候排除扫描service
<context:component-scan base-package="com.ck.fm.*" >
<!-- prevented Service injected twice -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
springmvc 用注解方式添加事务不生效解决方法的更多相关文章
- (转)使用Spring注解方式管理事务与传播行为详解
http://blog.csdn.net/yerenyuan_pku/article/details/52885041 使用Spring注解方式管理事务 前面讲解了怎么使用@Transactional ...
- SpringMVC的注解方式
mvc-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...
- 【spring cloud】spring boot2.x下 使用feign,注解@EnableFeignClients 找不到的解决方法
spring boot2.x下 使用feign,注解@EnableFeignClients 找不到的解决方法 在spring boot1.x下,使用注解@EnableFeignClients,jar包 ...
- 在Springmvc普通类@Autowired注入request为null解决方法
在Springmvc普通类@Autowired注入request为null解决方法 在类中加入以下注入request对象的代码,运行时发现request为null,注入失败.在@Controlle ...
- Windows中Nginx配置nginx.conf不生效解决方法(路径映射)
Windows中Nginx配置nginx.conf不生效解决方法 今天在做Nginx项目的时候,要处理一个路径映射问题, location /evaluate/ { proxy_pass http:/ ...
- Spring 使用注解方式进行事务管理
转载:http://www.cnblogs.com/younggun/archive/2013/07/16/3193800.html 使用步骤: 步骤一.在spring配置文件中引入<tx:&g ...
- SpringMVC的注解方式配置
SpringMVC支持使用注解方式配置,比配置文件方式更加灵活易用,是SpringMVC使用的主流模式. 1.在配置文件中开启SpringMVC的注解 <!-- 开启包扫描 --> < ...
- SpringMVC源码分析-400异常处理流程及解决方法
本文涉及SpringMVC异常处理体系源码分析,SpringMVC异常处理相关类的设计模式,实际工作中异常处理的实践. 问题场景 假设我们的SpringMVC应用中有如下控制器: 代码示例-1 @Re ...
- el表达式无法获取springmvc的model封装好的数据之解决方法
近日碰到奇怪的问题,应该挺好解决的,可是就是卡住我两天 下面我来描述一下问题 用的是springmvc,自然需要controller,假设我现在所有的配置都是对的. controller代码 @Req ...
随机推荐
- java快速学习
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Java是面向对象语言.这门语言其实相当年轻,于1995年才出现,由Sun公司出品 ...
- paper122:多尺度与多分辨率的关系
本文转自:http://blog.csdn.net/chgm_456d/article/details/8100513 我一直对于 多尺度与多分辨率没有一个准确的概念.后来看了一些文章,其中xiaow ...
- C# Linq 交集、并集、差集、去重
using System.Linq; List<string> ListA = new List<string>(); List<string> L ...
- Given a code_combination_id how can i get the code description? 获取科目组合描述
SELECT c.code_combination_id, c.concatenated_segments, apps.fa_rx_flex_pkg.get_description(101 -- p_ ...
- Linux 常用服务总结
使用linux有一段时间了,把自己在身边经常听到,使用linux经常遇到的linux常见服务总结出来,这样遇到问题会有更多的解决问题的办法,听别人摆这些专业术语时,才不会不知所云. 服务: 1.NFS ...
- LA 4255 UVa1423 拓扑排序
题目给出的是Sij的正负号,Sij=ai+...+aj,所以令前缀和Bi=a0+a1+..+ai,a0=0,B0=0,则有Sij=Bj-B(i-1): 由此构造出Bi的拓扑序列,只要每个拓扑序列相邻的 ...
- PHP开发中的缓存技术汇总
在PHP开发中,出于对网站服务器负载的考虑,往往需要对页面.数据等内容进行缓存处理,下面就来看看,在PHP开发中有哪些缓存方式吧. 1.页面部分缓存该种方式,是将一个页面中不经常变的部分进行静态缓存, ...
- 就最近学习MVC4.0的页面用法学到的东西
最近进了一家新公司,学习的东西还是蛮多的,首先了解的是@using(new Ajax.beginForm("",null,new AjaxOptions() { OnSuccess ...
- dev TreeList拖拽
一.说明 使用dev控件,TreeList1向TreeList2拖拽 二.属性 //允许拖拽 treeList1.AllowDrop = true; tre ...
- Xcode Build Settings Architectures
http://foggry.com/blog/2014/05/09/xcodeshe-zhi-xiang-zhi-architectureshe-valid-architectures/ https: ...