Spring transaction SPI and configuration introduction

Source: Internet
Author: User

The abstraction of spring transaction management, the core of three interfaces: Platformtransactionmanager, Transactiondefinition, and Transactionstatus. The relationship looks like this:


transactiondefinition: defines spring-compatible transaction properties, including: Transaction isolation level, transaction propagation behavior, timeout duration, read-only status;

Transactionstatus: represents the specific operational state of a transaction. The transaction manager obtains the state information of the transaction running through the interface, and can also rollback the transaction through it.

Platformtransactionmanager: The top-level interface of the transaction manager, as shown in its common implementation class:


The spring configuration file about transaction configuration is always made up of three components, namely DataSource,TransactionManager , and proxy mechanisms , regardless of the configuration method, The general change is only part of the agency mechanism.

DataSource, TransactionManager These two parts only according to the data access way change, for example uses Hibernate to carry on the data access, DataSource actually is sessionfactory, The implementation of TransactionManager is Hibernatetransactionmanager.


According to the different agent mechanism, the configuration of five kinds of spring transactions is summarized, and the configuration file is as follows:

The first way: Each bean has a proxy

<?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" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.or G/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd Http://www.sprin GFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <bean id=" Sessionfactory "class=" Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> <property Name= "Configlocation" value= "Classpath:hibernate.cfg.xml"/> <property name= "configurationclass" value= "org . hibernate.cfg.AnnotationConfiguration "/> </bean&Gt <!--define the transaction manager (declarative transaction)--<bean id= "TransactionManager" class= "Org.springframework.orm.hibernate3.Hibe        Rnatetransactionmanager "> <property name=" sessionfactory "ref=" Sessionfactory "/> </bean> <!--Configuring DAO--<bean id= "Userdaotarget" class= "Com.bluesky.spring.dao.UserDaoImpl" > <property nam E= "Sessionfactory" ref= "sessionfactory"/> </bean> <bean id= "Userdao" class= "Org.springfram  Ework.transaction.interceptor.TransactionProxyFactoryBean "> <!--configuration transaction Manager-<property            Name= "TransactionManager" ref= "TransactionManager"/> <property name= "target" ref= "Userdaotarget"/> <property name= "proxyinterfaces" value= "Com.bluesky.spring.dao.GeneratorDao"/> <!--Configure transaction Properties--&          Gt <property name= "Transactionattributes" > <props> <prop key= "*" >propagation_ REquired</prop> </props> </property> </bean> </beans> 

Second way: All beans share a proxy base class

<?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" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.or G/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd Http://www.sprin GFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <bean id=" Sessionfactory "class=" Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> <property Name= "Configlocation" value= "Classpath:hibernate.cfg.xml"/> <property name= "configurationclass" value= "org . hibernate.cfg.AnnotationConfiguration "/> </bean&Gt <!--define the transaction manager (declarative transaction)--<bean id= "TransactionManager" class= "Org.springframework.orm.hibernate3.Hibe        Rnatetransactionmanager "> <property name=" sessionfactory "ref=" Sessionfactory "/> </bean>               <bean id= "Transactionbase" class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean" Lazy-init= "true" abstract= "true" > <!--Configuration transaction Manager--<property name= "transaction Manager "ref=" TransactionManager "/> <!--Configure transaction Properties--<property name=" Transactionattributes "&              Gt          <props> <prop key= "*" >PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!--Configuring DAO--<bean id= "Userdaotarget" class= "Com.bluesky.spri Ng.dao.UserDaoImpl "> <property name=" sessionfactory "ref=" sessionfactory "/> </bean> < Bean id= "Userdao" parent= "Transactionbase" > <property name= "target" ref= "Userdaotarget"/> </ Bean></beans>

Third Way: Using interceptors

