Spring学习-29:Spring中的事务管理之事务开发常用API的详解

来源:互联网 发布:我想在淘宝上卖东西该怎么办 编辑:程序博客网 时间:2024/06/14 20:31

分层开发:事务处在Service层。

Spring事务管理高层抽象主要包括3个接口,它们的常见方法:

(1)PlatformTransactionManager:平台事务管理器

Method Summary voidcommit(TransactionStatus status)
         提交 TransactionStatusgetTransaction(TransactionDefinition definition)
         获取事务 voidrollback(TransactionStatus status)
       回滚

(2)TransactionDefinition:事务定义

ISOLation_XXX:设置事务的隔离级别

PROPAGATION_XXX:设置事务的传播行为。不是JDBC中有的,是为了解决实际开发中的问题,后面会详细的说。

TIMEOUT_DEFAULT:过期时间

Field Summarystatic intISOLATION_DEFAULT
          Use the default isolation level of the underlying datastore.static intISOLATION_READ_COMMITTED
          Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur.static intISOLATION_READ_UNCOMMITTED
          Indicates that dirty reads, non-repeatable reads and phantom reads can occur.static intISOLATION_REPEATABLE_READ
          Indicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur.static intISOLATION_SERIALIZABLE
          Indicates that dirty reads, non-repeatable reads and phantom reads are prevented.static intPROPAGATION_MANDATORY
          Support a current transaction; throw an exception if no current transaction exists.static intPROPAGATION_NESTED
          Execute within a nested transaction if a current transaction exists, behave likePROPAGATION_REQUIRED else.static intPROPAGATION_NEVER
          Do not support a current transaction; throw an exception if a current transaction exists.static intPROPAGATION_NOT_SUPPORTED
          Do not support a current transaction; rather always execute non-transactionally.static intPROPAGATION_REQUIRED
          Support a current transaction; create a new one if none exists.static intPROPAGATION_REQUIRES_NEW
          Create a new transaction, suspending the current transaction if one exists.static intPROPAGATION_SUPPORTS
          Support a current transaction; execute non-transactionally if none exists.static intTIMEOUT_DEFAULT
          Use the default timeout of the underlying transaction system, or none if timeouts are not supported.

Method Summary intgetIsolationLevel()
          Return the isolation level. StringgetName()
          Return the name of this transaction. intgetPropagationBehavior()
          Return the propagation behavior. intgetTimeout()
          Return the transaction timeout. booleanisReadOnly()
          Return whether to optimize as a read-only transaction.

(3)TransactionStatus:事务状态

是否有保存点,是否一个新事务,是否已经提交,是否已经回滚

Method Summary voidflush()
          Flush the underlying session to the datastore, if applicable: for example, all affected Hibernate/JPA sessions. booleanhasSavepoint()
          Return whether this transaction internally carries a savepoint, that is, has been created as nested transaction based on a savepoint. booleanisCompleted()
          Return whether this transaction is completed, that is, whether it has already been committed or rolled back. booleanisNewTransaction()
          Return whether the present transaction is new (else participating in an existing transaction, or potentially not running in an actual transaction in the first place). booleanisRollbackOnly()
          Return whether the transaction has been marked as rollback-only (either by the application or by the transaction infrastructure). voidsetRollbackOnly()
          Set the transaction rollback-only.

这三个接口的关系:

PlatformTransactionManager通过TransactionDefinition设置事务相关信息管理事务,管理事务过程中,产生一些事务状态:状态由TransactionStatus记录.

API详解:

PlatformTransactionManager:接口.

Spring为不同的持久化框架提供了不同PlatformTransactionManager接口实现

 org.springframework.jdbc.datasource.DataSourceTransactionManager :  使用SpringJDBCiBatis进行持久化数据时使用

org.springframework.orm.hibernate3.HibernateTransactionManager       :  使用Hibernate3.0版本进行持久化数据时使用

org.springframework.orm.jpa.JpaTransactionManager    使用JPA进行持久化时使用

org.springframework.jdo.JdoTransactionManager    当持久化机制是Jdo时使用

org.springframework.transaction.jta.JtaTransactionManager       使用一个JTA实现来管理事务,在一个事务跨越多个资源时必须使用

TransactionDefinition:接口

ISOLation_DEFAULT:默认级别 MySQL:repeatable_read;Oracle:read_commited

事务的传播行为:(不是JDBC事务管理,用来解决实际开发的问题.)传播行为:解决业务层之间的调用的事务的关系.


PROPAGATION_REQUIRED     :支持当前事务,如果不存在就新建一个

* A,B    如果A有事务,B使用A的事务,如果A没有事务,B就开启一个新的事务.(A,B是在一个事务中。)

PROPAGATION_SUPPORTS     :支持当前事务,如果不存在,就不使用事务

* A,B    如果A有事务,B使用A的事务,如果A没有事务,B就不使用事务.

PROPAGATION_MANDATORY    :支持当前事务,如果不存在,抛出异常

* A,B    如果A有事务,B使用A的事务,如果A没有事务,抛出异常.

PROPAGATION_REQUIRES_NEW 如果有事务存在,挂起当前事务,创建一个新的事务

* A,B    如果A有事务,B将A的事务挂起,重新创建一个新的事务.(A,B不在一个事务中.事务互不影响.)

PROPAGATION_NOT_SUPPORTED    以非事务方式运行,如果有事务存在,挂起当前事务

* A,B    非事务的方式运行,A有事务,就会挂起当前的事务.

PROPAGATION_NEVER    以非事务方式运行,如果有事务存在,抛出异常

PROPAGATION_NESTED   如果当前事务存在,则嵌套事务执行

* 基于SavePoint技术.

* A,B    A有事务,A执行之后,将A事务执行之后的内容保存到SavePoint.B事务有异常的话,用户需要自己设置事务提交还是回滚.

 

* 常用:(重点)

PROPAGATION_REQUIRED

PROPAGATION_REQUIRES_NEW

PROPAGATION_NESTED



0 0