Spring MVC and MyBatis transaction integration

Source: Internet
Author: User
Tags aop rollback thread xmlns

Previously, the company used MyBatis, but the transaction management was a transaction managed with the EJB's CMT container. The rationale is that the EJB request comes in, and the business code creates a MyBatis session and then puts it into the current thread, after which all the method operations involve the database from the current thread. When all service layer code is completed, when exiting the EJB, depending on whether there is an exception to decide whether to rollback the transaction, which is done by the interceptor (only set rollback to true on the transaction state entity when fallback), and when the entire EJB exits, the container then finally commits or rolls back the transaction based on the token.

Compared to the EJB transaction used by the company, a request for a transaction, some scenarios are not very flexible, but also must be used to support the EJB container, we use JBoss.

These days, MyBatis is combined with mybatis-spring to manage the transaction with spring. found that in the integration process, encountered a transaction does not work. Here's the record.

Organize the steps to gather as follows:

1. The following jars need to be introduced into Maven first:

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>

Spring-related jars, with a version of 3.0.0.RELEASE


2. Then the transaction file needs to be configured, where the service layer is the transaction agent, and all the bean definitions in addition to the spring MVC controller are placed in: Applicationcontext.xml, 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:tx= "Http://www.springframework.org/schema/tx"
xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
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/tx
Http://www.springframework.org/schema/tx/spring-tx-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/context
Http://www.springframework.org/schema/context/spring-context-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.video.*" >
<context:exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/>
</context:component-scan>

<tx:annotation-driven transaction-manager= "TransactionManager"/>

<context:property-placeholder location= "Classpath:mysql.properties"/>

<!--This database needs to refer to the Apache commons-dbcp Jar--
<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" >
<property name= "Driverclassname" value= "${jdbc.driverclassname}"/>
<property name= "url" value= "${jdbc.url}"/>
<property name= "username" value= "${username}"/>
<property name= "Password" value= "${password}"/>
</bean>

<!--transaction Manager--
<bean id= "TransactionManager"
class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "DataSource"/>
</bean>

<!--sqlsessionfactory DataSource must be the same as the datasource above TransactionManager-
<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<property name= "DataSource" ref= "DataSource"/>
<property name= "mapperlocations" value= "Classpath*:com/video/mapper/**/*.xml"/>
</bean>

<!--Create a sqlsession instance that is thread-safe and can be shared across all DAO instances, with the principle of sqlsession, and the transaction hooking up with the current thread.
<bean id= "sqlsession" class= "Org.mybatis.spring.SqlSessionTemplate" >
<constructor-arg index= "0" ref= "sqlsessionfactory"/>
</bean>

<!--create a DAO layer and inject a sqlsession--
<bean id= "Userdao" class= "Com.video.dao.UserDAOImpl" >
<property name= "sqlsession" ref= "sqlsession"/>
</bean>

</beans>


Here you need to pay special attention to the configuration:

<context:component-scan base-package= "com.video.*" >
<context:exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/>
</context:component-scan>

<tx:annotation-driven transaction-manager= "TransactionManager"/>

Tx:annotation-driver, this mark, is mainly used here is the annotation transaction, this open after spring will be in the "Com.video." All the following classes scan the identity containing @transactional, and generate the appropriate proxy (default is the interface-based JDK dynamic agent), or you can add proxy-target-class= "true" to use the Cglib class proxy. You need to add the Cglib jar.

<context:exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/>

This tag is a sign that does not scan spring MVC-related controllers

Scan generation of spring MVC-related controller instances, processed when the content of Webrequest-servlet.xml is loaded when the Web container starts.

The following is the Webrequest-servlet.xml file for the Spring MVC component definition

3.webrequest-servlet.xml Spring MVC component files

<?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:tx= "Http://www.springframework.org/schema/tx"
Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:context= "Http://www.springframework.org/schema/context"
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/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
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd
">

<!--enable Spring MVC annotations--
<context:annotation-config/>

<!--set the jar package where the annotated class is located, you must exclude components that scan the service layer--
<context:component-scan base-package= "com.video.*" >
<context:exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Service"/>
</context:component-scan>

<!--complete the request and annotation pojo mapping--
<bean
class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<!--The path resolution to the turn page. Prefix: prefix, suffix: suffix---
<bean
Class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix= "/jsp/" p:suffix= ". jsp"/>

</beans>

Note here:

<context:component-scan base-package= "com.video.*" >
<context:exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Service"/>
</context:component-scan>

The bean that scanned the @service was excluded. If not excluded, this webrequest-servlet.xml file is loaded first when the Web is started, and a service instance without a transaction is injected into the controller.


All the configuration has been completed, note the above mentioned scan troubleshooting issues.

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.