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 ...
随机推荐
- 5-Zend Studio配置
0-将文件编码设置成utf-8 Window>Preferences>General>Content Types>Text Default encoding:utf-8 1-Z ...
- Google Code Jam Round 1A 2015 解题报告
题目链接:https://code.google.com/codejam/contest/4224486/ Problem A. Mushroom Monster 这题题意就是,有N个时间点,每个时间 ...
- ThinkPad E430光驱面板拆卸方法
前一阵买了固态硬盘,拆出来的机械硬盘一直放在一边落灰.想着不能这么浪费资源,就买了光驱硬盘架,打算把光驱拆出来换上机械硬盘. 光驱很好拆,把后盖板打开之后,拧下固定的螺丝,用螺丝刀轻轻撬一下,光驱就出 ...
- 【数论】X problem
X problem X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- SNMP
net-snmp 了解snmp程序最好的工具,snmpwalk和snmptable都是关键命令,举例: snmptable -v 2c -c public X.X.X.X ifTable 显示网络接口 ...
- Scala学习笔记之二--基本数据类型
前言 本篇主要讲Scala的基本数据类型,更多教程请参考:Scala教程 基本数据类型 Scala一共提供了9中数据类型,Scala的基本数据类型与java中的基本数据类型是一一对应的,这是Scala ...
- 161229、SpringMVC的各种参数绑定方式
1. 基本数据类型(以int为例,其他类似): Controller代码: @RequestMapping("saysth.do") public void test(int co ...
- RabbitMQ高可用方案总结
RabbitMQ的集群方案有以下几种: 1.普通的集群 exchange,buindling再所有的节点上都会保存一份,但是queue只会存储在其中的一个节点上,但是所有的节点都会存储一份queue的 ...
- Java设计模式(一)——代理模式
有高手云:了解设计模式才算是入门级的程序员. 所以为了入门我打算把我学习到的设计模式逐条总结下来.和别人的文章不同,我几乎只提供了测试源码与细节分类.原因是,我相信对于设计来说,你永远无法给出终极答案 ...
- UITableView heightForHeaderInSection遇到的坑
出现这种现象只需要把 heightforfoot改为0.01 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSectio ...