五种Spring事务的配置方式

事务策略配置管理器
PROPAGATION_REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。spring默认的事务策略
PROPAGATION_SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。
-Exception表示有Exception抛出时,事务回滚. -代表回滚+就代表提交

readonly 就是read only, 设置操作权限为只读,一般用于查询的方法,优化作用.


1:每个Bean都有一个代理(个人不赞同这种配置,代码太臃肿了,例子就网上拉一个下来,感觉还是配置在service层比较好哈)

    <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 -->
    <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
   
    <bean id="userDao" 
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
           <!-- 配置事务管理器 --> 
           <property name="transactionManager" ref="transactionManager" />    
        <property name="target" ref="userDaoTarget" /> 
         <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />
        <!-- 配置事务属性 --> 
        <property name="transactionAttributes"> 
            <props> 
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props> 
        </property> 
    </bean>



2:所有Bean共享一个代理基类(自己项目中正使用的)

<!-- 定义事务管理器 --> 
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<!--transactionProxy-->
	<bean id="transactionProxy" abstract="true"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<props>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	
    <bean id="userDao" class="com.yeshun.dao.impl.UserDaoImpl" parent="baseDAO" />  
      
    <bean id="userService" parent="transactionProxy">
		<property name="target">
			<bean class="com.yeshun.service.impl.UserServiceImpl">
				<property name="userDao" ref="userDao" />
			</bean>
		</property>
	</bean>

3:使用拦截器

	<!-- 定义事务管理器 --> 
	<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"></property>
	  <property name="transactionAttributes">
	    <props>
	        <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
		<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
		<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
		<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
		<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
		<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
		<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
		<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
		<prop key="*">PROPAGATION_REQUIRED</prop>
	    </props>
	  </property>
	</bean>
	<bean id="beanNameAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
	  <property name="beanNames">
	    <value>*Service</value>
	  </property>
	  <property name="interceptorNames">
	    <list>
	       <value>transactionInterceptor</value>
	    </list>
	  </property>
	</bean>

	<bean id="userService" class="com.yeshun.service.impl.UserServiceImpl">
		<property name="userDao" ref="userDao" />
	</bean>

4:使用tx标签配置的拦截器(这个方式不错)

	<!-- 定义事务管理器 --> 
	<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="get*" propagation="REQUIRED" read-only="true" />
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />
            <tx:method name="list*" propagation="REQUIRED" read-only="true" />
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />
            <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
	    <!-- 其他方法使用默认的事务设置 -->
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
   
    <aop:config>
        <aop:pointcut id="aopPointcut"
            expression="execution(* com.yeshun.service.impl.*Impl.*(..))" />
        <aop:advisor advice-ref="txAdvice"
            pointcut-ref="aopPointcut" />       
    </aop:config> 
  
 	<bean id="userService" class="com.yeshun.service.impl.UserServiceImpl">
		<property name="userDao" ref="userDao" />
	</bean>  

5:注解方式
<!-- 采用注释的方式配置bean -->  
    <context:annotation-config />  
    <context:component-scan base-package="com.yeshun" />  
    
  
    <!-- 采用注释的方式配置 aop -->  
    <aop:aspectj-autoproxy /> 
    
     <!-- 采用annotation的方式配置事务 -->  
    <tx:annotation-driven transaction-manager="transactionManager" /> 

	<!-- 定义事务管理器 --> 
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

此时在Service上需加上@Transactional注解,如下:

@Transactional配置修改该规则(有时候不需要修改该规则,比如save,update....)
@Transactional(noRollbackFor=RuntimeException.class)方法事物说明运行时异常进行回滚
@Transactional(RollbackFor=Exception.clas)
@Transactional(readyOnly=true)
@Transactional(timeout=100)默认30
@Transactional(isolation)数据库的隔离级别 (没怎么用过,也不懂)

@Transactional
@Service("UserServiceImpl")
public class UserServiceImpl implements UserService{

	private @Resource(name = "UserDaoImpl")
		UserDao userDao;

	public List<User> findAllUser() {
		// TODO Auto-generated method stub
		return userDao.findAllUser();
	}

}



发布了33 篇原创文章 · 获赞 5 · 访问量 6万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章