让Spring架构减化事务配置

注:原创文章,曾发表在it168

Spring颠覆了以前的编程模式,引入了IOC等全新的概念,广受大家的喜爱。目前大多数j2ee项目都已经采用Spring框架。Spring最大的问题是太多的配置文件,使得你不仅需要维护程序代码,还需要额外去维护相关的配置文件。最典型的就是事务配置(注:这里的“事务配置”都指“声明式事务配置”),在Spring中进行事务配置除了定义对象自身的bean外,还需要定义一个进行事务代理的bean.如果你有n个类需要引入事务,那么你就必须定义2n个bean。维护这些bean的代价是十分昂贵的,所以必须要对事务配置进行减化。如果你是基于Spring进行架构设计,那么作为一个好的架构设计师,应该把一些公共的方面进行简化,让项目的开发人员只关心项目的业务逻辑,而不要花费太多的精力去关心业务逻辑之外的太多东西。所以作为一个好的架构就应该把事务管理进行简化,让程序员花在编程之外的工作最小化。

1.Spring声明式事务配置的几种方法

在Spring中进行事务控制首先要选择适当的事务管理器,其次为程序选择划分事务的策略。如果只有单个事务性资源,可以从“单一资源”的PlatformTransactionManger实现当中选择一个,这些实现有:DataSourceTransactionManager,HibernateTransactionManager,JdoTransactionManager,PersistenceBrokerTransactionManager和JmsTransactionManager。根据你所采用的数据库持久化技术选择。如果你的项目运行于支持JTA的服务器,那么将选择JtaTransactionManger,将会支持多资源事务。

下表将为你选择适当的事务管理器提供参考。

技术事务管理器内建的事务支持

JDBCDataSurceTransactionManagerJdbcTemplate和org.springframework.jdbc.object包中的所有类

JtaTransactionManager

IBATISDataSourceTransactionManagerSqlMapClientTemplate和SqlClientTemplate

JtaTransactionManager

HibernateHibernateTransactionManagerHibernateTemplate和HibernateInterceptor

JtaTransactionManager

JDOJdoTransactionManagerJdoTemplate和JdoInterceptor

JtaTransactionManager

ApacheOJBPersistenceBrokerTransactionManagerJtaTransactionManager

PersistenceBrokerTemplate

JMSJmsTransactionManagerJmsTemplate

在划分事务时,我们需要进行事务定义,也就是配置事务的属性。事务的属性有传播行业,隔离级别,超时值及只读标志。TransactionAttribute接口指定哪些异常将导致一个回滚,哪些应该一次性提交。

(1)使用ProxyFactoryBean和TransactionInterceptor

<!--定义本地数据源-->

<beanid="dataSource"name="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">

<propertyname="driverClassName"value="${jdbc.driverClassName}"/>

<propertyname="url"value="${jdbc.url}"/>

<propertyname="username"value="${jdbc.username}"/>

<propertyname="password"value="${jdbc.password}"/>

</bean>

<!--!定义单个jdbc数据源的事务管理器-->

<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<propertyname="dataSource"ref="dataSource"/>

</bean>

<!—定义拦截器-->

<beanid="transactionInterceptor"

class="org.springframework.transaction.interceptor.TransactionInterceptor">

<propertyname="transactionManager">

<refbean="transactionManager"/>

</property>

<propertyname="transactionAttributes">

<props>

<propkey="insert*">PROPAGATION_REQUIRED</prop>

<propkey="update*">PROPAGATION_REQUIRED</prop>

<propkey="save*">PROPAGATION_REQUIRED</prop>

<propkey="find*">PROPAGATION_SUPPORTS,readOnly</prop>

<propkey="get*">PROPAGATION_SUPPORTS,readOnly</prop>

<propkey="*">PROPAGATION_SUPPORTS,readOnly</prop>

</props>

</property>

</bean>

<!—定义业务对象-->

<beanid="com.prs.application.ehld.sample.biz.service.sampleService.target"

class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">

<propertyname="userInfoDAO"

ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">

</property>

</bean>

<!—定义业务对象的事务代理对象-->

<beanid="com.prs.application.ehld.sample.biz.service.sampleService"class="org.springframeword.aop.framework.ProxyFacgtoryBean">

<propertyname="target"

