Spring MVC one Transaction control problem

Source: Internet
Author: User

Spring MVC was used as a control-layer framework in a recent project, but there was a puzzling question: transaction control.

Spring MVC's configuration file is named: Springmvc-servlet.xml, which reads as follows:


<?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" xmln S:mvc= "Http://www.springframework.org/schema/mvc" xmlns:p= "http://www.springframework.org/schema/p" Xsi:schema location= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsdhttp://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 "> <!--annotation-based configuration--><context:component-scan base-package=" Com.xtayfjpk.esb.console "/><!--start the Spring MVC Annotation feature, complete the request and annotation pojo mappings--<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>

the spring configuration file is named: Applicationcontext.xml and reads as follows:

<?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" xmln S: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/c Ontext 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 "> <!--data source--><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><!--Configure declarative transaction management (in annotation-based manner)-- <bean id= "Txmanager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" ><property Name= "DataSource" ref= "DataSource"/></bean><!--enable slicing programming--><aop:aspectj-autoproxy/><!-- Enable annotation--><context:annotation-config/><!--Note-driven transaction manager--><tx:annotation-driven transaction-Manager= "Txmanager"/></beans> 

I think everyone is familiar with both profiles, but the problem I have is that the transaction manager does not work, and when an exception occurs, the transaction is not rolled back as expected.
Very puzzled, check several times the configuration is not a problem, but the problem arises, it must be the self where the problem. Later, when the hand was instantiated out of the container and then called the business layer method, the transaction manager worked, and the transaction was rolled back normally when the exception occurred. It's strange why the transaction manager works when you call the business layer method manually, and it doesn't work when you call through the page, and then, after a step-by-step troubleshooting, contrast, when the

<tx:annotation-driven transaction-manager= "Txmanager"/>
When this configuration is moved from Applicationcontext.xml to the Springmvc-servlet.xml file, the transaction manager is invoked through the page, and when the container call is manually instantiated, the transaction manager does not work, and finally the two configuration files are added

<tx:annotation-driven transaction-manager= "Txmanager"/>, both modes of transaction manager are working correctly.

Because I am not very familiar with the detailed working mechanism of the spring transaction manager, my own guess is that there are a set of mechanisms for transaction management between different containers (but it is possible to use the same transaction manager object) and cannot be shared, even if there is an inheritance relationship between the two containers. So in each container configuration file to add <tx:annotation-driven transaction-manager= "Txmanager"/> This configuration (note-Driven transaction manager only)

Spring MVC one Transaction control problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.