引言:
接上篇文章的探讨。初步将事务配置分为编程式事务管理,和声明式事务管理,两种事务管理的区别,已经在上篇文章中做了简要说明。下来将详细说明下spring的事务配置方式。看了很多资料,也结合我们实际项目中的配置方式,大致分为四五种。
使用拦截器方式:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans><bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value>classpath:sqlmap-config.xml</value> </property> <property name="dataSource" ref="dataSource" /> </bean> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <property name="transactionAttributes"> <props> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="modify*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> <!-- 自动代理 --> <bean id="autoproxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*Service</value> </list> </property> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean>
</beans>
使用tx标签配置的拦截器:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd" default-lazy-init="true" default-merge="false"> <!--事务管理 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <!-- spring声明式事务的配置,以下为spring的AOP事务管理的增强部分 --> <tx:advice id="tx-Advice" transaction-manager="transactionManager"> <tx:attributes> <!-- 需要由交给spring的aop来进行代理的方法的集合,如果应用有自己的方法需有由spring来进行事务控制必须添加方法 --> <!-- 读取数据方法,一般采用只读事务 --> <tx:method name="get*" isolation="DEFAULT" propagation="SUPPORTS" read-only="true" /> <tx:method name="load*" isolation="DEFAULT" propagation="SUPPORTS" read-only="true" /> <tx:method name="select*" isolation="DEFAULT" propagation="SUPPORTS" read-only="true" /> <tx:method name="query*" isolation="DEFAULT" propagation="SUPPORTS" read-only="true" /> <tx:method name="find*" isolation="DEFAULT" propagation="SUPPORTS" read-only="true" /> <tx:method name="list*" isolation="DEFAULT" propagation="SUPPORTS" read-only="true" /> <tx:method name="criteria*" isolation="DEFAULT" propagation="SUPPORTS" read-only="true" /> <tx:method name="*" isolation="DEFAULT" propagation="SUPPORTS" read-only="true" /> <!--其他方法,如save,update,insert等对数据库进行写入操作的方法,当产生Exception进行回滚 --> <tx:method name="init*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="insert*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="update*" isolation="DEFAULT" read-only="false" propagation="REQUIRED" rollback-for="Exception" /> <!-- 调用接口记录日志的事物 --> <tx:method name="createPNR" isolation="DEFAULT" read-only="false" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="queryQueryFlightInfos" isolation="DEFAULT" read-only="false" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="cancelPNR" isolation="DEFAULT" read-only="false" propagation="REQUIRED" rollback-for="Exception" /> <!-- 对外提供接口记录日志的事物 --> <tx:method name="getPNR" isolation="DEFAULT" read-only="false" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="doThirdPata" isolation="DEFAULT" read-only="false" propagation="REQUIRED" rollback-for="Exception" /> </tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="tx-Advice" pointcut="execution(* com.newtouch.platform..service..*Service*.*(..)) || execution(* com.test.service..*Service*.*(..)) || execution(* cn.com.besttone.reservation.service..*Service*.*(..))"/> </aop:config> </beans>
使用注解方式
<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> <!-- 定义事务管理器(声明式的事务) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean>
此时在 DAO 上需加上 @Transactional 注解,如下:
@Transactional
@Component("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public List<User> listUsers() {
return this.getSession().createQuery("from User").list();
}
......
}
共享代理类
<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><!-- 定义事务管理器(声明式的事务) --> <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"> <!-- 配置事务管理器 --> <property name="transactionManager" ref="transactionManager" /> <!-- 配置事务属性 --> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 配置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></pre>
以上几种方式在项目中比较常用,了解这些配置,以及按照规范对应项目中的编码就没什么大的问题。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于