歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring MVC一事務控制問題

Spring MVC一事務控制問題

日期:2017/3/1 9:14:05   编辑:Linux編程

在近期一個項目中用了Spring MVC作為控制層框架,但卻出現了一個讓人非常費解的問題:事務控制。

Spring MVC的配置文件名稱為:springMVC-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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
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/aop
http://www.springframework.org/schema/aop/spring-aop-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- 基於注解的配置 -->
<context:component-scan base-package="com.xtayfjpk.esb.console"/>
<!-- 啟動Spring MVC的注解功能。完畢請求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/app/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
</beans>

Spring配置文件名稱為: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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
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/aop
http://www.springframework.org/schema/aop/spring-aop-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/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">


<!-- 數據源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/esb_center?useUnicode=true&charset=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="xtayfjpk"/>
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean>


<!-- 配置聲明式的事務管理(採用基於注解的方式) -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 啟用切面編程 -->
<aop:aspectj-autoproxy />
<!-- 啟用注解 -->
<context:annotation-config/>
<!-- 注解驅動的事務管理器 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>

我想大家對於這兩個配置文件都非常熟悉。但我遇到的問題是事務管理器並沒有起作用,當發生異常時沒有按預期進行事務回滾。
非常納悶,檢查了好幾遍配置都沒有問題,但問題出現了,那肯定是自已哪裡出了問題。

後來手動用代碼實例化出容器再調用業務層方法時,事務管理器卻起作用了。異常時事務正常回滾。這就奇怪了,為什麼手動調用業務層方法時。事務管理器起作用,而通過頁面調用時去不起作用了呢,後經一步步排查,對照,當把


<tx:annotation-driven transaction-manager="txManager"/>
這一配置從applicationContext.xml移至springMVC-servlet.xml文件時,通過頁面調用事務管理器起作用了。而手動實例化容器調用時。事務管理器不起作用了,最後在這兩個配置文件裡都加上

<tx:annotation-driven transaction-manager="txManager"/>時,兩種方式事務管理器都正常工作了。

由於本人暫對Spring事務管理器的具體工作機制不是非常了解,但我自己推測是不同容器之間在事務管理上有各自的一套機制(但可能使用同一個事務管理器對象),是不能共享的,即使兩個容器之間存在著繼承關系。所以在每個容器的配置文件裡都要加上<tx:annotation-driven transaction-manager="txManager"/>這一配置(僅限於注解驅動的事務管理器)

Spring MVC+Spring3+Hibernate4開發環境搭建 http://www.linuxidc.com/Linux/2013-07/87119.htm

Spring MVC整合Freemarker基於注解方式 http://www.linuxidc.com/Linux/2013-02/79660.htm

基於注解的Spring MVC簡單介紹 http://www.linuxidc.com/Linux/2012-02/54896.htm

Spring MVC 框架搭建及詳解 http://www.linuxidc.com/Linux/2012-01/52740.htm

Spring MVC使用Cron表達式的定時器 http://www.linuxidc.com/Linux/2014-12/110733.htm

簡單的Spring MVC經典案例 http://www.linuxidc.com/Linux/2016-04/129718.htm

Copyright © Linux教程網 All Rights Reserved