spring事务管理

来源:互联网 发布:ecshop h5微商城源码 编辑:程序博客网 时间:2024/05/20 19:48

贴上地址:原文地址

spring 事务管理

数据库事务是被当作单个工作单元的操作序列。这些操作要么全部完成或全部不成功。事务管理是面向企业应用程序,以确保数据的完整性和一致性RDBMS中的重要组成部分。事务的概念可以用下面的描述为ACID四个关键属性来描述: www.yiibai.com

  1. 原子性: 一个事务应该被视为单个操作单元表示的操作的任一整个序列是成功的或不成功的。

  2. 一致性: 这代表了数据库的参照完整性,在桌等唯一主键的一致性

  3. 隔离性: 可能有很多事务处理相同的数据集的同时,每个事务都应由他人隔离,以防止数据损坏。

  4. 持久性: 一旦事务完成,本次事务的结果必须作出永久性的,不能从数据库中删除因系统故障。

一个真正的RDBMS数据库系统将保证所有的四个属性为每个事务。颁发给使用SQL数据库的事务的简单观点如下:www.yiibai.com

  • 使用begin transaction命令开始事务。

  • 使用SQL查询执行各种删除,更新或插入操作。

  • 如果所有的操作都成功,那么执行提交,否则回滚所有操作。

Spring框架提供的不同的底层事务管理API之上的抽象层。在Spring的事务支持,旨在通过增加事务功能,的POJO提供EJB的替代品事务。 Spring支持两种编程式和声明式事务管理。需要的EJB应用程序服务器,但Spring事务管理,而不需要一个应用服务器来实现。 www.yiibai.com

局部与全局事务

局部事务是针对像一个JDBC连接一个单一的事务性资源,而全局事务可以跨越像事务多个事务资源的分布式系统。www.yiibai.com

局部事务管理可以在一个集中式计算环境下的应用程序的组件和资源都位于一个单一的网站是有用的,而事务管理只涉及一个单独的机器上运行的本地数据管理。局部事务更容易实现。

全局事务管理,需要在分布在多个系统中的所有资源的分布式计算环境。在这种情况下,事务管理既需要在地方和全局层面的工作要做。一个分布式或全局事务在多个系统上执行,其执行需要全局事务管理系统和所有相关系统的所有局部数据管理人员之间的协调。

编程与声明

Spring支持两种类型的事务管理: yiibai.com

  1. 编程式事务管理: Spring支持两种类型的事务管理:

  2. 声明式事务管理: 这意味着你的业务代码分开事务管理。你只用注释或基于XML 配置来管理事务。

声明式事务管理要优于编程式事务管理,虽然它比编程式事务管理,它允许你通过代码来控制事务不够灵活。但作为一种横切关注点的,声明式事务管理可以用模块化的AOP方法。Spring通过Spring AOP框架支持声明式事务管理。 yiibai.com

Spring事务抽象

关键Spring事务抽象的定义是org.springframework.transaction.PlatformTransactionManager接口,如下所示:

public interface PlatformTransactionManager {   TransactionStatus getTransaction(TransactionDefinition definition);   throws TransactionException;   void commit(TransactionStatus status) throws TransactionException;   void rollback(TransactionStatus status) throws TransactionException;} yiibai.com 
S.N.方法 & 描述1TransactionStatus getTransaction(TransactionDefinition definition) 
This method returns a currently active transaction or create a new one, according to the specified propagation behavior.2void commit(TransactionStatus status)
This method commits the given transaction, with regard to its status.3void rollback(TransactionStatus status)
This method performs a rollback of the given transaction.

TransactionDefinition是在Spring中事务支持的核心接口,它的定义如下:

public interface TransactionDefinition {   int getPropagationBehavior();   int getIsolationLevel();   String getName();   int getTimeout();   boolean isReadOnly();}  
S.N.方法 & 描述1int getPropagationBehavior() yiibai.com 
This method returns the propagation behavior. Spring offers all of the transaction propagation options familiar from EJB CMT.2int getIsolationLevel()
This method returns the degree to which this transaction is isolated from the work of other transactions.3String getName()
This method returns the name of this transaction.4int getTimeout() 
This method returns the time in seconds in which the transaction must complete.5boolean isReadOnly()
This method returns whether the transaction is read-only.

下面是隔离级别可能的值:

S.N.隔离& 描述1TransactionDefinition.ISOLATION_DEFAULT yiibai.com 
This is the default isolation level.2TransactionDefinition.ISOLATION_READ_COMMITTED
Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur.3TransactionDefinition.ISOLATION_READ_UNCOMMITTED
Indicates that dirty reads, non-repeatable reads and phantom reads can occur.4TransactionDefinition.ISOLATION_REPEATABLE_READ 
Indicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur.5TransactionDefinition.ISOLATION_SERIALIZABLE
Indicates that dirty reads, non-repeatable reads and phantom reads are prevented.

以下是针对Propagation 类型可能的值:

S.N.传输& 描述1TransactionDefinition.PROPAGATION_MANDATORY 
Support a current transaction; throw an exception if no current transaction exists.2TransactionDefinition.PROPAGATION_NESTED 
Execute within a nested transaction if a current transaction exists.3TransactionDefinition.PROPAGATION_NEVER 
Do not support a current transaction; throw an exception if a current transaction exists.4TransactionDefinition.PROPAGATION_NOT_SUPPORTED  
Do not support a current transaction; rather always execute non-transactionally.5TransactionDefinition.PROPAGATION_REQUIRED 
Support a current transaction; create a new one if none exists.6TransactionDefinition.PROPAGATION_REQUIRES_NEW 
Create a new transaction, suspending the current transaction if one exists.7TransactionDefinition.PROPAGATION_SUPPORTS  
Support a current transaction; execute non-transactionally if none exists.8TransactionDefinition.TIMEOUT_DEFAULT 
Use the default timeout of the underlying transaction system, or none if timeouts are not supported.

TransactionStatus接口为处理事务的代码来控制事务执行和查询事务状态的简单方法。

public interface TransactionStatus extends SavepointManager {   boolean isNewTransaction();   boolean hasSavepoint();   void setRollbackOnly();   boolean isRollbackOnly();   boolean isCompleted();}  
S.N.方法 & 描述1boolean hasSavepoint() 
This method returns whether this transaction internally carries a savepoint, that is, has been created as nested transaction based on a savepoint.2boolean isCompleted()
This method returns whether this transaction is completed, that is, whether it has already been committed or rolled back.3boolean isNewTransaction() 
This method returns true in case the present transaction is new.4boolean isRollbackOnly()
This method returns whether the transaction has been marked as rollback-only.5void setRollbackOnly() 
This method sets the transaction rollback-only.
0 0
原创粉丝点击