“exec sp_reset_connection”是什么意思 在 Sql Server Profiler 中是什么意思?

发布于 2024-07-15 04:15:55 字数 708 浏览 8 评论 0原文

尝试通过发出“sp_reset_connection”来理解 Sql Profiler 的含义。

我有以下内容,“exec sp_reset_connection”行后跟 BatchStarting 和 Completed,

RPC:Completed       exec sp_reset_connection
SQL:BatchStarting   SELECT [c].[TestID] AS [TestID], [c].[Description] AS [Description] FROM [dbo].[Test] AS [c]
SQL:BatchCompleted  SELECT [c].[TestID] AS [TestID], [c].[Description] AS [Description] FROM [dbo].[Test] AS [c]    

基本上第一行“exec sp_reset_connection”意味着整个过程(我的连接已打开,select stmt 运行,然后连接关闭并释放回池)刚刚发生? 或者我的连接仍处于开放阶段。

而且,为什么 sp_reset_connection 在我自己的 select 语句之前执行,难道重置不应该在用户的 sql 之后执行吗?

我想知道是否有办法更详细地了解连接何时打开和关闭?

通过看到“exec sp_reset_connection”,这是否意味着我的连接已关闭?

Trying to understand what Sql Profiler means by emitting "sp_reset_connection".

I have the following, "exec sp_reset_connection" line followed by BatchStarting and Completed,

RPC:Completed       exec sp_reset_connection
SQL:BatchStarting   SELECT [c].[TestID] AS [TestID], [c].[Description] AS [Description] FROM [dbo].[Test] AS [c]
SQL:BatchCompleted  SELECT [c].[TestID] AS [TestID], [c].[Description] AS [Description] FROM [dbo].[Test] AS [c]    

Basically does first line "exec sp_reset_connection" mean the whole process (my connection was opened, the select stmt is run, then the connection is closed and released back to pool) just take place? Or my connection is still in open stage.

And, why does the sp_reset_connection executed before my own select statement, shouldn't it the reset come after user's sql?

I'm trying to know is there a way to know in more detail when a connection is opened and closed?

By seeing "exec sp_reset_connection", does that mean my connection is closed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

违心° 2024-07-22 04:15:55

就像其他答案所说,sp_reset_connection表示连接池正在被重用。 请注意一个特定的后果!

Jimmy Mays 的 MSDN 博客 说:

sp_reset_connection 不会重置
事务隔离级别为
之前的服务器默认值
连接的设置。

更新:从 SQL 2014 开始,对于 TDS 版本 7.3 或更高版本的客户端驱动程序,事务隔离级别将重置回默认值。

参考:SQL Server:跨池连接的隔离级别泄漏

此处是一些附加信息:

sp_reset_connection 的作用是什么做什么?

数据访问 API 的层,如 ODBC、
OLE-DB 和 System.Data.SqlClient 全部
调用(内部)存储过程
重新使用时 sp_reset_connection
来自连接池的连接。 它
这样做是为了重置的状态
在重新使用连接之前,
然而没有任何记录
事情被重置。 本文尝试
记录各部分
连接被重置。

sp_reset_connection 重置
连接的以下方面:

  • 所有错误状态和编号
    (如@@错误)

  • 停止所有 EC(执行上下文)
    它们是父 EC 的子线程
    执行并行查询

  • 等待任何未完成的 I/O
    出色的运营

  • 释放任何保留的缓冲区
    服务器通过连接

  • 解锁所有缓冲区资源
    连接使用的内容

  • 释放所有分配的内存
    由连接拥有

  • 清除所有工作或临时内容
    由创建的表
    连接

  • 杀死拥有的所有全局游标
    连接

  • 关闭所有打开的 SQL-XML 句柄

  • 删除任何打开的 SQL-XML 相关工作表

  • 关闭所有系统表

  • 关闭所有用户表

  • 删除所有临时对象

  • 中止未完成的事务

  • 加入时分布式事务的缺陷

  • 减少引用计数
    对于当前数据库中的用户
    释放共享数据库锁

  • 释放获取的锁

  • 释放任何获取的句柄

  • 将所有 SET 选项重置为默认值

  • 重置@@rowcount值

  • 重置@@identity值

  • 重置任何会话级别跟踪
    使用 dbcc traceon() 的选项

  • 在 SQL Server 2005 及更高版本中将 CONTEXT_INFO 重置为 NULL [不是原始文章的一部分]

sp_reset_connection 不会重置:

  • 安全上下文,这就是原因
    连接池匹配连接
    基于确切的连接字符串

  • 使用 sp_setapprole,因为在 SQL Server 2005 之前根本无法还原应用程序角色。从 SQL Server 2005 开始,可以还原应用程序角色,但只能通过附加不属于会话一部分的信息。 在关闭连接之前,需要通过 sp_unsetapprole 使用执行 sp_setapprole 时捕获的“cookie”值。

注意:我将列表包含在此处,因为我不希望它在瞬息万变的网络中丢失。

Like the other answers said, sp_reset_connection indicates that connection pool is being reused. Be aware of one particular consequence!

Jimmy Mays' MSDN Blog said:

sp_reset_connection does NOT reset the
transaction isolation level to the
server default from the previous
connection's setting.

UPDATE: Starting with SQL 2014, for client drivers with TDS version 7.3 or higher, the transaction isolation levels will be reset back to the default.

ref: SQL Server: Isolation level leaks across pooled connections

Here is some additional information:

What does sp_reset_connection do?

Data access API's layers like ODBC,
OLE-DB and System.Data.SqlClient all
call the (internal) stored procedure
sp_reset_connection when re-using a
connection from a connection pool. It
does this to reset the state of the
connection before it gets re-used,
however nowhere is documented what
things get reset. This article tries
to document the parts of the
connection that get reset.

sp_reset_connection resets the
following aspects of a connection:

  • All error states and numbers
    (like @@error)

  • Stops all EC's (execution contexts)
    that are child threads of a parent EC
    executing a parallel query

  • Waits for any outstanding I/O
    operations that is outstanding

  • Frees any held buffers on the
    server by the connection

  • Unlocks any buffer resources
    that are used by the connection

  • Releases all allocated memory
    owned by the connection

  • Clears any work or temporary
    tables that are created by the
    connection

  • Kills all global cursors owned by the
    connection

  • Closes any open SQL-XML handles that are open

  • Deletes any open SQL-XML related work tables

  • Closes all system tables

  • Closes all user tables

  • Drops all temporary objects

  • Aborts open transactions

  • Defects from a distributed transaction when enlisted

  • Decrements the reference count
    for users in current database which
    releases shared database locks

  • Frees acquired locks

  • Releases any acquired handles

  • Resets all SET options to the default values

  • Resets the @@rowcount value

  • Resets the @@identity value

  • Resets any session level trace
    options using dbcc traceon()

  • Resets CONTEXT_INFO to NULL in SQL Server 2005 and newer [ not part of the original article ]

sp_reset_connection will NOT reset:

  • Security context, which is why
    connection pooling matches connections
    based on the exact connection string

  • Application roles entered using sp_setapprole, since application roles could not be reverted at all prior to SQL Server 2005. Starting in SQL Server 2005, app roles can be reverted, but only with additional information that is not part of the session. Before closing the connection, application roles need to be manually reverted via sp_unsetapprole using a "cookie" value that is captured when sp_setapprole is executed.

Note: I am including the list here as I do not want it to be lost in the ever transient web.

老街孤人 2024-07-22 04:15:55

这表明正在使用连接池(这是一件好事)。

It's an indication that connection pooling is being used (which is a good thing).

百思不得你姐 2024-07-22 04:15:55

但请注意:

如果在存储过程或触发器中发出 SET TRANSACTION ISOLATION LEVEL,则当对象返回控制权时,隔离级别将重置为调用对象时有效的级别。 例如,如果在批处理中设置 REPEATABLE READ,然后该批处理调用将隔离级别设置为 SERIALIZABLE 的存储过程,则当存储过程将控制返回到批处理时,隔离级别设置将恢复为 REPEATABLE READ。

http://msdn.microsoft.com/en-us/library/ms173763.aspx

Note however:

If you issue SET TRANSACTION ISOLATION LEVEL in a stored procedure or trigger, when the object returns control the isolation level is reset to the level in effect when the object was invoked. For example, if you set REPEATABLE READ in a batch, and the batch then calls a stored procedure that sets the isolation level to SERIALIZABLE, the isolation level setting reverts to REPEATABLE READ when the stored procedure returns control to the batch.

http://msdn.microsoft.com/en-us/library/ms173763.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文