SqlConnection的线程安全? [英] SqlConnection Thread-Safe?

查看:232
本文介绍了SqlConnection的线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索在谷歌我的问题,但我没有发现这方面有任何解决方案。

I searched on Google about my problem, but I didn't find any solution about this.

我有一个日志类里面放日志在Windows日记本和SQL表。 。为了优化我的代码,我想只使用一个SqlConnection的

I have a Log Class which put logs in Windows journal and in a SQL table. In order to optimize my code, I would like use only one SqlConnection.

在MSDN,它说:任何公共静态(共享在Visual Basic中)这种类型的成员线程安全的。 。任何实例成员不能保证是线程安全的。

In MSDN, it said : Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

我的问题是:

private static readonly SqlConnection conn = new SqlConnection(ConfigParameters.Instance.UIDConnection);



是线程安全的?如果是的话,使用时打开()和关闭()?

is it thread-safe ? If yes, when use Open() and Close() ?

如果没有,如何正确使用的SqlConnection?

If no, how use properly SqlConnection ?

这是我满级代码:

private static readonly SqlConnection conn = new SqlConnection(ConfigParameters.Instance.UIDConnection);

public static long WriteLog(string sSource, string sMessage, int iErrorCode, EventLogEntryType xErrorType)
{
    // Windows Logs
    if (ConfigParameters.Instance.WindowsLog)
        EventLog.WriteEntry(sSource, sMessage, xErrorType, iErrorCode);

    // SQL Logs
    // TODO

    return 0;
}



在此先感谢:)

Thanks in advance :)

推荐答案

这不是共享一个SqlConnection常见的方式,只有在特殊的使用情况下,它应该被使用。

It's not a common way to share a SqlConnection, and it should be used only under special uses cases.

首先,你是真的的ressource池是用来与插座,网络流,Web服务时提高性能的通用模式...

First, You're true that ressource pooling is a common pattern used to improve performance when working with sockets, network streams, web services...

但尤其是对的SqlConnection,你不必担心这一点,因为该框架已经为你做这个,感谢的 SQL连接池

But especially for SqlConnection, you don't have to worry about this because the framework already do this for you, thanks to Sql Connection Pool.

当用户调用打开的连接上,池查找可用的
连接池中。如果一个池连接可用,
将它返回给调用者,而不是打开一个新的连接。当
中的应用程序调用连接关闭,池其返回
到池组的活动连接而不是关闭它。一旦
连接返回到池中,它准备在$ B $得到重用B中的下一个开放呼叫

Whenever a user calls Open on a connection, the pooler looks for an available connection in the pool. If a pooled connection is available, it returns it to the caller instead of opening a new connection. When the application calls Close on the connection, the pooler returns it to the pooled set of active connections instead of closing it. Once the connection is returned to the pool, it is ready to be reused on the next Open call

你可以考虑的SqlConnection围绕实际连接的包装。不要不敢相信这instanciating一个新的SqlConnection是昂贵的:它不是和高TRAFIC很多网站都用它建造的。

You can consider SqlConnection as a wrapper around real connection. Do not beleive that instanciating a new SqlConnection is costly : it's not and many web sites with high trafic are built with it.

默认策略(用于SQL Server至少)是它只是自动工作。你只需要知道你关闭连接(与使用块)。 。也有许多设置来管理池

The default strategy (for sql server at least) is that it will just work automatically. You just need to be aware of closing your connection (with a using block). There are also many settings to manage the pool.

您的代码还包含一个不正确的错误管理:如果连接被中止(DBA,网络故障......),你会抛出登录时...不理想

You code also contains an incorrect error management : if the connection is aborted (DBA, network failure, ...) you will throw exceptions when logging ... not ideal

目前为此例外,我不认为共享SQL连接你的情况适合。您将获得更多的PERF的使用异步日志库。

At this end, I don't think that sharing a sql connection is appropriate in your case. You will gain much more perf using an async logging library.

直到你相信这是一个真正的问题不关注这个了。

Do not focus on this now until you're sure it's a real problem.

我们应该忘记小的效率,说的时候约97%:
过早的优化是一切罪恶的根源,的高德纳

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil, Donald Knuth

这篇关于SqlConnection的线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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