Spring transaction SPI and configuration introduction, spring transaction spi

Source: Internet
Author: User

Spring transaction SPI and configuration introduction, spring transaction spi

Abstract of Spring transaction management. Three core interfaces are PlatformTransactionManager, TransactionDefinition, and TransactionStatus. Shows the link:


TransactionDefinition:Defines Spring-compatible transaction attributes, including transaction isolation level, transaction Propagation Behavior, timeout duration, and read-only status;

TransactionStatus:Represents the specific running status of a transaction. The transaction manager obtains the status information of the transaction running through this interface, and can also roll back the transaction through it;

PlatformTransactionManager: The top-level interface of the Transaction Manager. Its Common Implementation classes are shown in:


The transaction configuration in the Spring configuration file is always composed of three parts:DataSource,TransactionManagerAndProxy mechanismRegardless of the configuration method, the agent mechanism is usually changed.

DataSource and TransactionManager only change according to the data access method. For example, when Hibernate is used for data access, DataSource is actually SessionFactory, and the implementation of TransactionManager is HibernateTransactionManager.


According to different proxy mechanisms, Five Spring transaction configuration methods are summarized. The configuration file is as follows:

Method 1: 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.org/schema/conte Xt 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. c Fg. AnnotationConfiguration "/> </bean> <! -- Define the Transaction Manager (declarative transaction) --> <bean id = "transactionManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager "> <property name =" sessionFactory "ref =" sessionFactory "/> </bean> <! -- Configure 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 "> <! -- Configure the Transaction Manager --> <property name = "transactionManager" ref = "transactionManager"/> <property name = "target" ref = "userDaoTarget"/> <property name = "proxyInterfaces "value =" com. bluesky. spring. dao. generatorDao "/> <! -- Configure transaction properties --> <property name = "transactionAttributes"> <props> <prop key = "*"> PROPAGATION_REQUIRED </prop> </props> </property> </ bean> </beans>

Method 2: 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.org/schema/conte Xt 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. c Fg. AnnotationConfiguration "/> </bean> <! -- Define 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"/> <! -- Configure 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>

Method 3: Use interceptor

<? 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/conte Xt 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. c Fg. AnnotationConfiguration "/> </bean> <! -- Define the Transaction Manager (declarative transaction) --> <bean id = "transactionManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager "> <property name =" sessionFactory "ref =" sessionFactory "/> </bean> <bean id =" transactionInterceptor "class =" org. springframework. transaction. interceptor. transactionInterceptor "> <property name =" transactionManager "ref =" transactionManager "/> <! -- Configure transaction properties --> <property name = "transactionAttributes"> <props> <prop key = "*"> PROPAGATION_REQUIRED </prop> </props> </property> </ bean> <bean class = "org. springframework. aop. framework. autoproxy. beanNameAutoProxyCreator "> <property name =" beanNames "> <list> <value> * Dao </value> </list> </property> <property name =" interceptorNames "> <list> <value> transactionInterceptor </value> </list> </property> </bean> <! -- Configure DAO --> <bean id = "userDao" class = "com. bluesky. spring. dao. userDaoImpl "> <property name =" sessionFactory "ref =" sessionFactory "/> </bean> </beans>

Method 4: Use the interceptor 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/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-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 "/> <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> <! -- Define the Transaction Manager (declarative transaction) --> <bean id = "transactionManager" class = "org. springframework. 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 = "execution (* com. bluesky. spring. dao. *. *(..)) "/> <aop: advisor advice-ref =" txAdvice "pointcut-ref =" interceptorPointCuts "/> </aop: config> </beans>

<Span style = "font-size: 18px; color: # cc33cc;"> <strong> Method 5: 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/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-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.LocalSessionFactoryBean "> <property name =" configLocation "value =" classpath: hibernate. cfg. xml "/> <property name =" configurationClass "value =" org. hibernate. cfg. annotationConfiguration "/> </bean> <! -- Define the Transaction Manager (declarative transaction) --> <bean id = "transactionManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager "> <property name =" sessionFactory "ref =" sessionFactory "/> </bean> </beans>

@ Transactional annotation must be added to 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

Related Article

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.