ref="com.prs.application.ehld.sample.biz.service.sampleService.target">

</property>

<propertyname="interceptorNames">

<value>transactionInterceptor</value>

</property>

</bean>

通过ProxyFacgtoryBean和TransactionInterceptor组合使用,可以对事务进行更多的控制。所有需要事务控制的对象可以共享一个transactionInterceptor的事务属性。

(2)使用TransactionProxyFactoryBean

<!—定义业务对象-->

<beanid="com.prs.application.ehld.sample.biz.service.sampleService.target"

class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">

<propertyname="userInfoDAO"

ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">

</property>

</bean>

<!—定义业务对象的事务代理对象-->

<beanid="com.prs.application.ehld.sample.biz.service.sampleService"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"

abstract="true">

<propertyname="transactionManager">

<refbean="transactionManager"/>

</property>

<propertyname="target"

ref="com.prs.application.ehld.sample.biz.service.sampleService.target"/>

<propertyname="transactionAttributes">

<props>

<propkey="insert*">PROPAGATION_REQUIRED</prop>

<propkey="update*">PROPAGATION_REQUIRED</prop>

<propkey="save*">PROPAGATION_REQUIRED</prop>

<propkey="find*">PROPAGATION_SUPPORTS,readOnly</prop>

<propkey="get*">PROPAGATION_SUPPORTS,readOnly</prop>

<propkey="*">PROPAGATION_SUPPORTS,readOnly</prop>

</props>

</property>

</bean>

使用TransactionProxyFactoryBean需要为每一个代理对象都去定义自己的事务属性。

(3)使用TransactionProxyFactoryBean及abstract属性来简化配置

这种方工也是目前使用得最多的一种声明式事务配置方法

<!--事务控制代理抽象定义-->

<beanid="baseTransactionProxy"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"

abstract="true">

<propertyname="transactionManager">

<refbean="transactionManager"/>

</property>

<propertyname="transactionAttributes">

<props>

<propkey="insert*">PROPAGATION_REQUIRED</prop>

<propkey="update*">PROPAGATION_REQUIRED</prop>

<propkey="save*">PROPAGATION_REQUIRED</prop>

<propkey="find*">PROPAGATION_SUPPORTS,readOnly</prop>

<propkey="get*">PROPAGATION_SUPPORTS,readOnly</prop>

<propkey="*">PROPAGATION_SUPPORTS,readOnly</prop>

</props>

</property>

</bean>

<!—定义业务对象-->

<beanid="com.prs.application.ehld.sample.biz.service.sampleService.target"

class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">

<propertyname="userInfoDAO"

ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">

</property>

</bean>

<!—定义业务对象的事务代理对象-->

<beanid="com.prs.application.ehld.sample.biz.service.sampleService"parent="baseTransactionProxy">

<propertyname="target"

ref="com.prs.application.ehld.sample.biz.service.sampleService.target">

</property>

</bean>

使用abstract属性,可以让代理对象可以共享一个定义好的事务属性,使配置简化。

(4)使用BeanNameAutoProxyCreator

<!—定义拦截器-->

<beanid="transactionInterceptor"

class="org.springframework.transaction.interceptor.TransactionInterceptor">

<propertyname="transactionManager">

<refbean="transactionManager"/>

</property>

<propertyname="transactionAttributes">

<props>

<propkey="insert*">PROPAGATION_REQUIRED</prop>

<propkey="update*">PROPAGATION_REQUIRED</prop>

<propkey="save*">PROPAGATION_REQUIRED</prop>

<propkey="find*">PROPAGATION_SUPPORTS,readOnly</prop>

<propkey="get*">PROPAGATION_SUPPORTS,readOnly</prop>

<propkey="*">PROPAGATION_SUPPORTS,readOnly</prop>

</props>

</property>

</bean>

<!—定义bean别名自动代理创建器-->

<beanid="autoProxyCreator"

class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

<propertyname="interceptorNames">

<value>transactionInterceptor</value>

</property>

<propertyname="beanNames">

<list>

<idreflocal="com.prs.application.ehld.sample.biz.service.sampleService"/>

</list>

</property>

</bean>

<!—定义业务对象-->

<beanid="com.prs.application.ehld.sample.biz.service.sampleService"

class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">

<propertyname="userInfoDAO"

ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">

</property>

</bean>