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 ...
随机推荐
- C# 创建Windows服务
创建windows服务项目 2 右键点击Service1.cs,查看代码, 用于编写操作逻辑代码 3 代码中OnStart用于执行服务事件,一般采用线程方式执行方法,便于隔一段事件执行一回 END ...
- Pyton 模拟Post登录
import sys import urlib.parse import urllib.request import http.cookiejar import random import math ...
- linux运维自动化shell脚本小工具
linux运维shell 脚本小工具,如要分享此文章,请注明文章出处,以下脚本仅供参考,若放置在服务器上出错,后果请自负 1.检测cpu剩余百分比 #!/bin/bash #Inspect CPU # ...
- SQL语句 还原未知逻辑名称数据库
1. 查看 SQL Server 2000 中 Northwind 数据库文件的逻辑文件名(logical file name)和物理文件路径(operation system file name): ...
- linux修改IP
linux命令行修改IP的2个方法 2010-12-30 10:25:50 分类: LINUX 方式一: ifconfig eth0 192.168.1.18 netmask 255.255.255 ...
- springboot 注入Servlet,Filter,Listener的方法
其实就是注入 FilterRegistrationBean . ServletRegistrationBean . ServletListenerRegistrationBean 这三个类 直接上 ...
- tomcat输出servlet-api.jar - jar not loaded 解决办法
tomcat输出servlet-api.jar - jar not loaded 解决办法 启动tomcat后,控制台输出信息:WEB-INF/lib/servlet-api.jar not load ...
- 数据库为什么要用B+树结构--MySQL索引结构的实现
原理: http://blog.csdn.net/cangchen/article/details/44818485 http://blog.csdn.net/kennyrose/article/de ...
- EF 如何更新少量字段
EF更新少量字段需要解决两个问题 1.动态的将需要更新的字段提取出来 2.将提取出来的字段设为更新状态 通常更新的时候,都是根据条件将实体取出来,然后赋值字段,最后更新整个实体,所以在方法上看似是更新 ...
- poj2485 kruskal与prim
Kruskal: #include<iostream> #include<cstdio> #include<algorithm> using namespace s ...