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 用注解方式添加事务不生效解决方法的更多相关文章

  1. (转)使用Spring注解方式管理事务与传播行为详解

    http://blog.csdn.net/yerenyuan_pku/article/details/52885041 使用Spring注解方式管理事务 前面讲解了怎么使用@Transactional ...

  2. SpringMVC的注解方式

    mvc-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  3. 【spring cloud】spring boot2.x下 使用feign,注解@EnableFeignClients 找不到的解决方法

    spring boot2.x下 使用feign,注解@EnableFeignClients 找不到的解决方法 在spring boot1.x下,使用注解@EnableFeignClients,jar包 ...

  4. 在Springmvc普通类@Autowired注入request为null解决方法

    在Springmvc普通类@Autowired注入request为null解决方法   在类中加入以下注入request对象的代码,运行时发现request为null,注入失败.在@Controlle ...

  5. Windows中Nginx配置nginx.conf不生效解决方法(路径映射)

    Windows中Nginx配置nginx.conf不生效解决方法 今天在做Nginx项目的时候,要处理一个路径映射问题, location /evaluate/ { proxy_pass http:/ ...

  6. Spring 使用注解方式进行事务管理

    转载:http://www.cnblogs.com/younggun/archive/2013/07/16/3193800.html 使用步骤: 步骤一.在spring配置文件中引入<tx:&g ...

  7. SpringMVC的注解方式配置

    SpringMVC支持使用注解方式配置,比配置文件方式更加灵活易用,是SpringMVC使用的主流模式. 1.在配置文件中开启SpringMVC的注解 <!-- 开启包扫描 --> < ...

  8. SpringMVC源码分析-400异常处理流程及解决方法

    本文涉及SpringMVC异常处理体系源码分析,SpringMVC异常处理相关类的设计模式,实际工作中异常处理的实践. 问题场景 假设我们的SpringMVC应用中有如下控制器: 代码示例-1 @Re ...

  9. el表达式无法获取springmvc的model封装好的数据之解决方法

    近日碰到奇怪的问题,应该挺好解决的,可是就是卡住我两天 下面我来描述一下问题 用的是springmvc,自然需要controller,假设我现在所有的配置都是对的. controller代码 @Req ...

随机推荐

  1. vsftpd.conf

    引用:http://www.linuxidc.com/Linux/2012-08/67389.htm 研究嵌入式开发,开始使用的是Linux平台,用的是Ubuntu环境,但是发现Ubuntu的图形界面 ...

  2. eclipse 安装activity插件

    公司做流程需要用到流程插件,之前用了bpm4 activity是基于bpm4延伸的,这里先介绍下activity 插件是如何安装的 官网资料 *Name:*Activiti BPMN 2.0 desi ...

  3. msql,触发器无事物回滚,插入之前满足条件再插入

    很少写mysql的触发器和存储过程,由于需要需要做一个很小的判断,要用到触发器,要达到的效果就是,插入之前判断是否满足条件如果不满足就不插入 如果用sqlserver 或者orcale 就很简单,按s ...

  4. android 布局学习

    各种layout用到的一些重要属性 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_centerVertic ...

  5. NoSQL数据库笔谈(转)

    NoSQL数据库笔谈 databases , appdir , node , paper颜开 , v0.2 , 2010.2 序 思想篇 CAP 最终一致性 变体 BASE 其他 I/O的五分钟法则 ...

  6. HTTPS原理详解

         HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTT ...

  7. 不可错过的炒鸡棒的js迷你库

    小而美被实践是最好用的,这里收藏了一些很好用的js库,他们都功能单一且非常小. COOKIE.JS  https://github.com/js-coder/cookie.js 如果你操作过cooki ...

  8. 前端使用js读取文件

    最近同时问我js能不能读取本地文件: 想起以前看到js读取本地文件的文章,然后自己写了个demo. ps:这有点想Java的IO流,但是又有差别. 首先我们定义一个input标签type=" ...

  9. sql查看锁与解锁

    select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys.dm_tran ...

  10. springmvc json字符串转化成json对象

    问题出现在 :页面数据列表的展示出现 [object HTMLInputElement] 找到问题的所在原因后又三种解决方案 一:格式化json字符串为json对象字符串 success:functi ...