Spring Transaction 分析事务属性

众所周知的ACID属性:
原子性(atomicity)、一致性(consistency)、隔离性(isolation)以及持久性(durability)。我们无法控制一致性、原子性以及持久性,但可以控制超时,设置事务的只读性以指定隔离级别。
Spring在TransactionDefinition接口封装了所有这些设置。

探索TransactionDefinition接口:
view plaincopy to clipboardprint?
package org.springframework.transaction;

public interface TransactionDefinition {

int getPropagationBehavior();  

int getIsolationLevel();  

int getTimeout();  

boolean isReadOnly();  

String getName();  

}

getTimeout:返回一个事务必须完成的时间限制。

isReadOnly:表示事务是否只读。

getIsolationLevel:他对其他事务所看到的数据变化进行控制。
事务隔离级别:
隔离级别 说明
ISOLATION_DEFAULT 默认级别(对大多数数据库来说就是ISOLATION_READ_COMMITTED)
ISOLATION_READ_UNCOMMITTED 最低的隔离级别。事实上我们不应该隔离级别,因为在事务完成前,其他事务可以看到该事务所修改的数据。而在其他事务提交前,该事务也可以看到其他事务所做的修改。
ISOLATION_READ_COMMITTED 大多数数据库的默认级别。在事务完成前,其他事务无法看到该事务所修改的数据。遗憾的是,在该事务提交后,你就可以查看其他事务插入活更新的数据。这意味着在事务的不同点上,如果其他事务修改数据,你会看到不同的数据。
ISOLATION_REPEATABLE_READ 该隔离级别确保如果在事务中查询了某个数据集,你至少还能再次查询到相同的数据集,即使其他事务修改了所查询的数据。然而如果其他事务插入了新数据,你就可以查询到该新插入的数据。
ISOLATION_SERIALIZABLE 代价最大、可靠性最高的隔离级别,所有的事务都是俺顺序一个接一个的执行。

getPropagationBehavior:指定了当代码请求一个新的事务时Spring所做的事情。
传播行为指:
传播行为 说明
PROPAGATION_REQUIRED 当前如果有事务,Spring就会使用该事务;否则会开始一个新事务。
PROPAGATION_SUPPORTS 当前如果有事务,Spring就会使用该事务;否则不会开启一个新事务。
PROPAGATION_MANDATORY 当前如果有事务,Spring就会使用该事务;否则会抛出异常。
PROPAGATION_REQUIRES_NEW Spring总会开始一个新事务。如果当前有事务,则该事务挂起。
PROPAGATION_NOT_SUPPORTED Spring不会执行事务中的代码。代码总是在非事务环境下执行,如果当期有事务,则该事务挂起。
PROPAGATION_NEVER 即使当前有事务,Spring也会在飞事务环境下执行。如果当前有事务,则抛出异常。
PROPAGATION_NESTED 如果当前有事务,则在嵌套事务中执行。如果没有,那么执行情况与PROPAGATION_REQUIRED一样。

使用TransactionStatus接口:
view plaincopy to clipboardprint?
package org.springframework.transaction;

public interface TransactionStatus extends SavepointManager {

boolean isNewTransaction();  

boolean hasSavepoint();  

void setRollbackOnly();  

boolean isRollbackOnly();  

boolean isCompleted();  

}

setRollbackOnly:将一个事务表示为不可提交的。

PlatformTransactionManager的实现:
使用TransactionDefinition和TransactionStatus接口,创建并管理事务。
DataSourceTransactionManager控制着从DataSource中获得的JDBC Connection上的事务执行;
HibernateTransactionManager控制着Hibernate session上的事务执行;
JdoTransactionManager管理着JDO事务;
JtaTransactionManager将事务管理委托给JTA。

例如:
JDBC:
view plaincopy to clipboardprint?



<tx:advice id=“bookShopTx”
transaction-manager=“transactionManager”>
tx:attributes
<tx:method name=“purchase”
propagation=“REQUIRES_NEW”
isolation=“READ_COMMITTED”
rollback-for=“java.lang.ArithmeticException”/>
</tx:attributes>
</tx:advice>

aop:config

<aop:pointcut expression=“execution(* cn.partner4java.spring.transaction.BookShopService.*(…))”
id=“txPointCut”/>

<!-- 把切入点和事务通知联系起来: 既声明一个增强器 -->  
<aop:advisor advice-ref="bookShopTx" pointcut-ref="txPointCut"/>  

</aop:config>

Hibernate:
view plaincopy to clipboardprint?





<tx:advice id=“txAdvice” transaction-manager=“transactionManager”>
tx:attributes
<tx:method name=“new*” propagation=“REQUIRED” isolation=“DEFAULT” />
<tx:method name=“save*” propagation=“REQUIRED” isolation=“DEFAULT” />
<tx:method name=“update*” propagation=“REQUIRED” isolation=“DEFAULT” />
<tx:method name=“delete*” propagation=“REQUIRED” isolation=“DEFAULT” />
<tx:method name=“bulk*” propagation=“REQUIRED” isolation=“DEFAULT” />
<tx:method name=“load*” propagation=“REQUIRED” isolation=“DEFAULT” read-only=“true”/>
<tx:method name=“get*” propagation=“REQUIRED” isolation=“DEFAULT” read-only=“true”/>
<tx:method name=“query*” propagation=“REQUIRED” isolation=“DEFAULT” read-only=“true”/>
<tx:method name=“find*” propagation=“REQUIRED” isolation=“DEFAULT” read-only=“true”/>
<tx:method name=“is*” propagation=“REQUIRED” isolation=“DEFAULT” read-only=“true”/>

    <tx:method name="*" propagation="SUPPORTS" isolation="DEFAULT" />  
</tx:attributes>  

</tx:advice>
aop:config
<aop:advisor pointcut=“execution(* service.(…))” advice-ref=“txAdvice” />
</aop:config>

<context:component-scan base-package=“com.bytter”></context:component-scan>

tx:annotation-driven/

JPA:
view plaincopy to clipboardprint?






<tx:annotation-driven transaction-manager=“transactionManager”/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值