当前位置:网站首页>PG基础篇--逻辑结构管理(事务)
PG基础篇--逻辑结构管理(事务)
2022-07-06 12:42:00 【51CTO】
事务提交
pg默认是自动提交事务,可将自动提交功能关闭
关闭自动提交
savepoint
pg支持保存点功能,在一个大事务中,可以把操作过程分成几个部分,第一个部分执行成功后可以建一个保存点,若后面的部分执行失败,则回滚到此保存点,而不必回滚整个事务。
插入的第二条数据,回滚到第一个保存点。
事务隔离级别
和mysql一样,pg的事务隔离级别也分为4种,
read uncommited 读未提交
read commit读已提交
repeatable read 可重复读
serializable 串行化
脏读:一个事务读取了另一个事务未提交数据。
不可重复读:一个事务重新读取前面读取过的数据时,发现该数据已经被另一个事务修改过(update ,delete)。
幻读:一个事务开始后,查询符合条件的数据后,再次查询,发现多了数据(insert)。
pg可重复读可以避免不可重复读,但是避免不了幻读,这一点和mysql一样。不同点是pg默认的隔离级别是读已提交,而mysql默认是可重复读。
两阶段提交
在分布式系统中,事务中往往包含了多台数据库的操作,虽然单台数据库的操作能够保证原子性,但多台数据库之间的原子性就需要两阶段提交来实现了。
步骤
1)应用程序先调用各台数据库做一些操作,但不提交事务,然后应用程序调用事务协调器中的提交方法
2)事务协调器将联络事务中涉及的每台数据库,并通知它们准备提交事务,这是第一阶段的开始,就是调用PREPARE TRANSACTION。
3)各台数据库接收到PREPARE TRANSACTION后,如果返回成功,则数据库必须将自己置于如下状态:确保后续能在被提交事务的时候提价事务,或后续能在被要求回滚事务的时候回滚事务。所以PG会将已准备好提交的信息写入持久存储区中,如果数据库无法完成此事务,它会直接返回失败给事务协调器。
4)事务协调器接收所有数据库的响应。
5)第二阶段,如果任何一个数据库在第一阶段返回失败,则事务协调器将会发一个回滚命令给各台数据库,如果所有数据库的响应都是成功的,则向各台数据库发送commit prepared命令。通知各台数据库事务提交成功。
边栏推荐
- 【每周一坑】信息加密 +【解答】正整数分解质因数
- Tencent T4 architect, Android interview Foundation
- In unity space, an object moves around a fixed point on the sphere at a fixed speed
- [weekly pit] information encryption + [answer] positive integer factorization prime factor
- Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)
- 硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
- Detailed explanation of knowledge map construction process steps
- Core principles of video games
- Spiral square PTA
- 小孩子學什麼編程?
猜你喜欢

B-jiege's tree (pressed tree DP)

Pycharm remote execution

APS taps home appliance industry into new growth points

Application layer of tcp/ip protocol cluster

Jupyter launch didn't respond after Anaconda was installed & the web page was opened and ran without execution

B-杰哥的树(状压树形dp)

Distributed ID

HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother

Detailed introduction of distributed pressure measurement system VIII: basic introduction of akka actor model

Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
随机推荐
为什么新手在编程社区提问经常得不到回答,甚至还会被嘲讽?
How to turn a multi digit number into a digital list
Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)
Boder radius has four values, and boder radius exceeds four values
Unity making plug-ins
2022 construction electrician (special type of construction work) free test questions and construction electrician (special type of construction work) certificate examination
Pycharm remote execution
“罚点球”小游戏
01 basic introduction - concept nouns
Introduction of Xia Zhigang
[DSP] [Part 2] understand c6678 and create project
Variable star --- article module (1)
Is it safe to open an account in flush? Which securities company is good at opening an account? Low handling charges
性能测试过程和计划
知识图谱之实体对齐二
In line elements are transformed into block level elements, and display transformation and implicit transformation
Special topic of rotor position estimation of permanent magnet synchronous motor -- fundamental wave model and rotor position angle
B-杰哥的树(状压树形dp)
棋盘左上角到右下角方案数(2)
2022 portal crane driver registration examination and portal crane driver examination materials