<?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" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.or G/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd Http://www.sprin GFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <bean id=" Sessionfactory "class=" Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> <property Name= "Configlocation" value= "Classpath:hibernate.cfg.xml"/> <property name= "configurationclass" value= "org . hibernate.cfg.AnnotationConfiguration "/> </bean&Gt <!--define the transaction manager (declarative transaction)--<bean id= "TransactionManager" class= "Org.springframework.orm.hibernate3.Hibe        Rnatetransactionmanager "> <property name=" sessionfactory "ref=" Sessionfactory "/> </bean> <bean id= "Transactioninterceptor" class= "Org.springframework.transaction.interceptor.TransactionInterceptor" &          Gt  <property name= "TransactionManager" ref= "TransactionManager"/> <!--Configuring transaction Properties--<property Name= "Transactionattributes" > <props> <prop key= "*" >PROPAGATION_REQUIRED< /prop> </props> </property> </bean> <bean class= "Org.springfra Mework.aop.framework.autoproxy.BeanNameAutoProxyCreator "> <property name=" beannames "> <l ist> <value>*Dao</value> </list> </property> < ProPerty name= "Interceptornames" > <list> <value>transactioninterceptor</value > </list> </property> </bean> <!--Configuring DAO--<bean id= " Userdao "class=" Com.bluesky.spring.dao.UserDaoImpl "> <property name=" sessionfactory "ref=" Sessionfactory "/&G    T </bean></beans>

Fourth way: Interceptors configured with the TX tag

<?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" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring- Beans-2.5.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/cont Ext/spring-context-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/s Pring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2. 5.xsd "> <context:annotation-config/> <context:component-scan base-package=" Com.bluesky "/> <be An id= "sessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> <property name=" configlocation "value=" Classpa Th:hibernate.cfg.xml "/> <property name=" ConfigurationClass "value=" Org.hibernate.cfg.AnnotationConfigurati On "/> </bean> <!--define transaction manager (declarative transaction)--<bean id=" TransactionManager "class=" Org.spr Ingframework.orm.hibernate3.HibernateTransactionManager "> <property name=" sessionfactory "ref="        Sessionfactory "/> </bean> <tx:advice id=" Txadvice "transaction-manager=" TransactionManager "> <tx:attributes> <tx:method name= "*" propagation= "REQUIRED"/> </tx:attributes> < /tx:advice> <aop:config> <aop:pointcut id= "interceptorpointcuts" expression= "Executio N (* com.bluesky.spring.dao.*.* (..)) "/> <aop:advisor advice-ref=" Txadvice "pointcut-ref=" intercept Orpointcuts "/> </aop:config> </beans> 

<span style= "Font-size:18px;color: #cc33cc;" ><strong> Fifth way: Full annotation </strong></span>

 
 

<?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" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring- Beans-2.5.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/cont Ext/spring-context-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/s Pring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2. 5.xsd "> <context:annotation-config/> <context:component-scan base-package=" Com.bluesky "/> <tx : Annotation-driven transaction-manager= "traNsactionmanager "/> <bean id=" sessionfactory "class=" Org.springframework.orm.hibernate3.LocalSessionF Actorybean "> <property name=" configlocation "value=" Classpath:hibernate.cfg.xml "/> <propert Y name= "ConfigurationClass" value= "org.hibernate.cfg.AnnotationConfiguration"/> </bean> <!--define the transaction manager ( declarative transactions)--<bean id= "TransactionManager" class= "org.springframework.orm.hibernate3.HibernateTransaction Manager "> <property name=" sessionfactory "ref=" sessionfactory "/> </bean> </beans>

At this point, you need to add @transactional annotations on DAO, as follows:

@Transactional @component ("Userdao") public class Userdaoimpl extends Hibernatedaosupport implements Userdao {    Public list<user> listUsers () {        return this.getsession (). CreateQuery ("from User"). List ();    }        }


Configuration reference: http://www.blogjava.net/robbie/archive/2009/04/05/264003.html

Spring transaction SPI and configuration introduction

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.