sqlserver-----transactions

2019-08-20  本文已影响0人  鲸鱼酱375

1. TCL

1.1 What are Transactions?

It is a set of SQL commands which work as a single unit. i.e if they are executed, either they have to completely successful or rollback (revert) to original state of DB. No partial commits. Every transaction has to follow ACID properties.

所谓事务存储点就是在事务过程当中插入若干个标记,当事务执行中出现错误时,可
以不撤销整个事务,只是撤销部分事务,将事务退回到某个事物存储点。一旦事务提
交或回滚,则事务结束。

1.2 Types of Transactions

1.2.1 Implicit

User don't begin a transaction but has to commit or rollback. User has to make sure that SET IMPLICIT_TRANSACTIONS ON executed to start implicit transactions. If explicit ROLLBACK or COMMIT statement is not given by user, all the open transactions for the connection will be lost (rolled back) once the connection is disconnected.

1.2.2 Explicit (most used) 多数用这个 most transactions

1.2.3.1 implicit & complicit

implicit explicit
1.developer need not to start a transaction developer has to start a trans using begin tran
nesting is not possible with just implicit trans nesting is possible
need to specify SET IMPLICIT_TRANSACTIONS ON no need to alter any session settings
performs slow compared to explicit performance is better

1.2.3 auto commited

1.3 ACID Properties

1.3.1 Automaticity

The statements/statement (transaction) should be completed successfully or rollback to original state. Using BEGIN TRAN and COMMIT/ROLLBACK.

1.3.2 Consistency

DB has to be in consistent state before and after transaction. Making sure that all rules on the DB are validated for all transactions and should follow business rules. Using CHECK CONSTRAINTS, RULES, TRIGGERS, PROCEDURES, FUNCTIONS.

1.3.3 Isolation

Every transaction should be independent of each other or a transaction should not interfere with other transaction. Using ISOLATION Levels

eg: t1 should not touch t2

1.3.4 Durability

Once the transaction (data)is committed it should stay in that state until it is modified by another transaction. Data should not be lost. Using COMMIT command and proper Backups (mainly log back ups), High Availability (Mirroring, Replication, Log Shipping, Clustering).

Transactions will remain committed and final

1.4 视频代码


declare @int int

set @int =6

print @int

---comit transaction
go
declare @int int
begin tran
set @int =6
commit tran
print @int

----------------roll back & commit
go
declare @int int
begin tran
set @int =6
rollback tran

begin tran
set @int=2
commit tran
print @int


---------undo transaction
go
select * from client

select * from rank_test_table

begin tran
truncate table rank_test_table
rollback     transaction

select * from rank_test_table

-------------------------save point
go
begin tran t1
save tran s1
    update rank_test_table
    set name ='tim'
    where id = 4
rollback tran s1
    update rank_test_table
    set name ='superman'
    where id = 5
commit tran t1

select * from rank_test_table


-------------------------with mark
go
begin tran t1 with mark  'updating table'
save tran s1
    update rank_test_table
    set name ='tim'
    where id = 4
rollback tran s1
    update rank_test_table
    set name ='superman'
    where id = 5
commit tran t1

select * from rank_test_table


------implicit
go
Set Implicit_Transactions On

update rank_test_table
set name = 'tim'
where id =4

rollback transaction

update rank_test_table
set name ='superman'
where id =5
commit transaction

set Implicit_Transactions Off

select * from rank_test_table

1.5 error hadling in transaction



https://www.cnblogs.com/knowledgesea/p/3714417.html
链接有其他笔记

1.6 with mark


2. Isolation Levels & Locks

2.1 Locks in SQL

21.1 Shared Locks

2.1.2 Exclusive Locks

2.1.3 Update Locks

dead lock: both dml operation,nither transaction can be done,


https://www.jianshu.com/p/8b9065bb6a30

更新锁——Update lock
更新锁(U锁)。当T1给资源A加上更新锁后,代表该资源将在稍后更新,更新锁与共享锁兼容,更新锁可以防止例2里那种一般情况的死锁发生,更新锁会阻塞其他的更新锁和排他锁。因此相同资源上不能存在多个更新锁。

更新锁允许其他事务在更新之前读取资源。但不可以修改。因为其他事务想获取资源的排他锁时,发现该资源已存在U锁,则等待U锁释放。

