SQL Server 中用DBCC Opentran语句查看未关闭的事务(转载)
从SQL Server 2008开始,可以使用DBCC Opentran语句查看数据库中最早一个没有被关闭的事务,下面这篇文章讲述了如何使用DBCC Opentran语句。
An Open transaction may simply be something that has not finished yet, or someone issued a BEGIN TRAN without a corresponding COMMIT or ROLLBACK. Or as we will see at the end, replication is having issues.
You can use this against any database with minimal syntax and get back solid information very quickly.
--connect to sample db
use MyDatabase
go --as generic as this command gets and still runs:
DBCC OPENTRAN
Result if nothing is open:
No active open transactions.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
If I start and execute a DML (insert, update or delete) transaction with BEGIN TRAN and leave out the corresponding COMMIT, I get:
Transaction information for database ‘SmallData_BigLog’.
Oldest active transaction:
SPID (server process ID): 64 <———–
UID (user ID) : -1
Name : user_transaction
LSN : (637:4620:1)
Start time : Apr 1 2017 4:59:02:307PM
SID : 0x0105000000000005150000004893a0845595b6fef515cd5de9030000
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Now, if I open a second transaction (in a new query window) and execute any DML statement without the COMMIT, and then run DBCC OPENTRAN again, I get:
Transaction information for database ‘SmallData_BigLog’.
Oldest active transaction:
SPID (server process ID): 64 <———–
UID (user ID) : -1
Name : user_transaction
LSN : (637:4620:1)
Start time : Apr 1 2017 4:59:02:307PM
SID : 0x0105000000000005150000004893a0845595b6fef515cd5de9030000
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Yes…the same output, as this is just showing the ONE oldest transaction.
I can run a query to show that there are two SPIDs with open transactions:
--
SELECT spid, blocked,[dbid],last_batch,open_tran
FROM master.sys.sysprocesses
WHERE open_tran <> 0
Result:
If I COMMIT spid 64 and re-run DBCC OPENTRAN, the SPID changes to the second transaction I started:
Transaction information for database ‘SmallData_BigLog’.
Oldest active transaction:
SPID (server process ID): 52 <———–
UID (user ID) : -1
Name : user_transaction
LSN : (637:9603:1)
Start time : Apr 1 2017 5:11:20:830PM
SID : 0x0105000000000005150000004893a0845595b6fef515cd5de9030000
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
If I COMMIT spid 52 and re-run DBCC OPENTRAN along with checking sysprocesses for open_tran <> 0 I get:
No active open transactions.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
spid blocked dbid last_batch open_tran
—— ——- —— ———————– ———
(0 row(s) affected)
Now, all of that was just running DBCC OPENTRAN by itself. There are additional options:
--specify dbname, dbid or 0 for the current database
DBCC OPENTRAN (SmallData_BigLog)
You will get results in the same format as the previous examples.
You can suppress all messages, regardless of if a transaction is open or not (but I have no idea why this would help you…)
DBCC OPENTRAN (0) with no_infomsgs
Result:
Command(s) completed successfully.
If you needed to periodically capture the oldest transaction, in order to review later, use WITH TABLERESULTS:
-- TableResults only shows the oldest open tran
-- useful running in a loop to load the oldest
-- tran over time. --create a temp table
CREATE TABLE #OpenTranStatus (
ActiveTransaction varchar(25),
Details sql_variant
); -- Execute the command, putting the results in the table.
INSERT INTO #OpenTranStatus
EXEC ('DBCC OPENTRAN (SmallData_BigLog) with tableresults')
SELECT * FROM #OpenTranStatus
DROP TABLE #OpenTranStatus
In the above, you could create a user table instead of a temp table of course…it depends on your needs.
One more particularly useful item you may see when running DBCC OPENTRAN by itself:
Transaction information for database ‘Music’.
Replicated Transaction Information:
Oldest distributed LSN : (37:143:3)
Oldest non-distributed LSN : (37:144:1)
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
If your database is participating in Replication as a Publisher, this may show up when running OPENTRAN, but it doesn’t necessarily mean that the transaction is actually open. I set this up and stopped the Replication Log Reader and Distribution agent jobs. I then added some data to a published table (article) and ran DBCC OPENTRAN to get the above result. Note that there are two lines with LSN information in them (no SPIDs)
I then ran the Log Reader Agent job and got back:
Transaction information for database ‘Music’.
Replicated Transaction Information:
Oldest distributed LSN : (37:157:3)
Oldest non-distributed LSN : (0:0:0)
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
I have verified that new records I inserted have been read by the log reader, AND distributed to the subscriber(s). This means that while you are seeing
Oldest distributed LSN : (37:157:3)
There is not an error…just info.
If you have non-distributed LSNs, there is something to troubleshoot in the replication process which is way outside the scope of this post. A non-distributed replicated transaction/LSN CAN cause some huge Log file growth, and need to be investigated. If this happens frequently, use the TABLERESULTS option to log to a regular table and alert on it.
Hopefully this gives you some insight into various ways to use DBCC OPENTRAN as well as use cases for the various options. 90% of the time I run this, it is due to application transactions timing out, or log file growth issues.
自我总结:
一般使用DBCC Opentran语句,最常用的场景是数据库对象(例如表)被一个事务长久地锁住了,但是你不知道是哪一个事务锁住了数据库对象,就可以使用DBCC Opentran语句查看当前最早开启事务的SPID,如果确认该事务是因为误操作或者应用程序Bug,所以一直没有被提交或回滚,那么可以使用KILL语句强制结束该事务的SPID,从而人工干预结束事务。
SQL Server 中用DBCC Opentran语句查看未关闭的事务(转载)的更多相关文章
- SQL Server 2000 ——DBCC命令
http://blog.163.com/ruifeng_00/blog/static/6904584200971291923462/ 一.定义 微软当初从SYBASE将DBCC是作为数据库一致性检 ...
- 玩转SQL Server复制回路の变更数据类型、未分区表转为分区表
玩转SQL Server复制回路の变更数据类型.未分区表转为分区表 复制的应用: 初级应用:读写分离.数据库备份 高级应用:搬迁大型数据库(跨机房).变更数据类型.未分区表转为分区表 京东的复制专家 ...
- 在SQL Server中用好模糊查询指令LIKE
简介:like在sql中的使用 在SQL Server中用好模糊查询指令LIKE 查询是SQL Server中重要的功能,而在查询中将Like用上,可以搜索到一些意想不到的结果和效果,like的神奇之 ...
- 在SQL Server中用好模糊查询指令LIKE (转载)
like在sql中的使用:在SQL Server中用好模糊查询指令LIKE:查询是SQL Server中重要的功能,而在查询中将Like用上,可以搜索到一些意想不到的结果和效果,like的神奇 一.一 ...
- SQL SERVER 2005 DBCC IND命令说明
每天笑一笑,烦恼少一倍 轻松一笑!狗狗被调戏:http://947kan.com/video/player-52952-0-0.html ------------------------------- ...
- PowerDesigner反向数据库时遇到[Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句。SQLSTATE = 37错误解决方法
逆向工程中,有时会出现如下错误 ... [Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句 SQLSTATE = 37000 解决方案: 1. ...
- SQL Server Profiler监控执行语句
SQL Server Profiler监控执行语句,这个功能主要用在实时的监控对数据库执行了什么操作,从而及时有效的跟踪系统的运行. 常规配置选项,名称.模板.保存到文件(可以复用). 事件选择,可以 ...
- SQL Server FOR XML PATH 语句的应用---列转行
经常在论坛看到高手使用了 for xml path,由于是搜索一下,记录了详细的使用方法.在SQL Server中利用 FOR XML PATH 语句能够把查询的数据生成XML数据,下面是它的一些应用 ...
- SQL Server中的流控制语句
begin···end 该语句定义sql代码块,通常在if和while语句中使用 declare @num int ; ; begin ; print 'hello word' end if···el ...
随机推荐
- 【代码笔记】Web-JavaScript-JavaScript输出
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- docker-使用Dockerfile制作镜像
最近项目中有使用docker,组内做了关于docker的培训,然后自己跟着研究了一下,大概了解如何使用.我是基于tomcat镜像制作(不需要安装jdk,配置环境变量),基于centos镜像制作需要安装 ...
- NopCommerce 根据手机浏览器和桌面浏览器切换 Theme
自从 NopCommerce 升级到 3.x 以来,默认的 ViewName.Mobile.cshtml 方式就被响应式的默认 Theme 取代了. 但是在今天各种手机专用前端库大行其道的情况下,响应 ...
- python自动化开发-6-面向对象编程
面向对象编程 面向对象的特性 封装:把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏. 继承:面向对象编程 (OOP) 语言的一个主要功能就是“ ...
- python总结(更新中)
1 python函数中的参数传递(注意可变和不可变传递) 可更改(mutable)与不可更改(immutable)对象 在 python 中,strings, tuples, 和 numbers 是不 ...
- 测试思想-集成测试 关于接口测试 Part1
关于接口测试 by:授客 QQ:1033553122 接口定义 接口泛指实体把自己提供给外界的一种抽象化物,用以由内部操作分离出外部沟通方法,使其能被修改内部而不影响外界其他实体与其交互的方式. 举例 ...
- Angular基础(二) 组件的使用
一.简单操作 a) 使用Angular CLI可以快速创建项目框架,先运行 $ npm install –g @angular/cli@1.0.0安装CLI,为CLI的位置设置环境变量,然后就可以 ...
- 常用的Git命令整理
之前一直忙于项目苦于没有时间总结,今天刚好有时间特来总结一下在工作中常用到的代码版本管理器Git.至于为什么要用Git?Git相比SVN有哪些好处?我就不多说了,前人已经总结的很好.今天主要介绍的是常 ...
- C# 异步编程3 TPL Task 异步程序开发
.Net在Framework4.0中增加了任务并行库,对开发人员来说利用多核多线程CPU环境变得更加简单,TPL正符合我们本系列的技术需求.因TPL涉及内容较多,且本系列文章为异步程序开发,所以本文并 ...
- 基于VMware Workstation在Windows Server 2008 R2上搭建SQL Server 2012高可用性组(AlwaysOn Group)测试环境(二)
接上篇: 以SERVER02为例,将服务器加入域,并安装故障转移群集:(SERVER02-SERVER-04操作相同)