日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring MVC一事务控制问题

發(fā)布時間:2024/4/17 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring MVC一事务控制问题 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在近期一個項目中用了Spring MVC作為控制層框架,但卻出現(xiàn)了一個讓人非常費解的問題:事務(wù)控制。

Spring MVC的配置文件名稱為:springMVC-servlet.xml,內(nèi)容例如以下:


<?

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,內(nèi)容例如以下:

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-3.0.xsd"><!-- 數(shù)據(jù)源 --><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><!-- 配置聲明式的事務(wù)管理(採用基于注解的方式) --><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!-- 啟用切面編程 --><aop:aspectj-autoproxy /><!-- 啟用注解 --><context:annotation-config/><!-- 注解驅(qū)動的事務(wù)管理器 --><tx:annotation-driven transaction-manager="txManager"/> </beans>
我想大家對于這兩個配置文件都非常熟悉。但我遇到的問題是事務(wù)管理器并沒有起作用,當(dāng)發(fā)生異常時沒有按預(yù)期進(jìn)行事務(wù)回滾。
非常納悶,檢查了好幾遍配置都沒有問題,但問題出現(xiàn)了,那肯定是自已哪里出了問題。

后來手動用代碼實例化出容器再調(diào)用業(yè)務(wù)層方法時,事務(wù)管理器卻起作用了。異常時事務(wù)正常回滾。這就奇怪了,為什么手動調(diào)用業(yè)務(wù)層方法時。事務(wù)管理器起作用,而通過頁面調(diào)用時去不起作用了呢,后經(jīng)一步步排查,對照,當(dāng)把

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

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

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

轉(zhuǎn)載于:https://www.cnblogs.com/hrhguanli/p/5079810.html

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的Spring MVC一事务控制问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。