Spring学习-29:Spring中的事务管理之事务开发常用API的详解
来源:互联网 发布:我想在淘宝上卖东西该怎么办 编辑:程序博客网 时间:2024/06/14 20:31
分层开发:事务处在Service层。
Spring事务管理高层抽象主要包括3个接口,它们的常见方法:
(1)PlatformTransactionManager:平台事务管理器
void
commit(TransactionStatus status)
提交
TransactionStatus
getTransaction(TransactionDefinition definition)
获取事务
void
rollback(TransactionStatus status)
回滚
(2)TransactionDefinition:事务定义
ISOLation_XXX:设置事务的隔离级别
PROPAGATION_XXX:设置事务的传播行为。不是JDBC中有的,是为了解决实际开发中的问题,后面会详细的说。
TIMEOUT_DEFAULT:过期时间
static int
ISOLATION_DEFAULT
Use the default isolation level of the underlying datastore.
static int
ISOLATION_READ_COMMITTED
Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur.
static int
ISOLATION_READ_UNCOMMITTED
Indicates that dirty reads, non-repeatable reads and phantom reads can occur.
static int
ISOLATION_REPEATABLE_READ
Indicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur.
static int
ISOLATION_SERIALIZABLE
Indicates that dirty reads, non-repeatable reads and phantom reads are prevented.
static int
PROPAGATION_MANDATORY
Support a current transaction; throw an exception if no current transaction exists.
static int
PROPAGATION_NESTED
Execute within a nested transaction if a current transaction exists, behave like
PROPAGATION_REQUIRED
else.static int
PROPAGATION_NEVER
Do not support a current transaction; throw an exception if a current transaction exists.
static int
PROPAGATION_NOT_SUPPORTED
Do not support a current transaction; rather always execute non-transactionally.
static int
PROPAGATION_REQUIRED
Support a current transaction; create a new one if none exists.
static int
PROPAGATION_REQUIRES_NEW
Create a new transaction, suspending the current transaction if one exists.
static int
PROPAGATION_SUPPORTS
Support a current transaction; execute non-transactionally if none exists.
static int
TIMEOUT_DEFAULT
Use the default timeout of the underlying transaction system, or none if timeouts are not supported.
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.
(3)TransactionStatus:事务状态
是否有保存点,是否一个新事务,是否已经提交,是否已经回滚
void
flush()
Flush the underlying session to the datastore, if applicable: for example, all affected Hibernate/JPA sessions.
boolean
hasSavepoint()
Return whether this transaction internally 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学习-29:Spring中的事务管理之事务开发常用API的详解
- Spring学习-28:Spring中的事务管理之事务的回顾
- Spring学习之Spring的声明式事务管理详解
- Spring学习之Spring的声明式事务管理详解
- Spring框架事务管理之二:事务管理器与事务API的配置
- Spring学习-32:Spring中的事务管理之声明式事务(原始方式,基于TransactionProxyBean)
- Spring学习-33:Spring中的事务管理之声明式事务(基于切面自动代理)
- Spring学习-34:Spring中的事务管理之声明式事务(基于注解)
- Spring之事务管理详解
- 【Spring学习】之 事务管理
- JAVAWEB开发之Spring详解之——Spring事务管理详解(四种事务管理方式)
- Spring学习-30:Spring中的事务管理之环境搭建
- Spring中的事务管理实例详解
- Spring中的事务管理实例详解
- 事务以及Spring中的事务管理一
- 事务以及Spring中的事务管理二
- Spring学习-31:Spring中的事务管理之手动编码的方式
- spring学习之---spring的事务
- POJ
- URAL
- Opencv学习---霍夫变换检测圆的基本原理
- Bulbs 【模拟】or 【set】
- 第二部分 自动内存管理机制
- Spring学习-29:Spring中的事务管理之事务开发常用API的详解
- Xcode清理缓存和垃圾文件
- JSP (java server page) servlet生命周期
- Win10 将开启“游戏模式”
- 删数问题
- win7 装固态升级win10 心得
- JS性能优化之创建文档碎片(document.createDocumentFragment)
- 185
- PHP性能测试工具xhprof的安装和使用