数据库事务是被当作单个工作单元的操作序列。这些操作要么全部完成或全部不成功。事务管理是面向企业应用程序,以确保数据的完整性和一致性RDBMS中的重要组成部分。事务的概念可以用下面的描述为ACID四个关键属性来描述:
-
原子性: 一个事务应该被视为单个操作单元表示的操作的任一整个序列是成功的或不成功的。
-
一致性: 这代表了数据库的参照完整性,在桌等唯一主键的一致性
-
隔离性: 可能有很多事务处理相同的数据集的同时,每个事务都应由他人隔离,以防止数据损坏。
-
持久性: 一旦事务完成,本次事务的结果必须作出永久性的,不能从数据库中删除因系统故障。
一个真正的RDBMS数据库系统将保证所有的四个属性为每个事务。颁发给使用SQL数据库的事务的简单观点如下:
-
使用begin transaction命令开始事务。
-
使用SQL查询执行各种删除,更新或插入操作。
-
如果所有的操作都成功,那么执行提交,否则回滚所有操作。
Spring框架提供的不同的底层事务管理API之上的抽象层。在Spring的事务支持,旨在通过增加事务功能,的POJO提供EJB的替代品事务。 Spring支持两种编程式和声明式事务管理。需要的EJB应用程序服务器,但Spring事务管理,而不需要一个应用服务器来实现。
局部与全局事务
局部事务是针对像一个JDBC连接一个单一的事务性资源,而全局事务可以跨越像事务多个事务资源的分布式系统。
局部事务管理可以在一个集中式计算环境下的应用程序的组件和资源都位于一个单一的网站是有用的,而事务管理只涉及一个单独的机器上运行的本地数据管理。局部事务更容易实现。
全局事务管理,需要在分布在多个系统中的所有资源的分布式计算环境。在这种情况下,事务管理既需要在地方和全局层面的工作要做。一个分布式或全局事务在多个系统上执行,其执行需要全局事务管理系统和所有相关系统的所有局部数据管理人员之间的协调。
编程与声明
Spring支持两种类型的事务管理:
声明式事务管理要优于编程式事务管理,虽然它比编程式事务管理,它允许你通过代码来控制事务不够灵活。但作为一种横切关注点的,声明式事务管理可以用模块化的AOP方法。Spring通过Spring AOP框架支持声明式事务管理。
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; }
S.N. | 方法 & 描述 |
---|---|
1 |
TransactionStatus getTransaction(TransactionDefinition definition) This method returns a currently active transaction or create a new one, according to the specified propagation behavior. |
2 |
void commit(TransactionStatus status) This method commits the given transaction, with regard to its status. |
3 |
void 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. | 方法 & 描述 |
---|---|
1 |
int getPropagationBehavior() This method returns the propagation behavior. Spring offers all of the transaction propagation options familiar from EJB CMT. |
2 |
int getIsolationLevel() This method returns the degree to which this transaction is isolated from the work of other transactions. |
3 |
String getName() This method returns the name of this transaction. |
4 |
int getTimeout() This method returns the time in seconds in which the transaction must complete. |
5 |
boolean isReadOnly() This method returns whether the transaction is read-only. |
下面是隔离级别可能的值:
S.N. | 隔离& 描述 |
---|---|
1 |
TransactionDefinition.ISOLATION_DEFAULT This is the default isolation level. |
2 |
TransactionDefinition.ISOLATION_READ_COMMITTED Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur. |
3 |
TransactionDefinition.ISOLATION_READ_UNCOMMITTED Indicates that dirty reads, non-repeatable reads and phantom reads can occur. |
4 |
TransactionDefinition.ISOLATION_REPEATABLE_READ Indicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur. |
5 |
TransactionDefinition.ISOLATION_SERIALIZABLE Indicates that dirty reads, non-repeatable reads and phantom reads are prevented. |
以下是针对Propagation 类型可能的值:
S.N. | 传输& 描述 |
---|---|
1 |
TransactionDefinition.PROPAGATION_MANDATORY Support a current transaction; throw an exception if no current transaction exists. |
2 |
TransactionDefinition.PROPAGATION_NESTED Execute within a nested transaction if a current transaction exists. |
3 |
TransactionDefinition.PROPAGATION_NEVER Do not support a current transaction; throw an exception if a current transaction exists. |
4 |
TransactionDefinition.PROPAGATION_NOT_SUPPORTED Do not support a current transaction; rather always execute non-transactionally. |
5 |
TransactionDefinition.PROPAGATION_REQUIRED Support a current transaction; create a new one if none exists. |
6 |
TransactionDefinition.PROPAGATION_REQUIRES_NEW Create a new transaction, suspending the current transaction if one exists. |
7 |
TransactionDefinition.PROPAGATION_SUPPORTS Support a current transaction; execute non-transactionally if none exists. |
8 |
TransactionDefinition.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. | 方法 & 描述 |
---|---|
1 |
boolean hasSavepoint() This method returns whether this transaction internally carries a savepoint, that is, has been created as nested transaction based on a savepoint. |
2 |
boolean isCompleted() This method returns whether this transaction is completed, that is, whether it has already been committed or rolled back. |
3 |
boolean isNewTransaction() This method returns true in case the present transaction is new. |
4 |
boolean isRollbackOnly() This method returns whether the transaction has been marked as rollback-only. |
5 |
void setRollbackOnly() This method sets the transaction rollback-only. |