Spring 事务管理

事务:要么全有要么全无的操作叫做事务

[color=blue][b]事务的几个特征:[/b][/color]
(1)原子性Atomic
(2)一致性Consistent
(3)隔离性Isolated
(4)持久性Durable

[color=blue][b]Spring可供选择的事务管理器[/b][/color]

(1)DataSourceTransactionManager在单一的jdbc datasource中管理事务,配置bean时注入DataSource

(2)HibernateTrancationManager,配置bean时注入SessionFactory,
HibernateTrancationManager把事务管理委托给从Hibernate session中获得的net.sf.hibernate.Trancation对象
(2)JdoTrancationManager
(2)JtaTrancationManager一个事务跨越多个资源时必须使用,它把事务管理委托给一个JTA的实现,配置Bean时注入trancationManagerName,通过JNDI找到该事务管理器,它和javax.trancation.UserTrancation以及javax.tranction.TrancationManager对象一起工作,事务成功调用UserTrancation.commit(),否则调用UserTrancation.rollback()方法
(2)PersistenceBrokerTrancationManager
[b]
[color=blue]Spring中编写事务[/b][/color]

[b](1)编程实现[/b]
使用Spring的TrancationTemplate类

transactionTemplate.execute(
new TransactionCallback(){
public Object doInHibernate(TransactionStatus ts){
try{
//你的数据库操作代码
}catch(Exception e){
// 出现异常则回滚
ts.setRollbackOnly();
}
//如果成功则提交
return null;
}
});


transactionTemplate通过注入来获得


<bean id="transactionTemlate" class="*****TrancationTemplate">
<property name="transactionManager">
<ref bean="transactionManager"/>
<property>
</bean>


[b](2)声明式实现[/b]

它是通过AOP框架实现的,具体配置过程:

<bean id="transactionProxyFactoryBean" class="TransactionProxyFactoryBean"
<property name="proxyInterfaces">
<list>
<value>yourInterfaces</value>
</list>
</property>
<property name="target"......
<property name="transactionManager".....
<property name="transactionAttributeSource"..........
</bean>
//transactionProxyFactoryBean的命名规范,它继承目标对象的名称,而目标对象的id加Target

[color=blue][b]
事务属性[/b][/color]

Spring中事务属性是对事务策略如何应用到方法的描述,这个描述包括下列参数:

[b]传播行为[/b]
定义了关于客户端和被调用方法的事务边界,Spring中有7种不同的值
(1)propagation_mandatory方法必须在事务中,没有事务则异常
(2)propagation_nested如果有事务则嵌套,没有则和required相同
(3)propagation_never不在事务中,若有事务则异常
(4)propagation_not_supported方法不在事务中,若有事务,方法运行期间事务挂起
(5)propagation_required必须在事务中,如果没有创建新的事务
(6)propagation_requires_new必运行在自己的事务中,若有事务则方法运行期间挂起
(7)propagation_supports不需要事务,但如果有事务,也可运行在事务中


[b]隔离级别[/b]
多个事务同时访问一个数据,就会造成脏读dirty read,不可重复读nonrepeatedable read,幻读phantom read
Spring有5种隔离级别

(1)isolation_default使用数据的隔离级别
(2)isolation_read_uncommited运行脏读,幻读和不可重复读
(3)isolation_read_commited运行并发事务提交后读取,避免脏读
(4)isolation_repeatable_read对相同数据多次读取结果一致,防止脏读,不可重复读
(5)isolation_serializable完全服从ACID隔离级别,完全锁定在事务中涉及的所有表


[b]只读提示[/b]
如果一个事务只对数据库执行读操作,可以声明这个事务为只读,数据库就可以应用一些优化措施。
只读的优化措施是在事务启动时有后端的数据库实施的,所以把只有那些有可能启动新事务的传播行为标示为只读才有意义。
此外,如果使用hibernate作为持久化机制,声明一个只读事务将会把hibernate的flush设置为flush_never,避免和数据库进行不比要得同步,将所有的更新延迟到事务的结束。

