spring的事务配置主要有三部分组成DataSource、TransactionManager和代理机制。
DataSource根据数据源的不同分为DataSource,SessionFactory.分别对应TransactionManager的实现是DataSourceTransactionManager和HibernateTransactionManager。
而代理部分又有5种具体的实现:
1.每个Bean都有一个代理
<!-- 定义事务管理器(声明式的事务) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置DAO -->
<bean id="userDaoTarget" class="com.spring.dao.interface.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 配置事务管理器 -->
<property name="transactionManager" ref="transactionManager" />
<!-- 事务代理会实现目标对象的接口:这里是属性名是target的引用 -->
<property name="target" ref="userDaoTarget" />
<!--
使用proxyInterfaces属性来限定事务代理来代 理指定接口也是可以的
<property name="proxyInterfaces" value="com.spring.dao.GeneratorDao" />
-->
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<!--[-] 代表该类方法名为insert开头的方法出错的时候rollback,[+]为出错直接commit -->
<prop key="insert*">PROPAGATION_REQUIRED,-RuntimeException</prop>
</props>
</property>
</bean>
2.所有Bean共享一个代理基类
<!-- 定义事务管理器(声明式的事务) -->
同上
<!-- lazy-init="true" 代表延迟初始化 -->
<bean id="transactionBase"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
<!-- 配置事务管理器 -->
<property name="transactionManager" ref="transactionManager" />
<!-- 配置事务属性 PROPAGATION_REQUIRED
代表支持现在的事务,如果没有就建立一个新的事务-->
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 配置DAO -->
<bean id="userDaoTarget" class="com.spring.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- parent属性等于是把 transactionBase 注入到userDao中-->
<bean id="userDao" parent="transactionBase" >
<property name="target" ref="userDaoTarget" />
</bean>
3.使用拦截器
<!-- 定义事务管理器(声明式的事务) -->
同上
<!-- 事务拦截器,激活事务管理器所必须的bean -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- 定义事务处理代理bean,他需要两个属性,一个是指定需要代理的bean,另一个是代理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>
<!-- 业务逻辑层 -->
<bean id="tempService"
class="com.cj.transaction.service.TempService" abstract="false"
lazy-init="default" autowire="default" dependency-check="default">
<property name="userDAO">
<ref bean="userDAO" />
</property>
<property name="deptDAO">
<ref bean="deptDAO" />
</property>
</bean>
<bean id="userDAO" class="com.cj.transaction.hibernate.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="deptDAO" class="com.cj.transaction.hibernate.DeptDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
4.使用tx标签配置的拦截器
使用tx标签要注意xml头上引
<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"
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
default-autowire="byName">
<!-- 定义事务管理器(声明式的事务) -->
同上
<!-- 指定为save的方法创建事务 在发生错误的时候rollback , propagation设定事务的传播性属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<!-- 第1个*代表返回值 第2个*代表方法,(..)代表方法参数 -->
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.jj.service.WebService2dbBatchService.*(..))" advice-ref="txAdvice" />
</aop:config>
5.全注解
<!-- 定义事务管理器(声明式的事务) -->
同上
<!--启动spring注解功能-->
<!--说明: 如果事务管理器的id是transactionManager,这里可以不对transaction-manager进行配置,即<tx:annotation-driven />就可以,这个配置是告诉spring在类(接口)层面或者方法上层面上检查应用程序上下文中的所有标准了@Transactional的bean,,spring将自动把事务通知的内容通知给它。这个通知的事务参数将由@Transactional注释的参数来定义。如果注释的是接口,则该接口的所有实现类都将被事务化。
-->
<tx:annotation-driven transaction-manager="transactionManager" />
此时在DAO上需加上@Transactional注解,如下:
package com.bluesky.spring.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import com.bluesky.spring.domain.User;
@Transactional
@Component("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public List<User> listUsers() {
return this.getSession().createQuery("from User").list();
}
}