PostgreSQL设置事务隔离级别实验
2024-08-20 14:12:43
apple=# begin;
BEGIN
apple=# set transaction ISOLATION LEVEL read committed ;
SET
apple=# select * from test;
id | info
----+-------
2 | test
3 | test1
4 | test1
5 | test
6 | test
7 | test
8 | test1
9 | test1
10 | test
11 | test
1 | test
1 | test
1 | test
1 | test
1 | test
(15 rows)
中间别的连接插入一条数据,并提交,在本事务内查询,多了一条。
apple=# select * from test;
id | info
----+-------
2 | test
3 | test1
4 | test1
5 | test
6 | test
7 | test
8 | test1
9 | test1
10 | test
11 | test
1 | test
1 | test
1 | test
1 | test
1 | test
1 | test
(16 rows)
apple=# commit;
COMMIT 设置隔离级别为可重复读:
apple=# begin;
BEGIN
apple=# set transaction ISOLATION LEVEL repeatable READ;
SET
apple=# select * from test;
id | info
----+-------
2 | test
3 | test1
4 | test1
5 | test
6 | test
7 | test
8 | test1
9 | test1
10 | test
11 | test
1 | test
1 | test
1 | test
1 | test
1 | test
1 | test
(16 rows)
中间别的连接插入一条数据,并查询:
apple=# select * from test;
id | info
----+-------
2 | test
3 | test1
4 | test1
5 | test
6 | test
7 | test
8 | test1
9 | test1
10 | test
11 | test
1 | test
1 | test
1 | test
1 | test
1 | test
1 | test
(16 rows) apple=#
设置隔离级别只能在事物段中执行:
apple=# set transaction ISOLATION LEVEL repeatable READ;
WARNING: SET TRANSACTION can only be used in transaction blocks
SET
在PG配置文件有一项配置提交级别:
#default_transaction_isolation = 'read committed'
Read committed(读已提交)
读已提交是PostgreSQL中的默认隔离级别。 当一个事务运行使用这个隔离级别时, 一个查询(没有FOR UPDATE/SHARE子句)只能看到查询开始之前已经被提交的数据, 而无法看到未提交的数据或在查询执行期间其它事务提交的数据。实际上,SELECT查询看到的是一个在查询开始运行的瞬间该数据库的一个快照。不过SELECT可以看见在它自身事务中之前执行的更新的效果,即使它们还没有被提交。
所以脏读现象将不会再发生。
还要注意的是,即使在同一个事务里两个相邻的SELECT命令可能看到不同的数据,因为其它事务可能会在第一个SELECT开始和第二个SELECT开始之间提交。即会造成不可重复读。
Repeatable read(可重复读)
可重复读隔离级别只看到在事务开始之前被提交的数据;它从来看不到未提交的数据或者并行事务在本事务执行期间提交的修改(不过,查询能够看见在它的事务中之前执行的更新,即使它们还没有被提交)。
这个级别与读已提交不同之处在于,一个可重复读事务中的查询可以看见在事务中第一个非事务控制语句开始时的一个快照,而不是事务中当前语句开始时的快照。因此,在一个单一事务中的后续SELECT命令看到的是相同的数据,即它们看不到其他事务在本事务启动后提交的修改。
最新文章
- 云计算之路-阿里云上:从ASP.NET线程角度对“黑色30秒”问题的全新分析
- GitHub新手快速入门日常操作流程
- C#窗体自定义控件
- Win7重装后,如何删除cygwin目录?
- SpringBoot相关
- 1>; Strut2 Mapping to MVC
- SQLite语句
- XStream的使用方法、简单使用方法、xml的解析方法
- nopcommerce数据库字典
- Mui Webview下来刷新上拉加载实现
- vueX、vue中transition的使用、axios
- line-height各类属性值
- git 入门教程之 git 私服搭建教程
- docker 搭建简易仓库registry
- JavaScript 从定义到执行,你应该知道的那些事
- curl_init 接口
- Spring加载静态资源的方式
- 【HLSDK系列】groupinfo的基本用法
- 20155334 实验二 Java面向对象程序设计
- HDU 2114 Calculate S(n)
热门文章
- Nginx 403 forbidden原因及故障模拟重现(转载)
- PAT 1114 Family Property[并查集][难]
- 5.5 Components -- Customizing A Compnent's Element
- c#通过webrequest请求远程http服务时出现的问题
- firefox_flash_install_on_kali
- 小奇的糖果(candy)
- linux下如何退出tmux和重新进入tmux
- SQL 中 not in 查询不到数据问题
- 【图片下载-代码】java下载网络图片资源例子
- JDBC中的事物处理