简体   繁体   English

.NET,SqlConnection对象和多线程

[英].NET, the SqlConnection object, and multi-threading

We have an application which uses an SQL Server 2008 R2 database. 我们有一个使用SQL Server 2008 R2数据库的应用程序。 Within the application, calls to the database are made using a SqlConnection object. 在应用程序中,使用SqlConnection对象对数据库进行调用。

This SqlConnection object is initialized once, the first time it is accessed, and then re-used throughout the application. SqlConnection对象在第一次访问时初始化一次,然后在整个应用程序中重新使用。 The action that we use is the following: 我们使用的操作如下:

Protected _cn As SqlConnection = Nothing

...

Protected Sub Open()
    If _cn Is Nothing Then
        _cn = New SqlConnection(_sqlConn)
    End If

    If _cn.State = ConnectionState.Closed OrElse _cn.State = ConnectionState.Broken Then
        _cn.Open()
    End If
End Sub

This works perfectly fine during normal execution of the program. 这在程序的正常执行期间完美地工作。 However, there are a few portions of the application that are executed in a multi-threaded fashion. 但是,应用程序的一些部分以多线程方式执行。 When one of these parts is executing, frequent errors occur if other actions are made. 当其中一个部件正在执行时,如果进行其他操作,则会发生频繁的错误。

After digging a bit, I realised that this is because there were times where two different threads both attempted to use the same SqlConnection object. 在挖掘了一下之后,我意识到这是因为有时两个不同的线程都试图使用相同的SqlConnection对象。

So, after identifying the problem, I now need to find solutions. 因此,在确定问题后,我现在需要找到解决方案。 The obvious solution is to just re-create the SqlConnection object every time a database call requires one - in this case, it would never be shared. 显而易见的解决方案是每次数据库调用需要时重新创建SqlConnection对象 - 在这种情况下,它永远不会被共享。 Is there any reason not to do this? 有什么理由这样做吗? I assumed originally that we had only one connection object per session of the application for performance reasons, but is this actually the case? 我原先认为出于性能原因,每个应用程序会话只有一个连接对象,但实际情况如此吗?

If we do need to keep just one connection object open, what is the suggested solution? 如果我们确实需要保持一个连接对象打开,建议的解决方案是什么? Should I put in place some sort of timer which will keep cycling until the connection object is available, and then access it? 我应该安装某种计时器,它会一直循环,直到连接对象可用,然后访问它?

The obvious solution is to just re-create the SqlConnection object every time a database call requires one - in this case, it would never be shared. 显而易见的解决方案是每次数据库调用需要时重新创建SqlConnection对象 - 在这种情况下,它永远不会被共享。 Is there any reason not to do this? 有什么理由不这样做吗?

On the contrary, that's absolutely what you should do. 相反,这绝对是你应该做的。 That's the behaviour SqlConnection was designed for. 这就是SqlConnection 设计目的 You should use a Using statement to automatically close the connection at the end of the block you're using it for, and the connection pool mechanism will automatically handle the real underlying connections to the database. 您应该使用Using语句在您正在使用它的块的末尾自动关闭连接,并且连接池机制将自动处理与数据库的真实底层连接。

I see no reason NOT to create a SQL connection every time you need it. 我认为没有理由不在每次需要时创建SQL连接。 In fact, that is probably the best way to do it because it gives the .NET framework the flexibility to manage and reuse connections most efficiently. 实际上,这可能是最好的方法,因为它为.NET框架提供了最有效地管理和重用连接的灵活性。 Wrap each of your SQL connections in a USING so you hang on to them as short a time as possible. 在USING中包装每个SQL连接,以便尽可能短的时间挂起它们。

We've created a method that creates a connection and everyone uses that: 我们创建了一个创建连接的方法,每个人都使用它:

using (var conn = GetConnection())
    using (var proc = GetProcedure(conn, "procname"))
        using (var reader = proc.GetReader())
        {
            ... DB stuff
        }

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

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