在最近的一个项目中,采用springMVC、mybatis,发现一个很恼人的问题:事务管理不起作用!!网上查阅了大量的资料,尝试了各种解决办法,亦未能解决问题!
spring版本:3.0.5
mybatis版本:3.2.2
1、applicationContext.xml配置:
- <context:annotation-config />
- <!-- 引入jdbc属性配置文件 -->
- <context:property-placeholder location="classpath:jdbc.properties" />
- <!-- 数据源 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="${jdbc.driverClassName}"></property>
- <property name="url" value="${jdbc.url}"></property>
- <property name="username" value="${jdbc.username}"></property>
- <property name="password" value="${jdbc.password}"></property>
- </bean>
- <bean id="ftpConfig" class="com.wtas.utils.CustomizedPropertyConfig">
- <property name="location" value="classpath:ftp.properties" />
- </bean>
- <!-- 配置mybatis用到的sqlSessionFactory -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="configLocation" value="classpath:mybatisConfig.xml" />
- <property name="dataSource" ref="dataSource" />
- <property name="typeAliasesPackage" value="com.wtas.study.domain,com.wtas.studygroup.domain,com.wtas.sys.domain,com.wtas.test.domain" />
- <property name="mapperLocations" value="classpath*:com/wtas/**/*Mapper.xml" />
- </bean>
- <!-- 根据映射文件动态生成dao的实现 -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.wtas" />
- <property name="annotationClass" value="org.springframework.stereotype.Repository" />
- </bean>
- <!-- 事务控制 -->
- <bean name="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <tx:advice id="userTxAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="remove*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException" />
- <tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException" />
- <tx:method name="add*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" />
- <tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" />
- <tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" />
- <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
- <tx:method name="publish*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
- <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
- <tx:method name="list*" propagation="SUPPORTS" read-only="true" />
- <tx:method name="query*" propagation="SUPPORTS" read-only="true" />
- <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
- <tx:method name="page*" propagation="SUPPORTS" read-only="true" />
- </tx:attributes>
- </tx:advice>
- <tx:annotation-driven transaction-manager="transactionManager"/>
- <!-- execution(public * com.wtas.*.service.*.*(..)) OR execution(public * com.wtas.*.*.*.service.*.*(..))-->
- <aop:config>
- <aop:pointcut id="pc" expression="execution(public * com.wtas.*.service.*.*(..))" />
- <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
- </aop:config>
2、springMVC.xml配置:
- <!-- 这里注册转换器 -->
- <mvc:annotation-driven conversion-service="conversionService" />
- <context:component-scan base-package="com.wtas"></context:component-scan>
- <mvc:default-servlet-handler />
- <!-- 注册ConversionService -->
- <bean id="conversionService"
- class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
- <!-- 转换器 -->
- <property name="converters">
- <list>
- <bean class="com.wtas.converter.StringToDateConverter">
- <constructor-arg value="yyyy-MM-dd" />
- </bean>
- </list>
- </property>
- </bean>
- <bean
- class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <property name="messageConverters">
- <list>
- <bean
- class="org.springframework.http.converter.BufferedImageHttpMessageConverter" />
- <bean
- class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
- <bean
- class="org.springframework.http.converter.StringHttpMessageConverter" />
- <bean
- class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
- </list>
- </property>
- </bean>
- <!-- 视图解释类 -->
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsps/" />
- <property name="suffix" value=".jsp" />
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView" />
- </bean>
由于之前没用过springMVC,对其原理一知半解,一直在考虑是不是aop配置出错等等无关问题,结果破费周折,未能解决!
今天,偶然间看到一篇文章,颇有启发:
- 由于采用的是SpringMVC、 MyBatis,故统一采用了标注来声明Service、Controller
- <span style="color:#FF0000;"><strong>由于服务器启动时的加载配置文件的顺序为web.xml---root-context.xml(Spring的配置文件)---servlet-context.xml(SpringMVC的配置文件),由于
- root-context.xml配置文件中Controller会先进行扫描装配,但是此时service还没有进行事务增强处理,得到的将是原样的Service(没有经过事务加强处理,
- 故而没有事务处理能力)</strong></span>,所以我们必须在root-context.xml中不扫描Controller,配置如下:
- <!-- 自动扫描组件,这里要把controler下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。 -->
- <context:component-scan base-package="com.sence">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
- </context:component-scan>
- 在servlet-context.xml中扫描Controller同时不扫描Service,配置如下:
- <!-- 扫描所有的controller 但是不扫描service-->
- <context:component-scan base-package="com.sence">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
- </context:component-scan>
按照上述,修改了配置文件,将service的扫描放在applicationContext.xml中,controller的扫描放在springMVC.xml中,
applicationContext.xml中添加:
- <context:component-scan base-package="com.wtas">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
- </context:component-scan>
springMVC.xml中修改:
- <!-- 扫描所有的controller(@Controller注解) 但是不扫描service(@Service注解,如果你在java类上使用的是@Component,下面的org.springframework.stereotype.Service要改为org.springframework.stereotype.Component,
- 但是这样就有一个问题,因为Component已经包含了Controller,这样的话,也就是将Controller也排除了,所以我们在定义组件的时候尽量不要使用Component,而要使用Service)-->
- <context:component-scan base-package="com.wtas">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
- </context:component-scan>
重启服务器,调用事务测试方法,结果能够成功启动事务并在出错时回滚,配置完成。
- 2013-07-24 10:28:59.028 [http-80-1] DEBUG c.w.s.dao.IStudyGroupDao.add [BaseJdbcLogger.java:132] -==> Preparing: INSERT INTO t_study_group VALUES
- (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
- 2013-07-24 10:28:59.029 [http-80-1] DEBUG c.w.s.dao.IStudyGroupDao.add [BaseJdbcLogger.java:132] -==> Parameters: null, xxx(String), null, 1(Long),
- 2013-07-24 10:28:59.024(Timestamp), (String), null, null, null, null, 30(Integer), null
- 2013-07-24 10:28:59.031 [http-80-1] DEBUG org.mybatis.spring.SqlSessionUtils [SqlSessionUtils.java:165] -Releasing transactional SqlSession
- [org.apache.ibatis.session.defaults.DefaultSqlSession@d30557]
- 2013-07-24 10:28:59.083 [http-80-1] DEBUG org.mybatis.spring.SqlSessionUtils [SqlSessionUtils.java:283] -Transaction synchronization rolling back
- SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d30557]
- 2013-07-24 10:28:59.084 [http-80-1] DEBUG org.mybatis.spring.SqlSessionUtils [SqlSessionUtils.java:298] -Transaction synchronization closing
- SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d30557]
- 2013-07-24 10:28:59.085 [http-80-1] ERROR c.wtas.studygroup.domain.StudyGroup [StudyGroupAction.java:342] -edit study group error :
- java.lang.ArithmeticException: / by zero
- 顶
- 0
- 踩
- 0