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

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

问题描述

我有一个 Log 类,它将日志放入 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 中,它说:此类型的任何 public static(在 Visual Basic 中共享)成员都是线程安全的.不保证任何实例成员都是线程安全的.

In MSDN, it says: 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);

它是线程安全的吗?如果是,什么时候使用 Open()Close()?

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

如果不是,如何正确使用SqlConnection?

If no, how use properly SqlConnection?

这是我的完整课程代码:

Here is my full class code :

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;
}

推荐答案

SqlConnection 的共享方式并不常见,只能在特殊情况下使用.

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

首先,资源池是一种常见的模式,用于在使用套接字、网络流、Web 服务时提高性能......

First, You're true that resource 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.

每当用户在连接上调用 Open 时,池化器都会寻找一个池中的可用连接.如果池连接可用,它将它返回给调用者而不是打开一个新连接.什么时候应用程序在连接上调用 Close,池化器返回它到池中的活动连接集,而不是关闭它.一次连接返回到池中,它可以被重用下一个公开电话

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 视为真实连接的包装器.不要相信实例化一个新的 SqlConnection 是昂贵的:它不是,许多高流量的网站都是用它构建的.

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)是它会自动工作.您只需要注意关闭连接(使用 using 块).还有许多设置可以管理池.

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 连接不适合您的情况.使用异步日志库,您将获得更多性能.

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% 的时间:过早的优化是万恶之源,Donald Knuth

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天全站免登陆