Spring 事务最常用配置

1.(spring3mvc)

<!-- 事务管理器配置,单数据源事务 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 使用annotation定义事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="true" />

 2.(stuts1+ibatis+spring)

<!-- 事务管理器配置,单数据源事务 -->
	<!-- transaction Manager (public) -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- uses  @Transactional sign -->
	<tx:annotation-driven />

	<!-- uses @AspectJ sign-->
	<aop:aspectj-autoproxy />

	<!-- 配置事务的传播特性 -->
	<!-- default define, uses transactionManager,default *  is readonly -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="insert*" />
			<tx:method name="update*" />
			<tx:method name="del*" />
			<tx:method name="*" read-only="true" />
			<tx:method name="transaction*" propagation="NESTED" />
		</tx:attributes>
	</tx:advice>
	
	<!-- 那些类的哪些方法参与事务 -->
	<!--  uses AspectJ ding  AOP  -->
	<aop:config proxy-target-class="true">
		<aop:advisor
			pointcut="execution(* com.ving.xzfw.service.impl.*Service*.*(..))"
			advice-ref="txAdvice" />
	</aop:config>

 3.(stuts2+hibernate4+spring3)

 <!-- 开启AOP监听 只对当前配置文件有效 -->
	<aop:aspectj-autoproxy expose-proxy="true"/>
	
	<!-- 开启注解事务 只对当前配置文件有效 -->
  	<tx:annotation-driven transaction-manager="txManager"/>

    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="merge*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="put*" propagation="REQUIRED" />
            <tx:method name="use*" propagation="REQUIRED"/>
            <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />
            <tx:method name="count*" 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="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <aop:config expose-proxy="true">
        <!-- 只对业务逻辑层实施事务 -->
        <aop:pointcut id="txPointcut" expression="execution(* cn.javass..service..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

 4.(mybatis+springMVC)

<!-- ================================= 事务控制相关 ============================================= -->
   	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
    	<property name="dataSource" ref="dataSource"></property>
 	</bean>   
	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="delete*" propagation="REQUIRED" read-only="false" 
			           rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
			<tx:method name="insert*" propagation="REQUIRED" read-only="false" 
			           rollback-for="java.lang.RuntimeException" />
			<tx:method name="update*" propagation="REQUIRED" read-only="false" 
			           rollback-for="java.lang.Exception" />
			
			<tx:method name="find*" propagation="SUPPORTS"/>
			<tx:method name="get*" propagation="SUPPORTS"/>
			<tx:method name="select*" propagation="SUPPORTS"/>
		</tx:attributes>
	</tx:advice>
	
	<aop:config>
		<aop:pointcut id="pc" expression="execution(* *.*.*(..))" />
		<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
	</aop:config>

 

你可能感兴趣的:(spring)