Spring学习-29:Spring中的事务管理之事务开发常用API的详解
2017-04-13 20:49
633 查看
分层开发:事务处在Service层。
Spring事务管理高层抽象主要包括3个接口,它们的常见方法:
(1)PlatformTransactionManager:平台事务管理器
(2)TransactionDefinition:事务定义
ISOLation_XXX:设置事务的隔离级别
PROPAGATION_XXX:设置事务的传播行为。不是JDBC中有的,是为了解决实际开发中的问题,后面会详细的说。
TIMEOUT_DEFAULT:过期时间
(3)TransactionStatus:事务状态
是否有保存点,是否一个新事务,是否已经提交,是否已经回滚
这三个接口的关系:
PlatformTransactionManager通过TransactionDefinition设置事务相关信息管理事务,管理事务过程中,产生一些事务状态:状态由TransactionStatus记录.
API详解:
PlatformTransactionManager:接口.
Spring为不同的持久化框架提供了不同PlatformTransactionManager接口实现
org.springframework.jdbc.datasource.DataSourceTransactionManager : 使用SpringJDBC或iBatis进行持久化数据时使用
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事务管理,用来解决实际开发的问题.)传播行为:解决业务层之间的调用的事务的关系.
![](http://img.blog.csdn.net/20170413205233444?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRG92ZV9Lbm93bGVkZ2U=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
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
Spring事务管理高层抽象主要包括3个接口,它们的常见方法:
(1)PlatformTransactionManager:平台事务管理器
Method Summary | |
---|---|
void | commit(TransactionStatus status) 提交 |
TransactionStatus | getTransaction(TransactionDefinition definition) 获取事务 |
void | rollback(TransactionStatus status) 回滚 |
ISOLation_XXX:设置事务的隔离级别
PROPAGATION_XXX:设置事务的传播行为。不是JDBC中有的,是为了解决实际开发中的问题,后面会详细的说。
TIMEOUT_DEFAULT:过期时间
Field Summary | |
---|---|
staticint | ISOLATION_DEFAULT Use the default isolation level of the underlying datastore. |
staticint | ISOLATION_READ_COMMITTED Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur. |
staticint | ISOLATION_READ_UNCOMMITTED Indicates that dirty reads, non-repeatable reads and phantom reads can occur. |
staticint | ISOLATION_REPEATABLE_READ Indicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur. |
staticint | ISOLATION_SERIALIZABLE Indicates that dirty reads, non-repeatable reads and phantom reads are prevented. |
staticint | PROPAGATION_MANDATORY Support a current transaction; throw an exception if no current transaction exists. |
staticint | PROPAGATION_NESTED Execute within a nested transaction if a current transaction exists, behave like PROPAGATION_REQUIREDelse. |
staticint | PROPAGATION_NEVER Do not support a current transaction; throw an exception if a current transaction exists. |
staticint | PROPAGATION_NOT_SUPPORTED Do not support a current transaction; rather always execute non-transactionally. |
staticint | PROPAGATION_REQUIRED Support a current transaction; create a new one if none exists. |
staticint | PROPAGATION_REQUIRES_NEW Create a new transaction, suspending the current transaction if one exists. |
staticint | PROPAGATION_SUPPORTS Support a current transaction; execute non-transactionally if none exists. |
staticint | TIMEOUT_DEFAULT Use the default timeout of the underlying transaction system, or none if timeouts are not supported. |
Method Summary | |
---|---|
int | getIsolationLevel() Return the isolation level. |
String | getName() Return the name of this transaction. |
int | getPropagationBehavior() Return the propagation behavior. |
int | getTimeout() Return the transaction timeout. |
boolean | isReadOnly() Return whether to optimize as a read-only transaction. |
是否有保存点,是否一个新事务,是否已经提交,是否已经回滚
Method Summary | |
---|---|
void | flush() Flush the underlying session to the datastore, if applicable: for example, all affected Hibernate/JPA sessions. |
boolean | hasSavepoint() Return whether this transactioninternally carries a savepoint, that is, has been created as nested transaction based on a savepoint. |
boolean | isCompleted() Return whether this transaction is completed, that is, whether it has already been committed or rolled back. |
boolean | isNewTransaction() 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). |
boolean | isRollbackOnly() Return whether the transaction has been marked as rollback-only (either by the application or by the transaction infrastructure). |
void | setRollbackOnly() Set the transaction rollback-only. |
PlatformTransactionManager通过TransactionDefinition设置事务相关信息管理事务,管理事务过程中,产生一些事务状态:状态由TransactionStatus记录.
API详解:
PlatformTransactionManager:接口.
Spring为不同的持久化框架提供了不同PlatformTransactionManager接口实现
org.springframework.jdbc.datasource.DataSourceTransactionManager : 使用SpringJDBC或iBatis进行持久化数据时使用
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
相关文章推荐
- 学习《spring 3.x企业应用开发实战》之Spring的事务管理难点剖析
- 详解Spring学习之编程式事务管理
- Spring视频学习(九)使用Spring注解方式管理事务与传播行为详解
- JAVAWEB开发之Spring详解之——Spring事务管理详解(四种事务管理方式)
- JAVAWEB开发之工作流详解(二)——Activiti核心API的使用(流程定义和流程实例的管理、流程变量、监听器...)以及与Spring的集成
- Spring 注解方式进行事务管理的用法介绍详解【Java学习笔记】
- Spring学习之Spring的声明式事务管理详解
- 学习《spring 3.x企业应用开发实战》之Spring的事务管理
- spring 事务管理详解 学习心得
- 详解Spring学习之声明式事务管理
- Spring学习之Spring的声明式事务管理详解
- 使用Spring注解方式管理事务与传播行为详解
- Spring中的事务管理实例详解
- Spring学习笔记五:Spring进行事务管理
- Spring3.0学习-注解模式下的Spring3+hibernate3+struts2-事务管理(xml模式和注解模式的事务注入+@Repository@Service@Transactional)
- 【Todo】Java学习笔记 100==100 & Reflection API & Optional类详解 & DIP、IoC、DI & token/cookie/session管理会话方式
- Spring学习笔记 事务管理
- Java框架spring 学习笔记(十九):事务管理(注解管理)
- Spring学习-34:Spring中的事务管理之声明式事务(基于注解)
- JDBC学习01-JDBC开发步骤及API详解(IntelliJ IDEA)