• sql的几种常用锁简述


    比较全的文章地址保存下:http://www.cnblogs.com/knowledgesea/p/3714417.html

    SELECT * FROM dbo.AA
    SELECT * FROM dbo.B
    --1.排它锁
    --增删改查都不行
    --这个是数据库自己,为了出现数据错乱、脏数据自己加的处理机制
    --连接1
    begin tran

    update dbo.B

    set name='bb'

    where id=1

    waitfor delay '00:00:30' --等待30秒

    commit tran


    --在第二个连接中执行以下语句

    begin tran

    select * from B

    commit tran

    --若同时执行上述两个语句,则select查询必须等待update执行完毕才能执行即要等待30秒

    --2)共享锁
    --holdlock:可以查询但不能增删改
    --在第一个连接中执行以下语句

    begin tran

    select * from dbo.B (HOLDLOCK) --holdlock人为加锁

    waitfor delay '00:00:30' --等待30秒

    commit tran

    --在第二个连接中执行以下语句

    begin tran

    select * from dbo.B

    update dbo.B

    set name='ff'

    where id='1'

    commit tran

    --若同时执行上述两个语句,则第二个连接中的select查询可以执行

    --而update必须等待第一个事务释放共享锁转为排它锁后才能执行 即要等待30秒


    --3)TABLOCKX(独占锁)
    --TABLOCKX:其他事务增删改查都不行
    --在第一个连接中执行以下语句

    begin tran

    select * from dbo.B (TABLOCKX) --holdlock人为加锁

    waitfor delay '00:00:30' --等待30秒

    commit tran

    --在第二个连接中执行以下语句

    begin tran

    select * from dbo.B

    commit tran

    --若同时执行上述两个语句,则第二个连接中的select查询要等待30秒


    --3)死锁


    --在第一个连接中执行以下语句

    begin tran

    update dbo.A

    set name='aa'

    where id='1'

    waitfor delay '00:00:30'

    update dbo.B

    set name='aa'

    where id='1'

    commit tran

    --在第二个连接中执行以下语句

    begin tran

    update dbo.b

    set name='aa'

    where id='1'

    waitfor delay '00:00:10'

    update dbo.A

    set name='aa'

    where id='1'

    commit tran

    --同时执行,系统会检测出死锁,并中止进程


    --查看死锁
    select
    request_session_id spid,
    OBJECT_NAME(resource_associated_entity_id) tableName
    from
    sys.dm_tran_locks
    where
    resource_type='OBJECT'

    --杀死死锁进程
    kill spid

  • 相关阅读:
    【PAT甲级】1054 The Dominant Color
    【PAT甲级】1001 A+B Format
    【算法】二分排序和二分查找
    【PAT甲级】1008 Elevator (20分)
    移动端工作准备
    媒体查询
    多列布局
    怪异盒模型
    弹性盒
    圆角
  • 原文地址:https://www.cnblogs.com/zhuyapeng/p/6731147.html
Copyright © 2020-2023  润新知