spring+mybatis和spring 事务 事务不生效,求助各位大神

下次自动登录
现在的位置:
& 综合 & 正文
spring mvc + mybatis事务不起作用
用spring aop配置了事务,但是不起作用,困扰了好久。
事务配置如下:
&!-- 配置事务管理器,使用jdbc事务 --&
&bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&
&property name="dataSource" ref="dataSource" /&
&!-- 申明式事务 --&
&tx:advice id="txAdvice" transaction-manager="transactionManager"&
&tx:attributes&
&tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/&
&tx:method name="*" read-only="true"/&
&/tx:attributes&
&/tx:advice&
&aop:config&
&aop:pointcut id="allManagerMethod" expression="execution(* com.xiu.xclk.web.service.*.*(..))" /&
&aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" /&
&/aop:config&
查网上,有的说是因为spring mvc自动扫描的时候,把包括@service的注解一下都扫描今天了,所以在开始的时候要先排除掉对@service注解的扫描,见帖子:/architecture/14904.html
配置好之后,事务还是不起作用,很奇怪,之后查找,一个帖子给了很大的启发:/jakoes/blog/item/7256efc2c7ec3dc.html,说是的事务实现采用基于AOP的拦截器来实现,如果没有在事务配置的时候注明回滚的checked
exception,我的中抛的是java.lang.Exception,改成抛RuntimeException,事务起作用了。帖子中说两种办法可以解决:
,在事务属性后面加上需要回滚的checked exception。比如&prop
key="save*"&PROPAGATION_REQUIRED,-XXXXException&/prop&(注意那个"-",对应的是"+")
2, 不改配置文件,将需要事务回滚的异常继承自unchecked exception类,也就是RuntimeException。
那么对于第二种方法,我的是起作用了,但是第一种方法我的是声明了对Exception回滚了啊。后来一想,是不是要写全java类名啊,于是改成:
&tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/&,这样就起作用了。看来还是自己大意了,spring的配置文件要写全类名的。
&&&&推荐文章:
【上篇】【下篇】Spring4与Hibernate4整合后事务不生效 - 开源中国社区
当前访客身份:游客 [
当前位置:
整合spring4和hibernate4后发现声明式事务不生效,经检查将openSession()方法换为getCurrentSession()得到解决,但不知道是什么原理,希望各位朋友帮小弟解答
共有4个答案
<span class="a_vote_num" id="a_vote_num_
听宏哥的准没错
<span class="a_vote_num" id="a_vote_num_
spring+mybatis 才是王道
<span class="a_vote_num" id="a_vote_num_
因为session是交给spring容器管理的,openSession是自己new一个session,getCurrentSession是从上下文里取一个
--- 共有 1 条评论 ---
谢谢~有点明白了
(2年前)&nbsp&
<span class="a_vote_num" id="a_vote_num_
一而再,再而三的叫你们不要用Hibernate这种烂玩意, 就是不听
--- 共有 4 条评论 ---
其实,用jpa的话,hibernate还是可以做为 实现来用的。
(2年前)&nbsp&
: 现在我把hibernate逐渐驱除出我的项目了,太别扭了
(2年前)&nbsp&
这个我支持老宏,其他的不以为然
(2年前)&nbsp&
很久没用了~今天拿出来看看
(2年前)&nbsp&
更多开发者职位上
有什么技术问题吗?Spring+Mybatis整合事务不起作用之解决方案汇总
公司最近一个项目用到Spring和Mybatis,发现用起来挺方便,比以前的那个struts+hibernate舒服多了。废话少说,直接摆问题,碰到的问题是,mybatis不在事务中运行,后台日志报 &Closing no transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19006c9]&错误。无论是加了@Transactional注解和是没加都报这个信息。一个方法中插入多条数据,某次插入失败也不回滚。
问题描述:
环境: Spring 3.1.0 + Mybatis 3.1.0 + mybatis-spring 1.0.0 RC3 + DB2 9.5 + Tomcat 6.0.35
web工程名称: isap
配置文件:applicationContext.xml + isap-servlet.xml
先看配置信息:
applicationContext.xml
&?xml version=&1.0& encoding=&UTF-8&?&&
&beans xmlns=&http://www.springframework.org/schema/beans&&
&&& xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance&&&
&&& xmlns:tx=&http://www.springframework.org/schema/tx&&
&&& xmlns:context=&http://www.springframework.org/schema/context&&
&&& xsi:schemaLocation=&&&
&&&&&&&&& http://www.springframework.org/schema/beans&&
&&&&&&&&& http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&&
&&&&&&&&& http://www.springframework.org/schema/tx&&
&&&&&&&&& http://www.springframework.org/schema/tx/spring-tx-3.0.xsd&
&&&&&&&&& http://www.springframework.org/schema/context&&
&&&&&&&&& http://www.springframework.org/schema/context/spring-context-3.0.xsd&&&&&&
&&&&&&&&&&
&&&&&&&&&&&
&&& &!-- spring配置jndi --&&
&&& &bean id=&dataSource& class=&org.springframework.jndi.JndiObjectFactoryBean&&&
&&&&&&& &property name=&jndiName&&&
&&&&&&&&&&& &value&java:comp/env/jndi_isap&/value&&
&&&&&&& &/property&&
&&& &/bean&&&&&
&&& &bean id=&sqlSessionFactory& class=&org.mybatis.spring.SqlSessionFactoryBean&&&
&&&&&&& &property name=&configLocation& value=&classpath:config/mybatis-config.xml&&&/property&&
&&&&&&& &property name=&dataSource& ref=&dataSource&/&&
&&&&&&& &property name=&mapperLocations&&&
&&&&&&&&&&& &list&&
&&&&&&&&&&&&&&& &value&classpath:com/cosbulk/isap/*/dao/mapper/*Mapper.xml&/value&&
&&&&&&&&&&& &/list&&
&&&&&&& &/property&&
&&& &/bean&&
&&& &bean id=&transactionManager& class=&org.springframework.jdbc.datasource.DataSourceTransactionManager&&&
&&&&&&& &property name=&dataSource& ref=&dataSource& /&&
&&&&&&& &qualifier value=&isap&/&&
&&& &/bean&&&&&
&&& &!-- 如果是isap的表,用@Transactional(&isap&),如果是smis用 @Transactional(&smis&)管理事务--&&
&&& &tx:annotation-driven transaction-manager=&transactionManager& /&&&
&&& &bean class=&org.mybatis.spring.mapper.MapperScannerConfigurer&&&
&&&&&&& &property name=&sqlSessionFactory& ref=&sqlSessionFactory&/&&
&&&&&&& &property name=&basePackage& value=&com.cosbulk.isap&&&/property&&
&&& &/bean&&
&&& &!-- /////////////////////////////////////////////////////////////////////////// --&&
&&& &!-- smis数据源 --&&
&&& &bean id=&smisDataSource& class=&org.springframework.jndi.JndiObjectFactoryBean&&&
&&&&&&& &property name=&jndiName&&&
&&&&&&&&&&& &value&java:comp/env/jndi_smis&/value&&
&&&&&&& &/property&&
&&& &/bean&&&&&&&&
&&& &bean id=&smisSqlSessionFactory& class=&org.mybatis.spring.SqlSessionFactoryBean&&&
&&&&&&& &property name=&configLocation& value=&classpath:config/mybatis-config.xml&&&/property&&
&&&&&&& &property name=&dataSource& ref=&smisDataSource&/&&
&&&&&&& &property name=&mapperLocations&&&
&&&&&&&&&&& &list&&
&&&&&&&&&&& &value&classpath:com/cosbulk/smis/dao/mapper/*Mapper.xml&/value&&
&&&&&&&&&&& &/list&&
&&&&&&& &/property&&
&&& &/bean&&
&&& &bean id=&smisTransactionManager& class=&org.springframework.jdbc.datasource.DataSourceTransactionManager&&&
&&&&&&& &property name=&dataSource& ref=&smisDataSource& /&&
&&&&&&& &qualifier value=&smis&/&&
&&& &/bean&&&&&
&&& &!-- 如果是isap的表,用@Transactional(&isap&),如果是smis用 @Transactional(&smis&)管理事务--&&
&&& &tx:annotation-driven transaction-manager=&smisTransactionManager& /&&&
&&& &bean class=&org.mybatis.spring.mapper.MapperScannerConfigurer&&&
&&&&&&& &property name=&sqlSessionFactory& ref=&smisSqlSessionFactory&/&&
&&&&&&& &property name=&basePackage& value=&com.cosbulk.smis&&&/property&&
&&& &/bean&&&&&
在applicationContext.xml文件中,主要配置了数据源和dao接口以及mapper文件相关信息,其他的bean信息在isap-servlet.xml文件中定义。
配置文件: isap-servlet.xml
&?xml version=&1.0& encoding=&UTF-8&?&&
&beans xmlns=&http://www.springframework.org/schema/beans&&
&&& xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance& xmlns:p=&http://www.springframework.org/schema/p&&
&&& xmlns:context=&http://www.springframework.org/schema/context&&
&&& xmlns:mvc=&http://www.springframework.org/schema/mvc&&
&&& xsi:schemaLocation=&&&
&&&&&&&&&& http://www.springframework.org/schema/beans&&
&&&&&&&&&& http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&&
&&&&&&&&&& http://www.springframework.org/schema/context&&
&&&&&&&&&& http://www.springframework.org/schema/context/spring-context-3.0.xsd&
&&&&&&&&&& http://www.springframework.org/schema/mvc&&
&&&&&&&&&& http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd&&
&&& default-autowire=&byName&&&
&&& &span style=&color:#FF0000;&&&strong&&!--排除扫描的包, 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --&&
&&& &context:component-scan base-package=&com.cosbulk.isap&&&
&&&&&&& &context:exclude-filter type=&regex& expression=&com.cosbulk.isap.*.*.model&/&&
&&&&&&& &context:exclude-filter type=&regex& expression=&com.cosbulk.isap.*.*.dao.*&/&&
&&& &/context:component-scan&&
&&& &context:component-scan base-package=&com.cosbulk.smis&&&
&&&&&&& &context:exclude-filter type=&regex& expression=&com.cosbulk.smis.*.*.model&/&&
&&&&&&& &context:exclude-filter type=&regex& expression=&com.cosbulk.smis.*.*.dao.*&/&&
&&& &/context:component-scan&&/strong&&/span&&
&&& &mvc:annotation-driven/&&
&&& &!--添加拦截器,类级别的处理器映射 --&&
&&& &mvc:interceptors&&
&&&&&&& &bean class=&com.mon.interceptor.CheckLoginInterceptor& /&&
&&& &/mvc:interceptors&&
&&& &!-- 支持文件上传 --&&
&&& &bean id=&multipartResolver& class=&org.springframework.monsMultipartResolver&&&&&
&&&&&&& &property name=&defaultEncoding& value=&UTF-8& /&&
&&&&&&& &!-- &property name=&maxUploadSize& value=&5000000& /&& max size 5M --&&
&&& &/bean&&&
&&& &!-- 异常处理 --&&
&&& &bean id=&exceptionResolver& class=&org.springframework.web.servlet.handler.SimpleMappingExceptionResolver&&&
&&&&&&& &property name=&defaultErrorView&&&
&&&&&&&&&&& &value&errorPages/error&/value&&
&&&&&&& &/property&&
&&&&&&& &property name=&defaultStatusCode&&&
&&&&&&&&&&& &value&500&/value&&
&&&&&&& &/property&&
&&&&&&& &property name=&warnLogCategory&&&
&&&&&&&&&&& &value&org.springframework.web.servlet.handler.SimpleMappingExceptionResolver&/value&&
&&&&&&& &/property&&
&&&&&&& &property name=&exceptionMappings&&&&&
&&&&&&&&&&& &props&&&&
&&&&&&&&&&&&&&& &prop key=&org.springframework.web.multipart.MaxUploadSizeExceededException&&errorPages/maxUploadExceeded&/prop&&&&
&&&&&&&&&&& &/props&&&&&
&&&&&&& &/property&&
&&& &/bean&&
&&& &!-- 视图解析类 --&&
&&& &bean class=&org.springframework.web.servlet.view.InternalResourceViewResolver&&&
&&&&&&& &property name=&viewClass& value=&org.springframework.web.servlet.view.JstlView& /&&
&&&&&&& &property name=&prefix& value=&/view/& /&&
&&&&&&& &property name=&suffix& value=&.jsp& /&&
&&& &/bean&&
在这个配置文件里进行了所有的注解(@Controller、@Service等)扫描和mvc相关配置。
事务场景:
Service层通过注解@Transactional注入事务
@Service&&&
//默认将类中的所有函数纳入事务管理.&&&
@Transactional(&isap&)&&&
public class UserService{&&&
&&& @Autowired&&&
&&& private UserDao userD&&&
&&&& * 获取主键id&&
&&&& * @return&&
&&&& */&&&
&&& public Long getId() {&&&
&&&&&&& return userDao.getId();&&&
&&&&&&&&&&&&&
&&&&&&&&& /**&&
&&&& * 编辑用户&&
&&&& * @param user&&
&&&& * @param param&&
&&&& */&&&
&&& public void updateUser(User user,Map&String,Object& param) {&&&
&&&&&&& //编辑用户&&&
&&&&&&& userDao.updateUser(user);&&&
&&&&&&& //删除原有的用户角色关联&&&
&&&&&&& userDao.deleteUserRole(param);&&&
&&&&&&& String s =&&&
&&&&&&& s.getBytes();&&&
&&&&&&& //新增用户角色关联&&&
&&&&&&& insertUserRole(param);&&&
&&&&&&&& 这里用来测试事务回滚&&&
&&&&&&&& ...&&&
测试过几次updateUser的调用,事务就是不起作用的,后台log信息如下:
16:19:46,672 org.apache.mons.JakartaCommonsLoggingImpl: JDBC Connection [mons.dbcp.PoolableConnection@67aece] will not be managed by Spring&&&
16:19:46,672 org.apache.mons.JakartaCommonsLoggingImpl: SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19006c9] was not registered for synchronization because synchronization is not active&&&
16:19:46,687 org.apache.mons.JakartaCommonsLoggingImpl: Committing JDBC Connection [mons.dbcp.PoolableConnection@67aece]&&&
16:19:46,687 org.apache.mons.JakartaCommonsLoggingImpl: Closing no transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19006c9]&&&
16:19:46,687 org.springframework.jdbc.datasource.DataSourceUtils: Returning JDBC Connection to DataSource&&&
解决方案:
1、配置文件的问题吧? (见/topic/1123069)
1.root-context.xml&
&!-- 不扫描带有@Controller注解的类。因为这些类已经随容器启动时,在servlet-context中扫描过一遍了 --&&
&context:component-scan base-package=&com.kimho&&&
&context:exclude-filter type=&annotation& expression=&org.springframework.stereotype.Controller&/&&
&/context:component-scan&&
2、servlet-context.xml:&
&!-- 扫描业务组件,让spring不扫描带有@Service注解的类(留在root-context.xml中扫描@Service注解的类),防止事务失效 --&&
&context:component-scan base-package=&com.kimho&&&
&context:exclude-filter type=&annotation& expression=&org.springframework.stereotype.Service&/&&
&/context:component-scan&&&
我做了相应的修改,把isap-servlet里的bean扫描拆成了两部分,分别放入applicationContext.xml和isap-servlet.xml文件中。
applicationContext.xml中加入:
&!-- 扫描带有@Service注解的类。 --&&
&context:component-scan base-package=&com.cosbulk.isap&&&
&&& &context:include-filter type=&annotation& expression=&org.springframework.stereotype.Service& /&&
&/context:component-scan&&&
&context:component-scan base-package=&com.cosbulk.smis&&&
&&& &context:include-filter type=&annotation& expression=&org.springframework.stereotype.Service& /&&
&/context:component-scan&&
isap-servlet.xml文件中,扫描bean部分换成:
&!--排除扫描的包, 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --&&
&&& &context:component-scan base-package=&com.cosbulk.isap&&&
&&&&&&& &context:exclude-filter type=&annotation& expression=&org.springframework.stereotype.Service&/&&
&&& &/context:component-scan&&&
&&& &context:component-scan base-package=&com.cosbulk.smis&&&
&&&&&&& &context:exclude-filter type=&annotation& expression=&org.springframework.stereotype.Service&/&&
&&& &/context:component-scan&&
重启服务器测试,发现效果和之前一样,事务还是没起作用。开始以为是和包扫描的位置有关,于是把扫描的位置放到文件末尾,发现还是没有起作用。最后偶然一个想法clean了下tomcat,然后重启,居然事务有效了。
总结: 修改完配置文件后,clean下工程,重启加载新的配置文件。OK,问题解决。
2、如果按照你的步骤设置为ID为null的话,那么就需要捕获mybatis抛出的异常,然后在catch语句中抛出一个Exception,这个时候Spring容器的事务管理就会起作用,会回滚事务。
3、如果用mysql,数据库表你如果是自动建表,那么就需要把建表的Engine设置为InnoDB格式,自动建表的格式为:MyISAM,这中格式的是不支持事务管理的。
总结下: Spring在使用ContextLoadListener加载applicationContext.xml或其他名称的xml文件时,能进行数据源和相关事务注解的检查,启动事务特性。若在isap-servlet.xml文件中加载是,仅作为普通bean定义加载。所以一个良好的习惯就是,分层配置相关的bean。applicationContext.xml中配置数据库相关的bean(dao、service等), isap-servlet中配置m相关的bean(controller等)。
作者:walkerJong
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'spring+mybatis 事务不生效,求助各位大神_百度知道spring和mybatis事务配置不起作用,跪求高手指点 - 开源中国社区
当前访客身份:游客 [
当前位置:
&下面是配置文件applicationContext.
&?xml version=&1.0& encoding=&UTF-8&?& &beans xmlns=&&&
& xmlns:xsi=&&&&
& xmlns:aop=&&&
& xmlns:tx=&&&&
& xmlns:jdbc=&&&
& xmlns:context=&& &xsi:schemaLocation=&&&&&&&&&&
& &&&& & &&&&&
&&& &!-- jdbc属性文件读入 --& &&& &bean id=&propertyConfigurer& class=&org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&& &&&&&&& &property name=&locations&& &&&&&&&&&&& &list& &&&&&&&&&&&&&&& &value&classpath:jdbc.properties&/value& &&&&&&&&&&& &/list& &&&&&&& &/property& &&& &/bean& &&&&&
&&!-- 数据库连接池 --& &&bean id=&dataSource& class=&com.mchange.boPooledDataSource& destroy-method=&close&& &&&property name=&driverClass& value=&${jdbc.driver}& /& &&&property name=&jdbcUrl& value=&${jdbc.url}& /& &&&property name=&user& value=&${jdbc.username}& /& &&&property name=&password& value=&${jdbc.password}& /& && && &!--连接池中保留的最小连接数。--& &&& &property name=&minPoolSize& value=&5&/& &&&
&&& &!--连接池中保留的最大连接数。Default: 15 --& &&& &property name=&maxPoolSize& value=&30&/& &&&
&&& &!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --& &&& &property name=&initialPoolSize& value=&10&/& &&&
&&& &!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --& &&& &property name=&maxIdleTime& value=&10&/& &&&
&&& &!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --& &&& &property name=&acquireIncrement& value=&5&/& &&&
&&& &!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。 &&&&& 但由于预缓存的statements属于单个connection而不是整个连接池。所以设 &&&&& 置这个参数需要考虑到多方面的因素。如果maxStatements与maxStatementsPerConnection &&&&& 均为0,则缓存被关闭。Default: 0--& &&& &property name=&maxStatements& value=&0&/& &&&
&&& &!--每60秒检查所有连接池中的空闲连接。Default: 0 --& &&& &property name=&idleConnectionTestPeriod& value=&60&/& &&&
&&& &!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --& &&& &property name=&acquireRetryAttempts& value=&30&/& &&&
&&& &!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据 &&&&& 源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设 &&&&& 为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。Default: false--& &&& &property name=&breakAfterAcquireFailure& value=&true&/& &&&
&&& &!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection &&&&& 提交的时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable &&&&& 等方法来提升连接测试的性能。Default: false --& &&& &property name=&testConnectionOnCheckout& value=&false&/& &&/bean& & &&bean id=&sqlSessionFactory& class=&org.mybatis.spring.SqlSessionFactoryBean&& &&&property name=&dataSource& ref=&dataSource&&&/property& &&&property name=&configLocation& value=&classpath:mybatis-config.xml&/&&& &&/bean& & &&bean id=&sqlSession& &&class=&org.mybatis.spring.SqlSessionTemplate&& &&&constructor-arg index=&0& ref=&sqlSessionFactory& /& &&/bean& & &&bean name=&transactionManager& &&class=&org.springframework.jdbc.datasource.DataSourceTransactionManager&& &&&property name=&dataSource& ref=&dataSource&&&/property& &&/bean& & &&!-- 启用事务 --& &&tx:advice id=&txAdvice& transaction-manager=&transactionManager&& &&&tx:attributes& &&& &tx:method name=&save*&&& propagation=&REQUIRED&& rollback-for=&com.inspur.*.exception.CRUDException,Exception& /& &&&&&& &tx:method name=&create*& propagation=&REQUIRED&& rollback-for=&com.inspur.*.exception.CRUDException,Exception& /& &&&&&& &tx:method name=&copy*&&& propagation=&REQUIRED&& rollback-for=&com.inspur.*.exception.CRUDException,Exception& /& &&&&&& &tx:method name=&update*& propagation=&REQUIRED&& rollback-for=&com.inspur.*.exception.CRUDException,Exception& /& &&&&&& &tx:method name=&delete*& propagation=&REQUIRED&& rollback-for=&com.inspur.*.exception.CRUDException,Exception& /& &&&&&& &tx:method name=&find*&&& read-only=&true& rollback-for=&com.inspur.*.exception.CRUDException,Exception& /& &&&&&& &tx:method name=&list*&&& read-only=&true& rollback-for=&com.inspur.*.exception.CRUDException,Exception& /& &&&&&& &tx:method name=&is*& read-only=&true& rollback-for=&com.inspur.*.exception.CRUDException,Exception& /& &&&&&& &tx:method name=&*&& read-only=&true& rollback-for=&com.inspur.*.exception.CRUDException,Exception& /& &&&/tx:attributes& &&/tx:advice& & &&!-- AOP代理设置 --& &&aop:config& &&&&&&& &aop:advisor pointcut=&execution(* com.inspur.*.service.*.*(..))& advice-ref=&txAdvice& /& &&/aop:config&
&&!-- scan& mappers and let them be autowired --& &&bean class=&org.mybatis.spring.mapper.MapperScannerConfigurer&&&&&&&&
&&&property name=&basePackage& value=&com.inspur.courseSalarySys.persistence,com.inspur.framework.persistence& /& &&/bean& & & &!-- 激活annotation功能&&& 作用是式地向 Spring 容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 &&&&& PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。 &&&&&&&&&&&& 注册这4个 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。识别各种注解--& &&context:annotation-config /& &&context:spring-configured/& & &/beans&
在service中故意设置异常如下,不进行回滚,跪求指点!
&/** & * 保存 & *
course & *
CRUDException & */ &public void saveCourse(Course course) throws CRUDException { &&&& try{ &&&course.setId(Util.getUUID()); &&&&& courseMapper.saveCourse(course); &&&&& Course cr& = new Course(); &&&&& System.out.println(Integer.parseInt(&dsdfsf&)); &&&&& System.out.println(cr.getId().equals(&&)); &&&& }catch(Exception e){ &&&&log.error(e); &&&&throw new CRUDException(e); &&&& } &} &
共有8个答案
<span class="a_vote_num" id="a_vote_num_
看看是不是和我遇到同样问题
http://www.oschina.net/question/168
<span class="a_vote_num" id="a_vote_num_
& & @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = {RuntimeException.class, Exception.class})
& & @RequestMapping(value = &/address&, method = RequestMethod.POST)
& & public Object add(HttpServletRequest request) throws Exception {
& & & & ModelAndView modelAndView = new ModelAndView(model + &/add_result&);
& & & & RequestUtils req = new RequestUtils(request);
& & & & Address address = (Address) req.create(Address.class);
& & & & address.setCreateDate(new Date());
& & & & address.setLastUpDate(new Date());
& & & & if (Math.abs(address.getLatitude()) & 90 || Math.abs(address.getLongitude()) & 180) {
& & & & & & throw new MyException(ExpScheme.PARAM_ERR, &亲,你这是在火星上呢&);
& & & & User user = (User) request.getAttribute(&user&);
& & & & ResUserRel ru = new ResUserRel(address.getUuid(), user.getUuid(), ResUserRelType.CREATOR);
& & & & address.getResUserRels().add(ru);
& & & & address = (Address) resParser.parser(request, address);
& & & & int i = addressService.save(address);
& & & & if(true){
& & & & & throw new RuntimeException(&test&);
& & & & if (i == 1) {
& & & & & & modelAndView.addObject(&msg&, &成功&);
& & & & & & modelAndView.addObject(model, address);
& & & & & & modelAndView.addObject(&result&, ResultScheme.ACT_RESULT_SUCCESS);
& & & & & & return get(request, address.getUuid());
& & & & } else {
& & & & & & throw new MyException(&未知错误&);
<span class="a_vote_num" id="a_vote_num_
&?xml version=&1.0& encoding=&utf-8&?&
&beans xmlns=&http://www.springframework.org/schema/beans&
xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance&
xmlns:context=&http://www.springframework.org/schema/context&
xmlns:tx=&http://www.springframework.org/schema/tx&
xmlns:aop=&http://www.springframework.org/schema/aop&
xsi:schemaLocation=&http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd&&
&context:property-placeholder location=&classpath:oracle.properties&&&/context:property-placeholder&
&bean id=&sqlSessionFactory& class=&org.mybatis.spring.SqlSessionFactoryBean&&
&property name=&configLocation& value=&classpath:mybatis-config.xml&/&
&property name=&dataSource& ref=&dataSource&/&
&property name=&mapperLocations& value=&classpath:com/fishbone/**/mapper/oracle/*.xml&/&
&bean id=&sqlSessionTemplate& class=&org.mybatis.spring.SqlSessionTemplate&&
&constructor-arg index=&0& ref=&sqlSessionFactory&/&
&bean id=&transactionManager& class=&org.springframework.jdbc.datasource.DataSourceTransactionManager&&
&property name=&dataSource& ref=&dataSource&/&
&bean id=&txManager& class=&org.springframework.jdbc.datasource.DataSourceTransactionManager&&
&property name=&dataSource& ref=&dataSource&/&
&tx:annotation-driven transaction-manager=&transactionManager&/&
&!-- Transaction end --&
&aop:config&
&aop:pointcut id=&serviceOperation& expression=&execution(* com.fishbone..*.dao..*Service.*(..))&/&
&aop:advisor pointcut-ref=&serviceOperation& advice-ref=&txAdvice&/&
&/aop:config&
&tx:advice id=&txAdvice& transaction-manager=&txManager&&
&tx:attributes&
&tx:method name=&&/&
&tx:method name=&get*& read-only=&true&/&
&tx:method name=&*& propagation=&REQUIRED& rollback-for=&Throwable&/&
&/tx:attributes&
&/tx:advice&
&bean id=&dataSource& class=&com.alibaba.druid.pool.DruidDataSource& init-method=&init& destroy-method=&close&&
&property name=&driverClassName& value=&oracle.jdbc.OracleDriver&/&
&property name=&url& value=&jdbc:oracle:thin:@58.68.238.84:1521:sensky&/&
&property name=&username& value=&fbus&/&
&property name=&password& value=&fbus&/&
&property name=&maxActive& value=&20&/&
&property name=&initialSize& value=&1&/&
&property name=&maxWait& value=&60000&/&
&property name=&minIdle& value=&1&/&
&property name=&timeBetweenEvictionRunsMillis& value=&3000&/&
&property name=&minEvictableIdleTimeMillis& value=&300000&/&
&property name=&validationQuery& value=&SELECT 'x' FROM DUAL&/&
&property name=&testWhileIdle& value=&true&/&
&property name=&testOnBorrow& value=&false&/&
&property name=&testOnReturn& value=&false&/&
&property name=&poolPreparedStatements& value=&true&/&
&property name=&maxPoolPreparedStatementPerConnectionSize& value=&20&/&
&property name=&filters& value=&mergeStat&/&
&property name=&connectionProperties& value=&druid.stat.slowSqlMillis=5000& /&
&property name=&timeBetweenLogStatsMillis& value=&300000& /&
<span class="a_vote_num" id="a_vote_num_
你等等,我试一下。
<span class="a_vote_num" id="a_vote_num_
数据库是什么?
<span class="a_vote_num" id="a_vote_num_
&配置文件中这么写的的 &&tx:annotation-driven transaction-manager=&transactionManager&/&
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) &public void saveCourse(Course course) throws Exception { &&&& try{ &&&course.setId(Util.getUUID()); &&&&& courseMapper.saveCourse(course); &&&&& Course cr& = new Course(); &&&&& System.out.println(cr.getId().equals(&&)); &&&& }catch(Exception e){ &&&&log.error(e); &&&&throw new CRUDException(e); &&&& } &} 我故意有设置空指针异常,事务还是不起作用,找不到问题出在哪里,急
<span class="a_vote_num" id="a_vote_num_
我是没配置,在用的时候申明的。你这种没有用过,但看起来应该不像有问题。你可以多在网上找找例子对比一下。
--- 共有 2 条评论 ---
我按照上面的方式设置,发现事务还是不起作用,着急,能帮我看下嘛,按照评论下方写的
(2年前)&nbsp&
(2年前)&nbsp&
<span class="a_vote_num" id="a_vote_num_
& & &!-- Transaction start --&
& & &bean id=&txManager& class=&org.springframework.jdbc.datasource.DataSourceTransactionManager&&
& & & & &property name=&dataSource& ref=&dataSource&/&
& & &/bean&
& & &tx:annotation-driven transaction-manager=&transactionManager&/&
& & &!-- Transaction end --&
& & @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = {RuntimeException.class, Exception.class})
--- 共有 1 条评论 ---
你好?aop配置不可以吗
(2年前)&nbsp&
更多开发者职位上
有什么技术问题吗?
javaSir...的其它问题
类似的话题}

我要回帖

更多关于 求助大连律师事务所 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信