何时调用 DbConnection.StateChange? [英] When is DbConnection.StateChange called?

查看:25
本文介绍了何时调用 DbConnection.StateChange?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

   class Program
{
    static void Main()
    {
        var connection = new SqlConnection("myConnectionString");
        connection.Open();
        connection.StateChange += HandleSqlConnectionDrop;
        Console.WriteLine("Hi");
        Console.ReadLine();
    }

    private static void HandleSqlConnectionDrop(object connection, StateChangeEventArgs args)
    {
        Console.WriteLine("DB change detected");
    }
}

我在 SQL 服务器实例运行时启动上述代码.然后我继续执行

I start the above code while the SQL server instance is running. I then proceed to execute

SHUTDOWN WITH NOWAIT;

在程序连接到的 sql server 实例上.然后我观察 SQL 服务器服务停止.但是,我从未在输出中看到检测到数据库更改"消息.这是为什么?

on the sql server instance that the program is connected to. I then observer the SQL server service stopping. However, I never see the "DB change detected" message in the output. Why is this?

旁白: 如果我随后尝试对 SQL 连接执行操作,我将看到 StateChange 处理程序被调用,但之前不会.有没有办法改变这种行为?

Aside: I will see the StateChange handler get called if I then attempt to perform an operation on the SQL connection, but never before hand. Is there a way this behavior can be changed?

推荐答案

DbConnection.StateChange 何时调用?

When is DbConnection.StateChange called?

您可以通过查看 Microsoft 参考源代码来了解.

You can find out by looking at the Microsoft reference source code.

StateChange 事件由 DbConnection.OnStateChange<引发/a> 函数.查找对这个函数的引用只会产生几个实例:

The StateChange event is raised by the DbConnection.OnStateChange function. Looking for references to this function yields only a few instances:

首先,在SqlConnection 类中,OnStateChange 只在Close 方法.

Firstly, in the SqlConnection class, OnStateChange is called only in the Close method.

然后在 DbConnectionHelper.cs 文件,有一个名为 DBCONNECTIONOBJECT 的部分类.看起来它使用一些构建时恶作剧用于所有 DbConnection 派生类.因此,您可以将其视为 SqlConnection 的一部分.在任何情况下,它只从 OnStateChange>SetInnerConnectionEvent 函数.

Then in the DbConnectionHelper.cs file, there's a partial class called DBCONNECTIONOBJECT. It looks like it's used for all DbConnection-derived classes using some build-time shenanigans. So you can consider it to be part of SqlConnection. In any case, it calls OnStateChange only from within the SetInnerConnectionEvent function.

据我所知(部分类的废话使它变得困难),SqlConnection.SetInnerConnectionEvent 仅从 SqlConnectionFactory.SetInnerConnectionEvent.那个来自:

As far as I can tell (the partial class nonsense makes it difficult), the SqlConnection.SetInnerConnectionEvent is only called from SqlConnectionFactory.SetInnerConnectionEvent. And that is called from:

因此,总而言之 - 该事件仅在响应客户端操作时引发 - 似乎没有对 SQLConnection 中内置的连接状态进行任何轮询.

So, in summary - the event is only raised in response to client-side actions - there does not appear to be any polling of the connection-state built into SQLConnection.

有没有办法改变这种行为?

Is there a way this behavior can be changed?

查看源代码,我看不到一个.正如其他人所建议的那样,您当然可以实施自己的轮询.

Looking at the source code, I can't see one. As others have suggested, you could implement your own polling, of course.

这篇关于何时调用 DbConnection.StateChange?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