springmvc的controller中使用@Transactional无效
最近看mybatis的时候做了一个练习,但是进行事务处理的时候出了问题,如下
package com.henu.lz.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import com.henu.lz.dao.PersonMapper;
import com.henu.lz.model.Person; @Controller
public class SupplierController { @Autowired
PersonMapper personMapper; @Transactional
@RequestMapping("/add")
public String addPerson(
@RequestParam("name1") String name1,
@RequestParam("age1") int age1,
@RequestParam("name2") String name2,
@RequestParam("age2") int age2,
Model model) { Person person1 = new Person();
Person person2 = new Person();
person1.setName(name1);
person1.setAge(age1);
person2.setName(name2);
person2.setAge(age2); personMapper.save(person1);
personMapper.save(person2); model.addAttribute("message", "添加成功!");
return "success";
}
}
spring容器和springmvc的配置都没有问题,dao层就是mybatis比较与众不同的的写有sql的xml以及接口。在从前台传值的时候person1正常传,person2传能抛SQLException的值,按理说事务应该回滚的,数据库中不会有person1,但是查看数据库却有person1。用的mysql,引擎设置为innodb后还是这样,换了oracle之后依然如此。这两次save不在一个事务。
网上看了下别人的经历,有的是try…catch之后自行处理没有throw,有的是说设置rollbackFor……有一个比较接近
http://www.iteye.com/topic/714686
用的是 hibernate 3.2,在配置dao和controller的时候都用了注解方式自动扫描
<context:component-scan base-package="com..." />
这样说是导致dao中事务无效,我试过这样配置,服务启动直接就出错了,很显然冲突,而且用context:include-filter和context:exclude-filter来屏蔽掉controller层的解决办法也有点没必要,分别扫描dao层和controller层不就行了,我是这样配置的。
查看mybatis官网上的project,是比我多了一层service层,然后在service中用的@transactional,我加了一层
package com.henu.lz.service; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.henu.lz.dao.PersonMapper;
import com.henu.lz.model.Person; @Service(value="personService")
public class PersonServiceImpl implements PersonService { @Autowired
private PersonMapper personMapper; @Transactional
public void save(Person p1, Person p2) {
personMapper.save(p1);
personMapper.save(p2);
} }
相应的,controller也修改为
package com.henu.lz.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.henu.lz.model.Person;
import com.henu.lz.service.PersonService; @Controller
public class SupplierController { @Resource
private PersonService personService; @RequestMapping("/add")
public String addPerson(
@RequestParam("name1") String name1,
@RequestParam("age1") int age1,
@RequestParam("name2") String name2,
@RequestParam("age2") int age2,
Model model) { Person person1 = new Person();
Person person2 = new Person();
person1.setName(name1);
person1.setAge(age1);
person2.setName(name2);
person2.setAge(age2); personService.save(person1, person2); model.addAttribute("message", "添加成功!");
return "success";
}
}
这时再按开始那样传值能得到预期结果了。
一开始也注意到少了一层service,但是觉得在controller中做同样的事情也可以的,所以就杯具了。
因为spring的context和mvc是分开的,貌似controller不能被注册到spring的context中,于是不能被transactionManager拦截,那么controller中那个@transactional就不起作用了。看了声明式事务的五种配置,都是对注册到context中的bean起作用的,不论是拦截器方式还是aop:config方式。
事务注解方式不能应用于接口,我的mapper用的又是xml方式的,所以只能加一层service,然后controller中的交易就放在service层。
springmvc的controller中使用@Transactional无效的更多相关文章
- spring aop在mvc的controller中加入切面无效
spring aop在mvc的controller中加入切面无效 因为MVC的controller,aop默认使用jdk代理.要使用cglib代理. 在spring-mybatis.xml配置文件中加 ...
- SpringMVC的Controller中使用线程安全的初始化
因为SpringMVC的Controller默认是单例, 在这种情况下, Controller中使用的私有变量必须也是单例, 例如各种service, 否则会有多线程访问数据互相修改的问题. 对于需要 ...
- springmvc 解决 controller 中出现死循环并 stackoverflow 的问题
这是因为这个controller中的方法返回值为void类型,且没有request response这类衍生的重定向,或者返回值为String,但是是null等等的情况,都会引起死循环,然后stack ...
- 在springMVC的controller中获取request,response对象的一个方法
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttr ...
- springMVC:将controller中数据传递到jsp页面
1> 将方法的返回值该为ModelAndView在返回时,将数据存储在ModelAndView对象中如: newModelAndView("/WEBINF/jsp/showData.j ...
- springboot项目中,@transactional 无效
问题: springboot项目,依然是使用jpa.Hibernate来操作mysql,涉及到数据库的操作,就少不了事务.写了一个接口,用来测试@Transaction注解的作用,发现没有效果 分析: ...
- 9.SpringMVC和json结合传递数据 && 10.SpringMVC获取controller中json的数据
- SpringMVC Controller中注入Request成员域和在方法中定义中HttpServletRequest有啥区别
先说结论,在Controller中注入Request是线程安全的. 以下是解释: 我们先来看看这两者有什么不同 在controller注入成员变量request 可以看到注入的是一个代理对象 写在方法 ...
- 【转】在SpringMVC Controller中注入Request成员域
原文链接:https://www.cnblogs.com/abcwt112/p/7777258.html 原文作者:abcwt112 主题 在工作中遇到1个问题....我们定义了一个Controlle ...
随机推荐
- EL表达式和JSTL标签库
expresion language表达式语言 可以输出表达式的值.跟jsp的表达式脚本一样.计算表达式的值后输出. EL表达式出现的目的是为了使JSP写起来更加简单,让jsp的代码更佳简化. 1. ...
- FMDB数据库升级
FMDBMigrationManager 是与FMDB结合使用的一个第三方,可以记录数据库版本号并对数据库进行数据库升级等操作.首先要集成FMDB和FMDBMigrationManager,建议使用c ...
- HTTP Client使用总结
1.如果服务器使用HTTPS协议,使用HTTPClient的话,需要配以EasySSLProtocolSocketFactory 2.Tomcat对HTTP的Post和Get请求处理是不一样的.Spr ...
- String()与toString的区别
1..toString()可以将所有的的数据都转换为字符串,但是要排除null 和 undefined 代码示例: var a = null.toString()--报错 var b = underf ...
- python代码格式检查工具部署pre_commit
如何使用pre_commit?1. 合并该分支 2. 在git根目录下使用pre-commit install即可3. 如果没有装pre-commit 安装一下pip install pre-comm ...
- 关于RabbitMQ Queue Argument的简介
1.Message TTL message在队列queue中可以存活多长时间,以毫秒为单位:发布的消息在queue时间超过了你设定的时间就会被删除掉. channel.queueDeclare(&qu ...
- python之函数(可选参数和混合参数)
代码举例: # 函数可选参数举例,hoppy参数可传可不传 def getinfo(name, age, hoppy=''): if hoppy: print("name:", n ...
- async中await是干啥的,用不用有什么区别?
最近在研究异步编程,用的async await task啥的,但是都这几个概念很模糊,还有不太清楚await是干啥的,task又是干啥的,用不用await有什么区别,他们三个之间的联系是什么? tas ...
- BZOJ5178[Jsoi2011]棒棒糖——主席树
题目描述 Coffee的世界里也是有棒棒糖卖的,Coffee买了N(1≤N≤50000)只连着的.这N只棒棒糖包裹在小塑料袋中,排成 一列,相邻的两只棒棒糖的塑料袋是接起来的.为了方便,我们把棒棒糖从 ...
- python 时间模块 -- time
time 时间模块 和时间有关系的我们就要用到时间模块.在使用模块之前,应该先导入模块. # 常用方法 import time print("现在执行我") time.sleep( ...