Spring transaction management (transfer) and spring transaction management
Spring is the administrator of SSH and is responsible for managing other frameworks and coordinating the work of various parts. Let's take a look at Spring transaction management today. Spring transaction management is divided into declarative and programming. Declarative configuration is performed in the Spring configuration file; programmatic configuration is written into the code by annotation. The statement is as follows:
The transaction configuration in the Spring configuration file is always composed of three parts: DataSource, TransactionManager, and proxy mechanism. In any configuration method, the agent mechanism is 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. Let's take a look at the specific configurations of the three declarative transactions:
Declarative transactions
Public Configuration
<! -- Configure sessionFactory --> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.annotation. annotationSessionFactoryBean "> <property name =" configLocation "> <value> classpath: config/hibernate. cfg. xml </value> </property> <property name = "packagesToScan"> <list> <value> com. entity </value> </list> </property> </bean> <! -- Configure the Transaction Manager (declarative transaction) --> <bean id = "transactionManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager "> <property name =" sessionFactory "ref =" sessionFactory "> </property> </bean> <! -- Configure DAO --> <bean id = "userDao" class = "com. dao. userDaoImpl "> <property name =" sessionFactory "ref =" sessionFactory "> </property> </bean>
First, use the tx label method.
<! -- The first way to configure transactions, tx --> <tx: advice id = "txadvice" transaction-manager = "transactionManager"> <tx: attributes> <tx: method name = "add *" propagation = "REQUIRED" rollback-for = "Exception"/> <tx: method name = "modify *" propagation = "REQUIRED" rollback-for = "Exception"/> <tx: method name = "del *" propagation = "REQUIRED" rollback-for = "Exception"/> <tx: method name = "*" propagation = "REQUIRED" read-only = "true"/> </tx: attributes> </tx: advice> <aop: config> <aop: pointcut id = "daoMethod" expression = "execution (* com. dao. *. *(..)) "/> <aop: advisor pointcut-ref =" daoMethod "advice-ref =" txadvice "/> </aop: config>
Expression = "execution (* com. dao .*.*(..))"
The first * indicates the return value, the second * indicates the sub-package under dao, the third * indicates the method name, and "(...)" indicates the method parameter.
Second, use proxy
<! -- The second way to configure transactions, proxy --> <bean id = "transactionProxy" class = "org. springframework. transaction. interceptor. transactionProxyFactoryBean "abstract =" true "> <property name =" transactionManager "ref =" transactionManager "> </property> <property name =" transactionAttributes "> <props> <prop key = "add *"> PROPAGATION_REQUIRED, -Exception </prop> <prop key = "modify *"> PROPAGATION_REQUIRED,-Exception </prop> <prop key = "del *"> P ROPAGATION_REQUIRED,-Exception </prop> <prop key = "*"> PROPAGATION_REQUIRED, readOnly </prop> </props> </property> </bean> <bean id = "userDao" parent = "transactionProxy"> <property name = "target"> <! -- Replace ref with bean --> <bean class = "com. dao. userDaoImpl "> <property name =" sessionFactory "ref =" sessionFactory "> </property> </bean>
Set the abstract attribute of transactionProxy to "true", and then set the parent attribute of the specific Dao to "transactionProxy", which can simplify the code.
Third, use the interceptor
<! -- The third way to configure transactions, Interceptor (not commonly used) --> <bean id = "transactionInterceptor" class = "org. springframework. transaction. interceptor. transactionInterceptor "> <property name =" transactionManager "ref =" transactionManager "> </property> <property name =" transactionAttributes "> <props> <prop key =" add * "> PROPAGATION_REQUIRED, -Exception </prop> <prop key = "modify *"> PROPAGATION_REQUIRED,-Exception </prop> <prop key = "del *"> PROPAGATION_REQUIRED, -Exception </prop> <prop key = "*"> PROPAGATION_REQUIRED, readOnly </prop> </props> </property> </bean> <bean id = "proxyFactory" class = "org. springframework. aop. framework. autoproxy. beanNameAutoProxyCreator "> <property name =" interceptorNames "> <list> <value> transactionInterceptor </value> </list> </property> <property name =" beanNames "> <list> <value> * Dao </value> </list> </property> </bean>
Spring transaction type details:
PROPAGATION_REQUIRED-- Supports the current transaction. If no transaction exists, a new transaction is created. This is the most common choice.
PROPAGATION_SUPPORTS-- Supports the current transaction. If no transaction exists, it is executed in non-transaction mode.
PROPAGATION_MANDATORY-- Supports the current transaction. If no transaction exists, an exception is thrown.
PROPAGATION_REQUIRES_NEW-- Creates a transaction. If a transaction exists, the transaction is suspended.
PROPAGATION_NOT_SUPPORTED-- Execute operations in non-transaction mode. If a transaction exists, the current transaction is suspended.
PROPAGATION_NEVER-- Runs in non-transaction mode. If a transaction exists, an exception is thrown.
PROPAGATION_NESTED-- If a transaction exists, it is executed in the nested transaction. If no transaction exists, perform a similar operation as PROPAGATION_REQUIRED.
Programmatic transactions
In programming, annotations are used. Note that annotations must be added to the Spring configuration file: <context: annotation-config/>, the function is to enable annotation. The specific configuration is as follows:
<! -- Enable annotation --> <context: annotation-config/> <! -- Configure sessionFactory --> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.annotation. annotationSessionFactoryBean "> <property name =" configLocation "> <value> classpath: config/hibernate. cfg. xml </value> </property> <property name = "packagesToScan"> <list> <value> com. entity </value> </list> </property> </bean> <! -- Configure the Transaction Manager --> <bean id = "transactionManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager "> <property name =" sessionFactory "ref =" sessionFactory "> </property> </bean> <! -- The fourth way to configure transactions, annotation --> <tx: annotation-driven transaction-manager = "transactionManager"/>
Annotation file:
package com.dao;import org.springframework.orm.hibernate3.HibernateTemplate;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import com.entity.User;@Transactionalpublic class UserDaoImpl_BAK extends HibernateTemplate { @Transactional(propagation=Propagation.REQUIRED,rollbackForClassName="Exception") public void addUser(User user) throws Exception { this.save(user); } @Transactional(propagation=Propagation.REQUIRED,rollbackForClassName="Exception") public void modifyUser(User user) { this.update(user); } @Transactional(propagation=Propagation.REQUIRED,rollbackForClassName="Exception") public void delUser(String username) { this.delete(this.load(User.class, username)); } @Transactional(readOnly=true) public void selectUser() { }}
@ Transactional in the Class header is the default transaction configuration. If the method does not have its own transaction type, the default transaction is used. If the method has its own configuration, the default transaction is used.
The above four configuration methods are the first and second most commonly used, and the third is relatively old, while the annotation method is not suitable for large projects, it is good for simple small projects, and its features are simple and clear. Each method has the characteristics and applicable environment of each method. There is no absolute good or bad, but the first two methods are used more in actual work.
---
Original article: http://www.cnblogs.com/newsouls/p/3988216.html