Spring Transaction 分析事务属性(事务的基本概念、配置)

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


探索TransactionDefinition接口:
  1. packageorg.springframework.transaction;
  2. publicinterfaceTransactionDefinition {
  3. intgetPropagationBehavior();
  4. intgetIsolationLevel();
  5. intgetTimeout();
  6. booleanisReadOnly();
  7. String getName();
  8. }


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接口:
  1. packageorg.springframework.transaction;
  2. publicinterfaceTransactionStatusextendsSavepointManager {
  3. booleanisNewTransaction();
  4. booleanhasSavepoint();
  5. voidsetRollbackOnly();
  6. booleanisRollbackOnly();
  7. booleanisCompleted();
  8. }

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




PlatformTransactionManager的实现:
使用TransactionDefinition和TransactionStatus接口,创建并管理事务。

DataSourceTransactionManager控制着从DataSource中获得的JDBC Connection上的事务执行;
HibernateTransactionManager控制着Hibernate session上的事务执行;
JdoTransactionManager管理着JDO事务;
JtaTransactionManager将事务管理委托给JTA。

例如:
JDBC:
  1. <!-- 声明事务处理器 -->
  2. <bean id="transactionManager"
  3. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  4. <property name="dataSource"ref="dataSource"></property>
  5. </bean>
  6. <!-- 声明事务通知 -->
  7. <tx:advice id="bookShopTx"
  8. transaction-manager="transactionManager">
  9. <tx:attributes>
  10. <tx:method name="purchase"
  11. propagation="REQUIRES_NEW"
  12. isolation="READ_COMMITTED"
  13. rollback-for="java.lang.ArithmeticException"/>
  14. </tx:attributes>
  15. </tx:advice>
  16. <!-- 声明事务通知需要通知哪些类的那些方法, 即: 那些方法受事务管理 -->
  17. <aop:config>
  18. <!-- 声明切入点 -->
  19. <aop:pointcut expression="execution(* cn.partner4java.spring.transaction.BookShopService.*(..))"
  20. id="txPointCut"/>
  21. <!-- 把切入点和事务通知联系起来: 既声明一个增强器 -->
  22. <aop:advisor advice-ref="bookShopTx"pointcut-ref="txPointCut"/>
  23. </aop:config>

Hibernate:
  1. <bean id="sessionFactory"
  2. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  3. <property name="configLocation"value="classpath:hibernate.cfg.xml"></property>
  4. <property name="dataSource"ref="dataSource"></property>
  5. </bean>
  6. <bean id="transactionManager"
  7. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  8. <property name="sessionFactory"ref="sessionFactory"></property>
  9. </bean>
  10. <!-- 事务通知 -->
  11. <tx:advice id="txAdvice"transaction-manager="transactionManager">
  12. <tx:attributes>
  13. <tx:method name="new*"propagation="REQUIRED"isolation="DEFAULT"/>
  14. <tx:method name="save*"propagation="REQUIRED"isolation="DEFAULT"/>
  15. <tx:method name="update*"propagation="REQUIRED"isolation="DEFAULT"/>
  16. <tx:method name="delete*"propagation="REQUIRED"isolation="DEFAULT"/>
  17. <tx:method name="bulk*"propagation="REQUIRED"isolation="DEFAULT"/>
  18. <tx:method name="load*"propagation="REQUIRED"isolation="DEFAULT"read-only="true"/>
  19. <tx:method name="get*"propagation="REQUIRED"isolation="DEFAULT"read-only="true"/>
  20. <tx:method name="query*"propagation="REQUIRED"isolation="DEFAULT"read-only="true"/>
  21. <tx:method name="find*"propagation="REQUIRED"isolation="DEFAULT"read-only="true"/>
  22. <tx:method name="is*"propagation="REQUIRED"isolation="DEFAULT"read-only="true"/>
  23. <tx:method name="*"propagation="SUPPORTS"isolation="DEFAULT"/>
  24. </tx:attributes>
  25. </tx:advice>
  26. <aop:config>
  27. <aop:advisor pointcut="execution(* *..*service*.*(..))"advice-ref="txAdvice"/>
  28. </aop:config>
  29. <context:component-scan base-package="com.bytter"></context:component-scan>
  30. <tx:annotation-driven/>

JPA:
  1. <bean id="entityManagerFactory"class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  2. <property name="dataSource"ref="dataSource"/>
  3. <property name="persistenceXmlLocation"value="classpath:META-INF/persistence.xml"/>
  4. <property name="loadTimeWeaver">
  5. <beanclass="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
  6. </property>
  7. </bean>
  8. <bean id="transactionManager"class="org.springframework.orm.jpa.JpaTransactionManager">
  9. <property name="entityManagerFactory"ref="entityManagerFactory"/>
  10. </bean>
  11. <tx:annotation-driven transaction-manager="transactionManager"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值