MyBatis and SPRINGMVC integration transactions are valid under JUnit tests but are not valid in the actual project

Source: Internet
Author: User

I. Description of the problem

Project Framework uses SSM, integrated transaction rollback (see below), in unit testing, test transactions are valid, but when the actual project on-line, but there is no effect.

Second, the Integration method

Application-mybatis.xml (The following XML masks some irrelevant details)

    <!--Data Connection Pool -    <BeanID= "DataSource"class= "Com.alibaba.druid.pool.DruidDataSource">        < Propertyname= "Driverclassname"value= "${jdbc.driver.dev}"></ Property>        < Propertyname= "url"value= "${jdbc.url.dev}"></ Property>        < Propertyname= "username"value= "${jdbc.user}"></ Property>        < Propertyname= "Password"value= "${jdbc.password}"></ Property>    </Bean>    <!--transaction manager -    <BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager">        < Propertyname= "DataSource"ref= "DataSource"></ Property>    </Bean>    <!--Transaction Configuration 1: Manual annotation Required -    <!--Proxy-traget-class True to proxy the class and, if False, to proxy the interface, you need to add @Transactional annotations to the class or method when used.  -    <Tx:annotation-drivenTransaction-manager= "TransactionManager"Proxy-target-class= "true" />

Application-common.xml (the key is to have Spring management exclude controller parts)

    <!--All packages under Com.mc.bsframe are automatically scanned, including the classes under the sub-package except @controller.  -    <Scpan:component-scanBase-package= "Com.mc.bsframe">        <Scpan:exclude-filtertype= "Annotation"expression= "Org.springframework.stereotype.Controller" />        <Scpan:exclude-filtertype= "Annotation"expression= "Org.springframework.web.bind.annotation.ControllerAdvice" />    </Scpan:component-scan>

Spring-mvc.xml (the key is to handle only the controller part)

    <!--scans only classes that are annotated with a controller under Base-package.  -    <Context:component-scanBase-package= "Com.mc.bsframe.controller">        <Context:include-filtertype= "Annotation"expression= "Org.springframework.stereotype.Controller" />        <!--You must include Controlleradvice to handle global exceptions.  -        <Context:include-filtertype= "Annotation"expression= "Org.springframework.web.bind.annotation.ControllerAdvice" />    </Context:component-scan>

Basic about the configuration of the transaction as above, but I found that occasionally there will be a failure of the situation,

Third, analysis

Why JUNIT4 test is effective, guess because JUNIT4 is created under a context object, and the actual project is a spring context, a SPRINGMVC?

Iv. Methods of Solution

To add a configuration that excludes scan service in Spring-mvc.xml, the previous statement simply contains the controller and Controlleradvice, as follows:

    <!--scans only classes that are annotated with a controller under Base-package.  -    <Context:component-scanBase-package= "Com.mc.bsframe.controller">        <Context:include-filtertype= "Annotation"expression= "Org.springframework.stereotype.Controller" />        <!--You must include Controlleradvice to handle global exceptions.  -        <Context:include-filtertype= "Annotation"expression= "Org.springframework.web.bind.annotation.ControllerAdvice" />        <!-- !!! It is best to add this to allow SPRINGMVC to rule out the service layer and avoid the problem of transaction failure.  -        <Context:exclude-filtertype= "Annotation"expression= "Org.springframework.stereotype.Service" />    </Context:component-scan>

MyBatis and SPRINGMVC integration transactions are valid under JUnit tests but are not valid in the actual project

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.