事务Transaction的理解(一)
目录
什么是事务?
局部事务和全局事务
可编程事务管理和Java注解型事务管理
Spring抽象事务层
什么是事务?
数据库事务就是将数据库的一系列操作作为一个工作单元。这一系列的操作要么一起生效要么就全都不起作用。事务管理是数据库中非常重要的组成部分,它能确保数据的一致性和完整性。
事务具有如下几个特点:
- 原子性
- 一致性
- 隔离性
- 牢固性
一个关系型数据库的事务管理必须满足上边的四个特性,拿一个简单的SQL例子来展示一下,
用 begin transaction 来开始事务;
用SQL执行deleted, update or insert操作;
如果所有的这些操作执行成功,那就执行commit 否则的话 rollback所有的操作。
Spring或者Spring Boot在各种各样的事务Transaction管理API上封装了一层抽象层。Spring Boot 提供了两种事务管理方式:可编程型和Java注解型事务管理, 在下一系列来分别阐述其使用详解。
局部事务和全局事务
局部事务就是指单一资源的事务管理,比方说一个JDBC连接。而全局事务扩展到多个事务资源,比如分布式事务。
如果应用程序和数据库部署在单一位置上(application,resource可不在同一机器上),事务也仅仅是在一个机器上进行的数据管理,在这种情况下,局部事务是非常有效的。
而在分布式环境里, 所有的资源会部署到多个系统里,在这种情况下,事务管理要在局部层和全局层都要生效。一个分布式事务或者全局事务运行在多个系统里,这就要求全局事务管理和局部的数据管理必须协调一致地运行下去。
可编程事务管理和Java注解型事务管理
Spring和Spring Boot 支持两种类型的事务管理:
- 可编程事务管理:可以通过自己编程来实现事务管理,在一定程度上带来复杂性,相对而言较难维护。
- 注解型事务管理:可以将事务管理与业务逻辑相分离,可以通过注解或者XML配置来实现。
关于这一节内容,会在Transaction理解(二)和(三)里分别阐述。
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;}
Method | Description |
---|---|
getTransaction | Return a currently active transaction or create a new one, according to the specified propagation behavior. |
commit | Commit the given transaction, with regard to its status. If the transaction has been marked rollback-only programmatically, perform a rollback. |
rollback | Perform a rollback of the given transaction. |
另外一个核心组成部分是TransactionDefinition接口
public interface TransactionDefinition {int PROPAGATION_REQUIRED = 0;int PROPAGATION_SUPPORTS = 1;int PROPAGATION_MANDATORY = 2;int PROPAGATION_REQUIRES_NEW = 3;int PROPAGATION_NOT_SUPPORTED = 4;int PROPAGATION_NEVER = 5;int PROPAGATION_NESTED = 6;int ISOLATION_DEFAULT = -1;int ISOLATION_READ_UNCOMMITTED = Connection.TRANSACTION_READ_UNCOMMITTED;int ISOLATION_READ_COMMITTED = Connection.TRANSACTION_READ_COMMITTED;int ISOLATION_REPEATABLE_READ = Connection.TRANSACTION_REPEATABLE_READ;int ISOLATION_SERIALIZABLE = Connection.TRANSACTION_SERIALIZABLE;int TIMEOUT_DEFAULT = -1;int getPropagationBehavior();int getIsolationLevel();int getTimeout();boolean isReadOnly();String getName();}
Method |
Description |
---|---|
getPropagationBehavior |
Return the propagation behavior. Must return one of the |
getIsolationLevel |
Return the isolation level. |
getTimeout | Return the transaction timeout. |
isReadOnly | Return whether to optimize as a read-only transaction. |
getName |
Return the name of this transaction. Can be |
接下来列举一下如何事务的隔离性
Isolation | Description |
---|---|
ISOLATION_DEFAULT | Use the default isolation level of the underlying datastore. All other levels correspond to the JDBC isolation levels. |
ISOLATION_READ_UNCOMMITTED | Indicates that dirty reads, non-repeatable reads and phantom reads can occur |
ISOLATION_READ_COMMITTED | Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur. |
ISOLATION_REPEATABLE_READ | Indicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur. |
ISOLATION_SERIALIZABLE | Indicates that dirty reads, non-repeatable reads and phantom reads are prevented. |
接下来还有事务之间的关系
Propagation | Description |
---|---|
PROPAGATION_REQUIRED |
Support a current transaction; create a new one if none exists. Analogous to the EJB transaction attribute of the same name. |
PROPAGATION_SUPPORTS |
Support a current transaction; execute non-transactionally if none exists. Analogous to the EJB transaction attribute of the same name. |
PROPAGATION_MANDATORY |
Support a current transaction; throw an exception if no current transaction exists. Analogous to the EJB transaction attribute of the same name. |
PROPAGATION_REQUIRES_NEW |
Create a new transaction, suspending the current transaction if one exists. Analogous to the EJB transaction attribute of the same name. |
PROPAGATION_NOT_SUPPORTED |
Do not support a current transaction; rather always execute non-transactionally. Analogous to the EJB transaction attribute of the same name. |
PROPAGATION_NEVER |
Do not support a current transaction; throw an exception if a current transaction exists. Analogous to the EJB transaction attribute of the same name. |
PROPAGATION_NESTED |
Execute within a nested transaction if a current transaction exists, behave like |
TIMEOUT_DEFAULT | Use the default timeout of the underlying transaction system, or none if timeouts are not supported. |
TransactionStatus 接口提供了一系列管理事务和获取事务状态的方式方法。
public interface TransactionStatus extends SavepointManager, Flushable {/*** 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 isNewTransaction();/*** Return whether this transaction internally carries a savepoint,* that is, has been created as nested transaction based on a savepoint.* <p>This method is mainly here for diagnostic purposes, alongside* {@link #isNewTransaction()}. For programmatic handling of custom* savepoints, use SavepointManager's operations.* @see #isNewTransaction()* @see #createSavepoint* @see #rollbackToSavepoint(Object)* @see #releaseSavepoint(Object)*/boolean hasSavepoint();/*** Set the transaction rollback-only. This instructs the transaction manager* that the only possible outcome of the transaction may be a rollback, as* alternative to throwing an exception which would in turn trigger a rollback.* <p>This is mainly intended for transactions managed by* {@link org.springframework.transaction.support.TransactionTemplate} or* {@link org.springframework.transaction.interceptor.TransactionInterceptor},* where the actual commit/rollback decision is made by the container.* @see org.springframework.transaction.support.TransactionCallback#doInTransaction* @see org.springframework.transaction.interceptor.TransactionAttribute#rollbackOn*/void setRollbackOnly();/*** Return whether the transaction has been marked as rollback-only* (either by the application or by the transaction infrastructure).*/boolean isRollbackOnly();/*** Flush the underlying session to the datastore, if applicable:* for example, all affected Hibernate/JPA sessions.* <p>This is effectively just a hint and may be a no-op if the underlying* transaction manager does not have a flush concept. A flush signal may* get applied to the primary resource or to transaction synchronizations,* depending on the underlying resource.*/@Overridevoid flush();/*** Return whether this transaction is completed, that is,* whether it has already been committed or rolled back.* @see PlatformTransactionManager#commit* @see PlatformTransactionManager#rollback*/boolean isCompleted();}
事务Transaction的理解(一)相关推荐
- 关于事务管理的理解和Spring事务管理详解
转载于:http://www.mamicode.com/info-detail-1248286.html 1 初步理解 理解事务之前,先讲一个你日常生活中最常干的事:取钱. 比如你去ATM机取1000 ...
- 基于MySQL 8.0 对事务的深度理解
基于MySQL 8.0 对事务的深度理解 一.MySQL中事务隔离级别 事务的隔离级别有哪些? 隔离级别 脏读 不可重复读 幻读(虚读) 未提交读(Read uncommitted) 可能 可能 可能 ...
- Java的acid是什么意思_事务的ACID理解
ACID,是指在可靠数据库管理系统(DBMS)中,事务(transaction)所应该具有的四个特性:原子性(Atomicity).一致性(Consistency).隔离性(Isolation).持久 ...
- FireDAC 下的 Sqlite [7] - 备份、优化、事务(Transaction)
用 TFDSQLiteBackup 控件, 两三行代码即可完成 Sqlite 数据库的备份. procedure TForm1.Button1Click(Sender: TObject); begin ...
- 化零为整WCF(14) - 事务(Transaction)
[索引页] [源码下载] 化零为整WCF(14) - 事务(Transaction) 作者:webabcd 介绍 WCF(Windows Communication Foundation) - 事务( ...
- 什么是事务(Transaction)
摘要:事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元.事务通常由高级数据库操纵语言或编程语言书写的用户程序的执行所引起,并用形如begin transaction和 ...
- MySQL事务(transaction) (有这篇就足够了..)
MySQL事务处理(TransAction) 大家好,我是胡亦,一名热爱分享技术干货的博主. 思考了很久,决定写一篇关于mysql事务(transaction)的博客,一来嘛,因为最近在复习mysql ...
- Oracle 存储过程 中如何使用事务Transaction 自主事务 自治事务
Oracle基础 存储过程和事务 一.事务和存储过程 在存储过程中如何使用事务.当需要在存储过程中同时执行多条添加.修改.删除SQL语句时,为了保证数据完整性,我们需要使用事务.使用方式和在PL-SQ ...
- 数据库事务(Transaction)的ACID特性解释
事务(Transaction)是并发控制的基本单位.所谓事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位.例如,银行转帐工作:从一个帐号扣款并使另一个帐号增款,这两 ...
- oledb 访问接口sqlncli10返回了消息 没有活动事务_这样理解分布式事务你是不是就会懂了?...
分布式事务主要解决分布式一致性的问题.说到底就是数据的分布式操作导致仅依靠本地事务无法保证原的性.与单机版的事务不同的是,单机是把多个命令打包成一个统一处理,分布式事务是将多个机器上执行的命令打包成一 ...
最新文章
- 2021-02-23 如何用简单易懂的例子解释条件随机场(CRF)模型?它和HMM有什么区别?从HMM、MEMM、CRF某牛自己总结的
- Goods:注册页面保存User功能发送邮件以及激活功实现
- Python基础1 历史 变量
- J2EE中一些常用的名词【简】
- JDBC连接不同数据库的连接参数
- 压力与动力是否成正比?
- xdf文件转换成pdf在线_在线PDFtodoc转换器智能转换PDF文件
- windows pip命令不见了_Python中Pygame以及pip的下载与安装
- arm linux 自动挂载,ARM-Linux支持并自动挂载U盘
- [转]阿里云配置mysql远程连接
- BERT+CRF互联网金融新实体发现
- Linux源码安装Python3.7出现的各种坑
- 给老师的作文:育儿经验-父母是孩子最好的老师
- c++实现课程管理系统
- 深圳大学本科毕业论文答辩PPT模板
- 实验5、D/A转换实验
- 北京大学 引进一位人工智能世界级专家!
- UiPath认证流程
- 在Java控制台实现学生成绩管理系统
- springboot 集成kafka 实现多个customer不同group