TransactionProxyFactoryBean

Spring容器中有两种思想很重要,也就是我们常用的Ioc和Aop,如果理解了这两种思想,对于我们学习设计模式和编程有很大的帮助,美国四人帮(GOF)写的设计模式中,有很多都用到了Ioc的思想。简单的说就是依赖注入的思想。常见的一种情况:如果一个类中要复用另外一个类中的功能时,我们可能会首先想到继承,如果你知道Ioc这种思想的话,我想你不会用继承,你会马上想到把要用到功能抽取出来,在我们要用到的类中只需通过set方法简单的注入就可以了,其实这里用到了对象的组合代替继承,这样不仅避免了单一继承,还很好的实现了松耦合。同时也遵循了面向对象的编程的设计原则:多用组合,少用继承。在这里对于Ioc和Aop这两种思想的好处。我就不介绍了。接下来我要说的是Spring中几种常见的事务配置,是Aop和Ioc的充分体现。
--------------------------------------
一、Propagation :

对于特定的方法或方法命名模式,代理的具体事务行为由事务属性驱动,如下面的例子所示:
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>
  key属性确定代理应该给哪个方法增加事务行为。这样的属性最重要的部份是传播行为。有以下选项可供使用:

PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。
前六个策略类似于EJB CMT:常量名相同,因此,对EJB开发人员来说,应该立刻就感到熟悉。第七个(PROPAGATION_NESTED)是Spring所提供的一个特殊变量。它要求事务管理器或者使用JDBC 3.0 Savepoint API提供嵌套事务行为(如Spring的DataSourceTransactionManager),或者通过JTA支持嵌套事务。

    

二、Isolation Level(事务隔离等级):
1、Serializable:最严格的级别,事务串行执行,资源消耗最大;
2、REPEATABLE READ:保证了一个事务不会修改已经由另一个事务读取但未提交(回滚)的数据。避免了“脏读取”和“不可重复读取”的情况,但是带来了更多的性能损失。
3、READ COMMITTED:大多数主流数据库的默认事务等级,保证了一个事务不会读到另一个并行事务已修改但未提交的数据,避免了“脏读取”。该级别适用于大多数系统。
4、Read Uncommitted:保证了读取过程中不会读取到非法数据。

spring中的Isolation属性:
1、ISOLATION_DEFAULT :使用当前数据源的默认级别
2、ISOLATION_READ_UNCOMMITTED :Dirty reads, non-repeatable reads, and phantom reads can occur.
3、ISOLATION_READ_COMMITTED :Dirty reads are prevented; non-repeatable reads and phantom reads can occur.
4、ISOLATION_REPEATABLE_READ:Dirty reads and non-repeatable reads are prevented; phantom reads can occur.
5、ISOLATION_SERIALIZABLE:Dirty reads, non-repeatable reads, and phantom reads are prevented.

三、readOnly
事务属性中的readOnly标志表示对应的事务应该被最优化为只读事务。这是一个最优化提示。在一些情况下,一些事务策略能够起到显著的最优化效果,例如在使用Object/Relational映射工具(如:Hibernate或TopLink)时避免dirty checking(试图“刷新”)。

四、Timeout

在事务属性中还有定义“timeout”值的选项,指定事务超时为几秒。在JTA中,这将被简单地传递到J2EE服务器的事务协调程序,并据此得到相应的解释。

事务划分策略

1、推荐在业务层使用事务,这样可以允许业务层捕获导致rollback的异常,并抛出恰当的业务层异常;不在dao层使用事务是因为这会限制了dao重用其他事务需求,并且dao层没有实现业务逻辑,并且原子性也是业务层的概念。

spring声明性事务的划分:
1、有四个地方需要配置:The four participants are transaction manager, proxy factory, transaction interceptor, and a set of transaction attributes.


2、使用ProxyFactoryBean/Transaction Interceptor(transactionInterceptor)配置spring事务

以下为配置实例:


<!-- The DBCP DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>

<!-- The DAO class -->
<bean id="dao"
class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

<!-- The transactionmanager to use for regular non JTA datasource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

<!-- TransactionInterceptor -->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="transactionAttributeSource"> <value>org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS,readOnlyorg.springframework.prospring.ticket.service.BoxOffice.allocate*=PROPAGATION_REQUIRED
</value>
</property>
</bean>

<!-- Transactional proxy for the primary business object -->
<bean id="boxOffice" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target"> <ref local="boxOfficeTarget"/> </property> <property name="proxyInterfaces"> <value>org.springframework.prospring.ticket.service.BoxOffice</value> </property> <property name="interceptorNames"> <value>transactionInterceptor</value> </property>
</bean>

<!-- Business Object -->
<bean id="boxOfficeTarget"
class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
<property name="boxOfficeDao">
<ref local="dao"/>
</property>
</bean>
3、使用TransactionProxyFactoryBean配置spring事务
以下为配置实例:

<!-- The DBCP DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>

<!-- The DAO class -->
<bean id="dao"
class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

<!-- The transactionmanager to use for regular non JTA datasource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

