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 very confusing question: transaction control.

The configuration file name for Spring MVC is called: Springmvc-servlet.xml, the contents are 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.spring Framework.org/schema/aop "xmlns:tx=" Http://www.springframework.org/schema/tx "xmlns:mvc=" Http://www.springfram Ework.org/schema/mvc "xmlns:p=" http://www.springframework.org/schema/p "xsi:schemalocation=" HTTP://WWW.SPRINGF Ramework.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 "/><!-- Starts the annotation feature for spring MVC. Complete the request and annotation Pojo map--<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 profile name is called: Applicationcontext.xml, for example, the following:

<?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 (using annotation-based approach)-- <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 you are very familiar with both of these profiles. However, the problem I encountered was that the transaction manager did not work, and the transaction rollback did not occur as expected when an exception occurred.
Very puzzled, checked several times the configuration is not a problem, but the problem arises, it must be the self where the problem.

The transaction manager then worked when the hand was instantiated out of the container and then called the business layer method. The transaction is normally rolled back when the exception occurs. It's strange why the business layer method is invoked manually. The transaction manager works, and when called through the page does not work, after a step by step to check, control, 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. When you manually instantiate a container call. The transaction manager does not work, and finally in both of these configuration files are added

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

Since I am not very aware of the specific working mechanism of the spring transaction manager, I have myself been able to extrapolate that there is a separate 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.