使用 BasePage 打开和关闭 SQL 连接有多安全或多坏?

发布于 2024-09-17 05:43:05 字数 1004 浏览 3 评论 0原文

我有 BasePage.cs 类,它被其他 .cs 文件使用,而不是 System.Web.UI.Page (公共部分类页面:BasePage)。

我用它来打开和关闭 SQL 连接,以确保每个 SQL 连接都被关闭。

代码看起来像这样

{
    public class BasePage: System.Web.UI.Page
    {
        public SqlConnection globalConnection;

        protected override void OnInit(EventArgs e)
        {
             globalConnection = new SqlConnection();
             globalConnection.ConnectionString = ConfigurationManager.ConnectionStrings["kontemiConnectionString"].ToString();
             globalConnection.Open();
        }

        protected override void OnUnload(EventArgs e)
        {
            if (globalConnection != null)
            {
                 globalConnection.Close();
            }
        }
    }
}

到目前为止,它对我来说效果非常好。这意味着每次连接打开时它也会关闭。或者至少我是这么认为的。

我的问题是这个解决方案是否是万无一失的,并且每个连接都会被关闭,以防代码执行期间出现一些处理错误。跟踪此代码时,如果我故意创建错误 500,它总是会转到 OnUnload 事件并关闭。

那么,您认为这次执行安全吗? (为了停止讨论我是否应该在实际需要时打开 SQL,答案是每个使用 BasePage 的页面也会打开一个 SQL 连接。)

I have BasePage.cs class which is being used by other .cs files instead of System.Web.UI.Page (public partial class page : BasePage).

I am using it for opening and closing SQL connections to make sure every SQL connection gets closed.

Code looks like this

{
    public class BasePage: System.Web.UI.Page
    {
        public SqlConnection globalConnection;

        protected override void OnInit(EventArgs e)
        {
             globalConnection = new SqlConnection();
             globalConnection.ConnectionString = ConfigurationManager.ConnectionStrings["kontemiConnectionString"].ToString();
             globalConnection.Open();
        }

        protected override void OnUnload(EventArgs e)
        {
            if (globalConnection != null)
            {
                 globalConnection.Close();
            }
        }
    }
}

So far it has worked really well for me. It means that every time a connection opens it also gets closed. Or at least I thought so.

My question is whether this solutions is bulletproof and every single connection gets closed in case there is some processing error during code execution. When tracing this code, if I on purpose create error 500 it always goes to OnUnload event and gets closed.

So, do you think is this execution safe?
(To stop discussion whether I shouldn't open SQL when I actually need it, answer is that every page which uses BasePage also opens a SQL connection.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

压抑⊿情绪 2024-09-24 05:43:06

在使用 SQL 连接的地方打开和关闭它们是更好的做法。在幕后,CLR 无论如何都会管理连接池 - 重用连接并在认为合适时关闭它们。使用相同的连接字符串对连接进行大量的打开和关闭操作不会产生您预期的那么多开销。

Opening and closing SQL connections where you are using them is better practice. Under the hood, the CLR manages connection pooling anyway - reusing connections and closing them when it sees fit. Doing lots of open and closes on connections with the same connection string doesn't add up to as much overhead as you might expect.

谁把谁当真 2024-09-24 05:43:06

我可能是错的,但我的猜测是,如果页面生命周期在 OnUnload 触发之前通过异常结束,连接不会关闭。为防止这种情况,您至少可以做的是确保在全局“最后机会”级别捕获所有异常,并在那里关闭连接。我仍然认为在本地使用连接(最好是使用 using 块)是更好的解决方案,因为它不会使连接保持打开状态超过所需的时间,并且您不必担心关闭它们( using 的语义将为您完成这项工作)。

I might be wrong, but my guess would be that the connection does not get closed if the page lifecycle is ended through an exception before OnUnload fires. The very least you could do to prevent this is make sure you catch all exceptions on a global 'last-chance' level, and close the connection there. I still think using connections locally, ideally with using blocks, is the better solution, because it doesn't keep connections open longer than it needs to, and you don't have to worry about closing them (the semantics of using will do the work for you).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文