[b]事务超时间隔[/b]

在那些可能启动新事务的传播行为上设置事务超时间隔才有意义。

[color=blue][b]声明一个简单的事务策略[/b][/color]

TransactionProxyFactoryBean 有一个transactionAttributeSource属性,这个属性被设置成TransactionAttributeSource的实例,TransactionProxyFactoryBean 是作为在方法上查找事务属性的一个参考
TransactionAttributeSource的接口定义

public interface TransactionAttributeSource{
public TransactionAttribute getTransactionAttribut(java.lang.reflect.Method method,java.lang.Class targetClass);
}

随着transactionAttributeSource的bean对象的声明,所有被TransactionProxyFactoryBean的目标类代理的方法都被执行到一个事务环境中了,但没有指定哪个方法是事务性的,这个由MatchAlwaysTransactionAttributeSource来指定,它是TransactionAttributeSource的一个最简单的实现,每次调用,都是返回相同的TransactionAttribute [required和isolation_default]

[b]可以通过配置一个DefaultTransactionAttribute的bean,来修改MatchAlwaysTransactionAttributeSource默认的事务属性[/b]
(1)配置一个DefaultTransactionAttribute

<bean id="myDefautAttribute" class="****.DefaultTransactionAttribute">
//传播行为
<property name="propagationBehaviorName">/\
<value>propagation_never</value>
</property>
//隔离级别
<property name="isolationLevleName">
<value>isolation_default</value>
</property>
</bean>

(2)把myDefautAttribute注入到MatchAlwaysTransactionAttributeSource的transactionAttribute属性中即可

[color=blue][b]对目标对象的每个方法实施不同的事务管理[/b][/color]

[b]使用NameMatchTransactionAttributeSource[/b]

NameMatchTransactionAttributeSource可以让你在方法上配置不同的事务,配置一个它的实例bean,并注入到TransactionProxyFactoryBean中的transactionAttributeSource中
NameMatchTransactionAttributeSource实例配置

<bean id="transactionAttributeSource" class="NameMatchTransactionAttributeSource">
<property name="properties">
<props>
//指定使用事务的方法名,可以使用通配符
<prop key="Your Method Name">
//传播行为,隔离级别,只读设定,-触发回滚,+异常也提交事务
PROPAGATION,ISOLATION,readonly,-Exceptions,+Exceptions
</prop>
</props>
</property>
</bean>



[b]不使用NameMatchTransactionAttributeSource[/b]
TransactionProxyFactoryBean中有一个transactionProperties属性,我们可以通过配置这个属性来完成不同方法不同事务的配置

......
<property name="transactionProperties">
<props>
//指定使用事务的方法名,可以使用通配符
<prop key="Your Method Name">
//传播行为,隔离级别,只读设定,-触发回滚,+异常也提交事务
PROPAGATION,ISOLATION,readonly,-Exceptions,+Exceptions
</prop>
</props>
</property>
......


[color=blue][b]用元数据声明事务[/b][/color] page 193

[b]bean的配置[/b]

我们通过修改transactionAttributeSource的注入替换为注入AttributesTransactionAtrributeSource即可,下面是AttributesTransactionAtrributeSource的配置

<bean id="transactionAttributeSource" class="AttributesTransactionAtrributeSource">
<contructor-arg>
<ref bean="attributesImpl"//见下
<contructor-arg>
</bean>

//attributesImpl 对象将被事务属性源使用,和底层的元数据实现配合,这样就与JSR-175或者Jakarta-Commons Attributes解耦合

<bean id="attributesImpl" class="org.springframw...CommonsAttributes">
....
</bean>


[b]java方法上加上事务标签[/b]

在类或者方法前面的注释中增加doclet标签,标签有以下几种形式


/**
*@@attribute-name("arg1",property="arg2")
*attribute-name:属性名称;arg1构造方法的参数,property="arg2":设置一个属 性的参数 */
[b]例如给 方法simpleMethod()增加事务描述[/b]
/**
*@@org.springframework.transaction.interceptor.DefaultTransactionAttribute(propagationBehaviorName="PROPAGATION_REQUIRED")*/
public void simpleMethod(){
......
}
//DefaultTransactionAttribute必须用全限定名,避免IDE删除导入的包
//如果把注释写在class类上,那么事务属性应用于所有的method



