事务错误 和进程阻塞

--阻塞和错误 
--
version 2005
declare @tcount int,@rcount int
select @tcount=@@trancount
if @tcount=0 ---外部没有事务打开
begin tran
else
save tran s1 --外部事务已经打开
begin try
set lock_timeout 5000 --5秒超时
select b.text,c.*,d.* from sys.dm_exec_connections as a
cross apply sys.dm_exec_sql_text(most_recent_sql_handle) as b
inner join sys.dm_tran_locks as c on a.session_id=c.request_session_id and request_status='WAIT'
inner join sys.dm_tran_locks as d on c.resource_database_id=d.resource_database_id
and c.resource_associated_entity_id=d.resource_associated_entity_id and d.request_status='GRANT'
and c.resource_description=d.resource_description --查看阻塞
select @count= @@rowcount
insert into ..... --语句
if @tcount=0 and @count=0
begin
commit tran
end
end try
begin catch
if @tcount=0 and @count>0 --外部事物没打开并且阻塞
begin
select b.text,c.*,d.* from sys.dm_exec_connections as a
cross apply sys.dm_exec_sql_text(most_recent_sql_handle) as b
inner join sys.dm_tran_locks as c on a.session_id=c.request_session_id and request_status='WAIT'
inner join sys.dm_tran_locks as d on c.resource_database_id=d.resource_database_id
and c.resource_associated_entity_id=d.resource_associated_entity_id and d.request_status='GRANT'
and c.resource_description=d.resource_description --查看阻塞
select error_message(),error_severity(),error_line()
rollback tran
end
if @tcount>0 and @count=0 --外部事物打开并且没阻塞
begin
if xact_state()=1 --事务错误
begin
select error_message(),error_severity(),error_line()
rollback tran s1
end
if xact_state()=-1 --外部事务错误
begin
print '外部事务错误 自己判断 committed 或 rollback '
end
end
if @tcount>0 and @count>0
begin
select b.text,c.*,d.* from sys.dm_exec_connections as a
cross apply sys.dm_exec_sql_text(most_recent_sql_handle) as b
inner join sys.dm_tran_locks as c on a.session_id=c.request_session_id and request_status='WAIT'
inner join sys.dm_tran_locks as d on c.resource_database_id=d.resource_database_id
and c.resource_associated_entity_id=d.resource_associated_entity_id and d.request_status='GRANT'
and c.resource_description=d.resource_description --查看阻塞
if xact_state()=1
begin
select error_message(),error_severity(),error_line()
rollback tran s1
end
if xact_state()=-1 --外部事务错误
begin
print '外部事务错误 自己判断 committed 或 rollback '
end
end
end catch
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值