简体   繁体   English

C#定时器,以保持SQL服务器连接到数据库“活着”

[英]c# Timer to keep the sql-server connection to db “Alive”

I have met with the following problem. 我遇到了以下问题。 When the software does not make queries for long time, the connection to the database seems to go into a sleep mode, so that when I do perform a query it needs some time to wake-up. 当软件长时间不进行查询时,与数据库的连接似乎进入了睡眠模式,因此当我执行查询时,它需要一些时间才能唤醒。 My solution to that is a timer defined in a static class that every 20 seconds does just this: opens and closes the connection. 我的解决方案是在静态类中定义一个计时器,该计时器每20秒执行一次:打开和关闭连接。 My question is, since the timer is asynchronous is my approach thread safe? 我的问题是,由于计时器是异步的,我的方法线程安全吗? This is my code. 这是我的代码。

 public static class Db
 {
    private static string connStr = "";
    private static System.Timers.Timer TimerConn = null;

     public static void Init(string theconnStr)
        {
            connStr = theconnStr;
            if (TimerConn!=null)
            {
                TimerConn.Enabled = false;
            }
            TimerConn = new System.Timers.Timer();
            TimerConn.Interval = 20000;
            TimerConn.Elapsed += HandleTimerConTick;

            TimerConn.Enabled = true;
        }


        private static void HandleTimerConTick(object source, ElapsedEventArgs e)
        {
            TestConnection();
        }
}


public static bool TestConnection()
        {
            try
            {
                SqlConnection con = new SqlConnection(connStr);
                con.Open();
                con.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

  }

Given the only shared state used is the connection string, yes it is thread-safe. 由于使用的唯一共享状态是连接字符串,是的,它是线程安全的。 If the connection string changes over time (which seems unlikely based on your code) then it may try and connect to an 'old version' of the connection string, but that is the worst behaviour that you might see. 如果连接字符串随时间变化(根据您的代码,这似乎不太可能),则它可能会尝试连接到连接字符串的“旧版本”,但这是您可能会看到的最糟糕的行为。 This won't be an issue if the connection string never changes. 如果连接字符串永不更改,这将不是问题。

One thing to consider, to make it slightly cleaner, is to change to: 为了使其更加简洁,要考虑的一件事是更改为:

try
{
    using (SqlConnection con = new SqlConnection(connStr))
    {
        con.Open();
    }
    return true;
}
catch
{
    return false;
}

to remove the need for the explicit Close call. 无需显式的Close调用。

Note, I am not commenting on whether your code is a good idea. 注意,我没有评论您的代码是否是个好主意。 It doesn't seem to be a particularly good idea to be continually connecting and disconnecting from the database like that, but that is outside the scope of your question. 像这样不断地与数据库连接和断开连接似乎不是一个特别好的主意,但这超出了您的问题范围。 I would suggest only reconnecting every three minutes or so though, since normally connections are removed from the pool after four minutes . 我建议每三分钟左右重新连接一次,因为通常四分钟后会从池中删除连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM