【数据库基础】事务控制(Transaction)Part 1

事务控制Part 2:级联回滚,死锁,时间戳,日志文件,检查点等https://blog.csdn.net/YFY20020109/article/details/125133798?spm=1001.2014.3001.5502


目录

一、事务的定义、性质

what is Transaction:

4 Properties:

二、并发问题(Concurrency Problem)

1.丢失更新问题(Lost updates problem)

 2.未提交依赖问题 / 脏读问题(Uncommitted dependency / Dirty read problem)

3.异常分析问题(Incorrect analysis problem)

三、并发控制、调度(Concurrency Control & Schedule)

四、上锁(Locking)

两相锁(Two-Phases Locking,2PL)

五、可串行性(Serialisability)


一、事务的定义、性质

what is Transaction:

      Action, or series of actions, carried out by user or application, which reads or updates(write) contents of database.
      Two outcomes: 
        • Success (commit)
        • Failure (rollback)
      
      An application program can be thought as a series of transactions.
      Transforms database from one consistent state to another, although consistency may be violated during transaction.
Eg.

4 Properties:

  • Atomicity:‘All or nothing’ property,a transaction is performed in its entirety or is not performed at all;The logic unit of work
       原子性:事务像原子一样不可再分
  • Consistency:Must transform database from one valid state to another.

       

  • Isolation:Partial effects of incomplete transactions should not be visible to other transactions.
       transactions execute independently of one another
        After a failure, an uncommitted transaction is rolled back and all of its effects are erased
       A transaction updates a tuple, but has not committed the changes,another transaction  cannot read the uncommitted data
 
        如果某一事务重写了X后发生撤回(rollback),以后其他事务读取的X应仍是重写前的X
     
  • Durability:Effects of a committed transaction are permanent and must not be lost
       because of later failure;保存了的事儿,那就是永久保存了

二、并发问题(Concurrency Problem)

In order to run transactions concurrently we interleave their operations,
Each transaction gets a share of the computing time,
This leads to 3 main problems:
Lost updates problem
Uncommitted dependency problem
Incorrect analysis problem
All arise because isolation is broken.
三种问题本质上相同,根源都在事务之间互相造成不正当的干涉,只不过表现的结果不一样:

1.丢失更新问题(Lost updates problem

一个事务改写了另一个事务已经上传的结果


 2.未提交依赖问题 / 脏读问题(Uncommitted dependency / Dirty read problem

一个事务读入别的事务正在加工的数据项;

“Rollback Problem”:它造成的结果多半是T1已经rollback,可T2还在用已经废止的数据值处理问题,最终结果也不正确


3.异常分析问题(Incorrect analysis problem

不像前两种problem,问题大概率出在被读取再改写的值X、Y本身;而是出在由他们推导产生的新数据出错(如加和sum等),原因是X、Y等值的改写与得出新数据的值两种操作顺序出现了错位(如用原来的X算出了sum后,才把新X成功上传)。

Eg.


   

三、并发控制、调度(Concurrency Control & Schedule)

  • Concurrency control : process of managing simultaneous(同时的) operations on the database without having them interfere(干扰) with one another
        让不同事务各自独立
  • schedule: a sequence of the operations by a set of concurrent transactions that preserves the order of the operations in each of the individual transactions
  • serial schedule(串行调度): a schedule where operations of each transaction are executed consecutively without any interleaved operations from other transactions
        各个事务串起来,一个接一个,干完这一件才会干下一件。不会在某时停下正在做的事务去做另一件事,各个事务不会穿插进行

四、上锁(Locking)

  • Locking is a conservative approaches for concurrency control - delay transactions in case they conflict with other transactions
  • Transaction uses locks to deny access to other transactions and so prevent incorrect updates
  • 分为两种:

       共享锁(又叫读锁) Shared lock(read lock):和其他事务共同使用数据

       互斥锁(写锁) Exclusive lock(write lock):阻拦其他事务使用该数据

  • Before reading from a resource a transaction must acquire a read-lock
       Before writing to a resource a transaction must acquire a write-lock
       
  • A transaction may not acquire a lock on any resource that is write-locked by another transaction

       数据源只能同时上1个互斥锁,一个事务上了,其他的就暂时上不了

  • A transaction may not acquire a write-lock on a resource that is locked by another transaction

       不管那个数据源现在身上有什么锁,只要有,就不能再上互斥锁

上了read-lock,只能对它再上read-lock而不是write-lock;而上了write-lock,就什么也不能再上了

  • If the requested lock is not available, transaction waits
可是, 加锁仍可能发生并发问题。还需要正确的加法:

两相锁(Two-Phases Locking,2PL)

  • All locking operations precede unlock operation in the transaction

       每个要锁的数据开头先全锁上,最后再全解开,所有上锁的操作必在所有解锁操作之前即可。

  • Every transaction must lock an item (read or write) before accessing it    先锁再读

       Once a lock has been released, no new items can be locked

  • what is Two phases?
        1. Growing phase - acquires all locks but cannot release any locks.
        2. Shrinking(收缩) phase - releases locks but cannot acquire any new locks.

   

2PL解决三大并发问题:

1.Lost Update Problem

 

Preventing the Lost Update Problem using 2PL

 

注意:虽然最终T1还是改变了T2改写过的X,但它和 lost update不一样。这次T1不仅上传,而且读入的都是T2处理完的X,这说明T1本就需要T2先处理后的X——T1依赖于T2!

2.Uncommitted Dependency Problem

 

 

 3.Inconsistent Analysis Problem

 

90+50+35 = 185 ?!

 


五、可串行性(Serialisability)

  • Nonserial Schedule: Schedule where operations from set of concurrent transactions are interleaved.    可参考serial schedule,它的对立面;各个事务你中有我我中有你
  • Objective of serialisability is to find nonserial schedules that are equivalent to some serial schedule. Such a schedule is called serialisable.

如:某调度A包含事务T1、T2,T2全过程夹在T1前半段和后半段之间,之类的,如果调度A实际效果=调度B(先T1   ||||   后T2),或者调度A=调度C(先T2   ||||   后T1),那么A就可以被串行化

  • A schedule is conflict serialisable(冲突可串行) if transactions in the schedule have a conflict but the schedule is still serialisable.

 

  • Two transactions have a conflict If at least one is a write and they use the same resource.


Details:

  • Transaction T1 reads data items A, B and C, a concurrent transaction T2 reads data items B, C, and D, and write data item D. Then T1 and T2 have NO conflict!

尽管都作用于B、C,但只有D被写了

  • The timing locks released is important for serialisable schedules
  • Two-phase locking (2PL) can guarantee serialisability

    注:部分内容参考自图书《数据库系统-设计、实现与管理(基础篇)》

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PostgreSQL 是一个功能强大的关系型数据库管理系统,在处理数据库事务和并发控制方面有着很多特性和机制。下面是关于 PostgreSQL 数据库事务和并发控制的一些重要信息: 1. 数据库事务事务是一组操作的逻辑单元,要么全部执行成功,要么全部回滚。在 PostgreSQL 中,事务的开始和结束通过 BEGIN 和 COMMIT 或 ROLLBACK 语句来定义。默认情况下,每个 SQL 语句都在单独的事务中执行,但你可以使用显式的 BEGIN 和 COMMIT 指令来控制事务的边界。 2. 并发控制:并发控制是指在多个用户同时访问数据库时,保证数据的一致性和正确性。PostgreSQL 使用多版本并发控制(MVCC)机制来实现并发控制。MVCC 使用了版本号(或时间戳)来跟踪事务的可见性和一致性。 3. 锁机制:PostgreSQL 使用锁来控制并发访问。锁可以对表、行或其他数据库对象进行加锁,以防止其他事务对其进行修改或访问。锁分为共享锁和排它锁,用于控制读取和写入操作之间的冲突。 4. 事务隔离级别:PostgreSQL 支持四种事务隔离级别,分别是读未提交(Read Uncommitted)、读已提交(Read Committed)、可重复读(Repeatable Read)和串行化(Serializable)。这些隔离级别提供了不同的并发控制策略,可以根据应用程序的需求进行配置。 5. 并发控制配置:PostgreSQL 提供了多种配置选项来调整并发控制的性能和行为。你可以通过修改配置文件或使用 ALTER SYSTEM 命令来更改这些选项。一些常见的配置选项包括 max_connections(最大连接数)、max_locks_per_transaction(每个事务最大锁数)和deadlock_timeout(死锁超时时间)等。 总而言之,PostgreSQL 提供了强大的数据库事务和并发控制机制,通过锁机制、MVCC 以及事务隔离级别来处理并发操作和保证数据的一致性。这些特性使得 PostgreSQL 成为处理高并发场景下数据操作的理想选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值