<!-- Transactional proxy and the primary business object -->
<bean id="boxOffice" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"><ref bean="transactionManager"/></property> <property name="target"> <bean class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
<property name="boxOfficeDao">
<ref local="dao"/>
</property>
</bean>
</property>
<property name="transactionAttributes"> <props> <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop> <prop key="allocate*">PROPAGATION_REQUIRED</prop> </props> </property>
</bean>
4、使用BeanNameAutoProxyCreator配置spring事务
如果有大量的bean需要使用事物,那么只要在配置文件中提供bean name给BeanNameAutoProxyCreator,spring就会个给该bean提供事务代理,配置实例如下:


<!-- The DBCP DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>

<!-- The DAO class -->
<bean id="dao"
class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

<!-- The transactionmanager to use for regular non JTA datasource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

<!-- TransactionInterceptor -->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="transactionAttributeSource"> <value>org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS,readOnlyorg.springframework.prospring.ticket.service.BoxOffice.allocate*=PROPAGATION_REQUIRED </value> </property>
</bean>

<!-- BeanNameAutoProxyCreator -->
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <value>transactionInterceptor</value> </property> <property name="beanNames"> <list> <idref local="boxOffice"/> </list> </property>
</bean>

<!-- Business Object -->
<bean id="boxOffice"
class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
<property name="boxOfficeDao">
<ref local="dao"/>
</property>
</bean>

http://www.blogjava.net/280211429/articles/75529.html
-----------------------------------------------------
Spring AOP特性的声明式事务管理,就可以避免编程式事务带来的难维护性。声明式事务只需要在配置文件中声明即可。这样的好处是业务逻辑(Dao)就不会意识到事务管理的存在,而且维护起来极其方便。

简化配置:
<bean id="TransactionProxyFactoryBean" lazy-init="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="do*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

<bean id="ForumService" parent="TransactionProxyFactoryBean">
<property name="target">
<bean class="ForumServiceImp">
</property>
</bean>
<bean id="ForumServiceImp" class="com.bbs.service.imp.ForumServiceImp">
<property name="forumDAO">
<ref local="ForumDAO" />
</property>
</bean>

-------------------------------------------------------

<!-- spring中配置 声明式事务的方法 -->
<!-- Transactional proxy and the primary business object -->
<bean id="daytestLogic"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 代理的目标类 -->
<property name="target">
<bean class="com.xiaonei.wap.app.daytest.logic.impl.DaytestLogicImpl">
<property name="daytestDAO" ref="daytestDAO" />
<property name="optionDAO" ref="optionDAO" />
<property name="questionDAO" ref="questionDAO" />
<property name="answerDAO" ref="answerDAO" />
<property name="globalCacheManager" ref="globalCacheManager" />
</bean>
</property>
<!-- DaytestLogicImpl是类,实现了接口 -->
<property name="proxyTargetClass" value="true" />
<property name="transactionManager" ref="daytestTransactionManager"/>
<!-- 定义了事物的执行策略,如传播属性,隔离等级等 -->
<property name="transactionAttributes">
<props>
<!-- PROPAGATION_REQUIRED支持当前事务,如果当前没有事务,就新建一个事务 -->
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<!-- 事务属性中的readOnly标志表示对应的事务应该被最优化为只读事务 -->
<prop key="*">PROPAGATION_REQUIRED, readOnly</prop>
</props>
</property>
</bean>


TransactionProxyFactoryBean里的transactionAttributes,他的prop元素中的key属性定义了target类的某个方法,可以使用通配符,如save*,prop元素的值定义了对key中定义方法应用的事务执行属性,指定格式如下:

传播行为,隔离等级,只读,+异常,-异常

其中传播行为必须定义,其他可省,例如:

......
<prop key="xxxx">PROPAGATION_REQUIRED,readOnly,-DataAccessException</prop>
......

"-"表示发生指定异常后立即回滚,"+"表示发生指定异常后立即提交。

经过上面xml定义,就相当于在UserDaoImpl的saveOrUpdate方法前后加入了事务处理的代码。

定义了userDaoProxy后,我们在想使用userDaoImpl,就需要注入userDaoProxy的bean了,而不是userDaoImpl的bean
<property name="userDao">
<ref bean="userDaoProxy"/>
</property>
当不想使用事务管理时,把userController注入的userDaoProxy改为注入userDaoImpl就行了。

proxyTargetClass需要配置的情况只有一种:
当TestTram类有实现某个接口,而TestProxyClass类中配置的类对象是TestTram时(而不是TestTram实现的接口),这时候你需要配置proxyTargetClass=true

如果TestTram没有实现某个接口,而TestProxyClass类中配置的类对象是TestTram,这个时候我们是不需要配置proxyTargetClass=true的.(使用cgilib来动态代理)
如果TestTram实现某个接口, 而TestProxyClass类中配置的是TestTram实现的interface的话.那样我既不需要配置proxyInterface,也不需要配置proxyTargetClass


为什么我们在没有配置proxyInterface情况下,去配置proxyTargetClass.因为spring会去拿到当前配置的target实现的所有接口,然后通过动态代理出类.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值