Five ways of spring transaction configuration and transaction propagation-related __spring transaction configuration and transaction propagation correlation

Source: Internet
Author: User
Tags aop

The previous period has been a more in-depth study of spring's transactional configuration, which, although configured for spring's transaction configuration, has not been clearly understood. Through this learning to find spring's transaction configuration as long as the idea is clear, or better grasp.

Summarized as follows:

In the spring configuration file, there are always three components in the transaction configuration, namely the DataSource, TransactionManager, and proxy mechanisms, regardless of the configuration, the general change is only the agent mechanism of this part.

DataSource, TransactionManager These two parts only according to the data access mode changes, such as using hibernate for data access, DataSource is actually sessionfactory, The implementation of TransactionManager is Hibernatetransactionmanager.

The following figure:



Based on the different agent mechanism, the configuration of five spring transactions is summarized, and the configuration files are as follows:

The first way: Each bean has an agent

[HTML]  View plain copy? <?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.org/schema/context               http://www.springframework.org/schema/context/spring-context-2.5.xsd               http://www.springframework.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>             <!--  Defining the transaction manager (declarative transaction)  -->         <bean id = "TransactionManager"            class= " Org.springframework.orm.hibernate3.HibernateTransactionManager ">            <property name= "Sessionfactory"  ref= "Sessionfactory"  />       </bean>               <!--  configuration dao -->       <bean id= "Userdaotarget"  class= "Com.bluesky.spring.dao.UserDaoImpl" >           < Property name= "Sessionfactory"  ref= "Sessionfactory"  />        </bean>              <bean id= "Userdao "             class=" Org.springframework.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 " />            <!--  Configuration transaction Properties  -->              <property name= "Transactionattributes" >                  <props>                      <prop key= "*" >PROPAGATION_REQUIRED</prop>                </props>              </property>          </bean>     </beans>  

The second way: all beans share a proxy base class

[HTML]  View plain copy? <?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.org/schema/context               http://www.springframework.org/schema/context/spring-context-2.5.xsd               http://www.springframework.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>             <!--  Defining the transaction manager (declarative transaction)  -->         <bean id = "TransactionManager"            class= " Org.springframework.orm.hibernate3.HibernateTransactionManager ">            <property name= "Sessionfactory"  ref= "Sessionfactory"  />       </bean>               <bean id= "Transactionbase"                   class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean"                   lazy-init= "true"  abstract= "true" >             <!--  Configure the transaction manager  -->             <property  Name= "TransactionManager"  ref= "TransactionManager"  />             <!--  Configuration transaction Properties  -->              <property name= "Transactionattributes" >                  <props>                       <prop key= "*" >PROPAGATION_REQUIRED</prop>                  </props>              </property>         </bean>                  <!--  Configure DAO  -->       <bean id= "Userdaotarget"  class= " Com.bluesky.spring.dao.UserDaoImpl ">           <property name=" Sessionfactory "  ref= "Sessionfactory"  />       </bean>               <bean id= "Userdao"  parent= "Transactionbase"   >             <property name= "Target"  ref= "Userdaotarget"  />          </bean>    </beans>  

The third way: using interceptors

[HTML] view plain copy? <

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.