在T1找到需要更新的数据时,更新锁直接转为排他锁,开始更新数据,不需要等待其他事务释放共享锁啥的。

那么就问了,共享锁为什么不可以直接升级为排他锁,而必须等待其他共享锁都释放掉才可以转为排他锁呢?

这就是共享锁和更新锁的一个区别了,共享锁之间是兼容的,但是更新锁之间互不兼容,因此仅有一个更新锁直接转为排他锁是安全的,而多个共享锁问也不问直接转为排他锁,那怎么行呢,排他锁只能有一个的,这就是为什么共享锁需要等待其他共享锁释放才可以升级为排他锁的原因了。

例4:
T1:

begin
select * from table with (updlock) (加更新锁)
update table set column1='hello' (重点:这里T1做update时,不需要等T2释放什么,而是直接把更新锁升级为排他锁,然后执行update)

T2:

begin
select * from table (T1的更新锁不影响T2的select)
update table set column1='world' (T2的update需要等待T1的update执行完)

分析:(1)T1先到达,T1的select句对table加更新锁,此时T2紧接着到达,T2的select句对table加共享锁,假设T2的select先执行完,要开始T2的update,发现table已有更新锁,则T2等,T1此时执行完select,然后将更新锁升级为排他锁,开始更新数据,执行完成,事务结束,释放排他锁,此时T2才开始对table加排他锁并更新。

(2)T2先到,T1紧接着,T2加共享锁 => T1加更新锁 => 假设T2先结束select => 试图将共享锁升级为排他锁 => 发现已有更新锁 => 之后的情况同(1)


2.1.4 Intent Locks

2.1.5 Schema Locks

2.1.6 Bulk Update Locks

2.2 Isolation Levels in SQL

It is a mechanism/process/property used by SQL Server to control the concurrency of transactions.
It uses locks to control the concurrency.

2.2.1 Read Uncommitted

2.2.2 Read Committed (default)

2.2.3 Repeatable Read

2.2.4 Snapshot

What are the 2 issues with Snapshot Isolation Level?
1. Snapshot maintains a ROWVERSIONING of transactions in TempDB, this is a overhead that SQL Server has to deal with. Along with copies of SNAPSHOTs stored in TempDB.
2. When a transaction started with Snapshot ISOLATION level and if it modifies some data within the snapshot and before this is committed if there is another transaction modifies the same data (and this trans started after the snapshot tran), data modifications by snapshot is not allowed.

2.2.5 Serializable :phantom

2.3 视频代码

set transaction isolation level read committed

begin tran
    select*
    from AdventureWorks2017.HumanResources.Employee
commit tran

2.4 data wrong problem

更新丢失:多个用户同时对一个数据资源进行更新,必定会产生被覆盖的数据,造成数据读写异常。
不可重复读:如果一个用户在一个事务中多次读取一条数据,而另外一个用户则同时更新啦这条数据,造成第一个用户多次读取数据不一致。
脏读:第一个事务读取第二个事务正在更新的数据表,如果第二个事务还没有更新完成,那么第一个事务读取的数据将是一半为更新过的,一半还没更新过的数据,这样的数据毫无意义。
幻读:第一个事务读取一个结果集后,第二个事务,对这个结果集经行增删操作,然而第一个事务中再次对这个结果集进行查询时,数据发现丢失或新增。

2.5 dead lock

2.5.1 how to identify the dead lock?

  1. Use SQL Profiler to identify what are 2 transactions and who is victim.
  2. PerfMon can identify how many deadlocks are happening.
  3. Use Extended Events
  4. Trace Flags: DBCC TRACEON (1204)
    https://www.mssqltips.com/sqlservertip/1036/finding-and-troubleshooting-sql-server-deadlocks/
  5. Let users/DBA tell you.

2.5.2 how to solve dead locks?

  1. Usually SQL server identifies and kills a process which is cheaper to redo based on resources the transaction takes.
  2. Use more optimistic isolation levels which would minimize the locking on resources leading to less conflicts on resource locking there by less dead locks.
  3. Use dead lock priority for queries which you don't want to be killed. This is not a solution, it a way of not being a victim in dead lock scenario.
  4. See if you can change timings of the queries or procs which conflict in Dead Lock scenarios.
上一篇 下一篇

猜你喜欢

热点阅读