事务管理---声明式事务管理spring配置方式(二)

本贴最后更新于 3760 天前,其中的信息可能已经渤澥桑田

引言:

      接上篇文章的探讨。初步将事务配置分为编程式事务管理,和声明式事务管理,两种事务管理的区别,已经在上篇文章中做了简要说明。下来将详细说明下spring的事务配置方式。看了很多资料,也结合我们实际项目中的配置方式,大致分为四五种。

使用拦截器方式:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
 "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
&lt;bean id=&quot;sqlMapClient&quot; class=&quot;org.springframework.orm.ibatis.SqlMapClientFactoryBean&quot;&gt;
	&lt;property name=&quot;configLocation&quot;&gt;
		&lt;value&gt;classpath:sqlmap-config.xml&lt;/value&gt;
	&lt;/property&gt;
	&lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot; /&gt;
&lt;/bean&gt;

&lt;bean id=&quot;transactionInterceptor&quot;
	class=&quot;org.springframework.transaction.interceptor.TransactionInterceptor&quot;&gt;
	&lt;property name=&quot;transactionManager&quot;&gt;
		&lt;ref bean=&quot;transactionManager&quot; /&gt;
	&lt;/property&gt;
    &lt;property name=&quot;transactionAttributes&quot;&gt;
     &lt;props&gt;
      &lt;prop key=&quot;remove*&quot;&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
      &lt;prop key=&quot;add*&quot;&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
      &lt;prop key=&quot;modify*&quot;&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
      &lt;prop key=&quot;find*&quot;&gt;PROPAGATION_REQUIRED,readOnly&lt;/prop&gt;
     &lt;/props&gt;
    &lt;/property&gt;
&lt;/bean&gt;
&lt;!-- 自动代理 --&gt;
&lt;bean id=&quot;autoproxy&quot;
	class=&quot;org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator&quot;&gt;
	&lt;property name=&quot;beanNames&quot;&gt;
		&lt;list&gt;
			&lt;value&gt;*Service&lt;/value&gt;
		&lt;/list&gt;
	&lt;/property&gt;
	&lt;property name=&quot;interceptorNames&quot;&gt;
		&lt;list&gt;
			&lt;value&gt;transactionInterceptor&lt;/value&gt;
		&lt;/list&gt;
	&lt;/property&gt;
&lt;/bean&gt;

</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" />
&lt;tx:annotation-driven transaction-manager=&quot;transactionManager&quot; /&gt;

&lt;bean id=&quot;sessionFactory&quot; class=&quot;org.springframework.orm.hibernate3.LocalSessionFactoryBean&quot;&gt;
    &lt;property name=&quot;configLocation&quot; value=&quot;classpath:hibernate.cfg.xml&quot; /&gt;
    &lt;property name=&quot;configurationClass&quot; value=&quot;org.hibernate.cfg.AnnotationConfiguration&quot; /&gt;
&lt;/bean&gt;

&lt;!-- 定义事务管理器(声明式的事务) --&gt;
&lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.orm.hibernate3.HibernateTransactionManager&quot;&gt;
    &lt;property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory&quot; /&gt;
&lt;/bean&gt;

  此时在 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>
&lt;!-- 定义事务管理器(声明式的事务) --&gt;
&lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.orm.hibernate3.HibernateTransactionManager&quot;&gt;
    &lt;property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory&quot; /&gt;
&lt;/bean&gt;

&lt;bean id=&quot;transactionBase&quot; class=&quot;org.springframework.transaction.interceptor.TransactionProxyFactoryBean&quot; lazy-init=&quot;true&quot; abstract=&quot;true&quot;&gt;
    &lt;!-- 配置事务管理器 --&gt;
    &lt;property name=&quot;transactionManager&quot; ref=&quot;transactionManager&quot; /&gt;
    &lt;!-- 配置事务属性 --&gt;
    &lt;property name=&quot;transactionAttributes&quot;&gt;
        &lt;props&gt;
            &lt;prop key=&quot;*&quot;&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
        &lt;/props&gt;
    &lt;/property&gt;
&lt;/bean&gt;

&lt;!-- 配置DAO --&gt;
&lt;bean id=&quot;userDaoTarget&quot; class=&quot;com.bluesky.spring.dao.UserDaoImpl&quot;&gt;
    &lt;property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory&quot; /&gt;
&lt;/bean&gt;

&lt;bean id=&quot;userDao&quot; parent=&quot;transactionBase&quot;&gt;
    &lt;property name=&quot;target&quot; ref=&quot;userDaoTarget&quot; /&gt;
&lt;/bean&gt;</pre> 

以上几种方式在项目中比较常用,了解这些配置,以及按照规范对应项目中的编码就没什么大的问题。

  • Spring

    Spring 是一个开源框架,是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。

    940 引用 • 1458 回帖 • 158 关注
  • 事务管理
    3 引用

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...