[color=blue][b]修剪事务声明[/b][/color]
[b]
繁复的XML配置文件[/b]

当你选择了TransactionAttributeSource,并且把服务层的方法声明为事务的,并且定义了合适的事务管理器,并完成了如下bean的配置

<bean id="txLoanBS"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>cn.ccb.csps.efbuss.bizservice.ITxLoanBS</value>
</list>
</property>
<property name="target">
<ref bean="txLoanBSImplTarget" />
</property>
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributeSource">
<ref bean="namingTransactionAttribute" />
</property>
</bean>

<bean id="txLoanBSImplTarget"
class="cn.ccb.csps.efbuss.bizservice.impl.TxLoanBSImpl">



浏览这个配置对象,你会发现好几对服务/目标,你会发现好几个对象的声明,它们的名字暗示它们是服务对象比如txLoanBS,但是它只是TransactionProxyFactoryBean的一个实例,真正的服务对象是被命名为带target后缀并被注入到TransactionProxyFactoryBean中target属性中的bean

一般项目中服务对象都用了同样的方法定义,并设定了相同的事务管理器相同的事务属性源,看上去是一大堆繁复的XML文件

[b]消除繁复的XML配置[/b]

Spring提供了2种方法:
(1)bean继承,从父TransactionProxyFactoryBean继承,使用Spring对父bean的支持,即bean的parent属性继承父bean的属性
对于上面的bean的具体实现过程如下
首先通过lazy-init=true,它告诉容器不要创建bean,构造一个抽象的TransactionProxyFactoryBean Bean

<bean id="abstractTxDefinition"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="init">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributeSource">
<ref bean="namingTransactionAttribute" />
</property>
</bean>

然后再配置某个具体的事务服务
<bean id="txLoanBS" parent="abstractTxDefinition">
<property name="proxyInterfaces">
<list>
<value>cn.ccb.csps.efbuss.bizservice.ITxLoanBS</value>
</list>
</property>
<property name="target">
<bean id="txLoanBSImplTarget"
class="cn.ccb.csps.efbuss.bizservice.impl.TxLoanBSImpl">

</property>
</bean>

(2)AOP自动代理


首先声明一个bean,成为DefaultAdvisorAutoProxyCreator的实列

<bean id="autoProxy" class="********.DefaultAdvisorAutoProxyCreator"/>

DefaultAdvisorAutoProxyCreator将在context中遍历advisor(对于事务为TransactionAttributeSourceAdvisor)自动用它们来代理匹配advisor的pointcut的所有bean

//事务切面
<bean id="transactionAdvisor" class=""TransactionAttributeSourceAdvisor>
<constructor-org>
<ref bean="transactionInterceptor"/>
</constructor-org>
</bean>
//事务通知
<bean id="transactionInterceptor" class="TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource"/>
</property>
</bean>
//定义属性源
如果使用NameMatchTransactionAttributeSource配置属性源,比如get*配置为执行事务,那么所有的bean的get方法都会被加上事务,这可能不是我们想要的,因此对于自动代理,我们更好的选择是MethodMapTransactionAttributeSource,它需要指定需要事务化的完整类名和方法名

<bean id="transactionAttributeSource" class="MethodMapTransactionAttributeSource">
<property name="methodMap">
<map>
<entry key="cn.ccb..TxLoanBSImpl.get*">
<value>PROPAGATION_REQUIRED</value>
</entry>
</map>
</property>
</bean>
接下来我们删除所有的TransactionProxyFactoryBean实例,并把服务层的bean的名称去掉Target就可以了

当用自动代理的时候,事务属性源采用AttributesTransactionAtrributeSource是最好的选择了,使方法事务化和非事务化,只不过是往方法中添加合适的元数据就可以了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值