当应用程序关闭,将DB连接紧密瞬间? [英] When application close, will DB connection close instantly?

查看:109
本文介绍了当应用程序关闭,将DB连接紧密瞬间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我一直一直怀疑....会么?

因为每当我写code使用DB连接,我总是莫名其妙必须确保流程下一块之前被关闭。

但是,当,如果我有一个开在其构造一个连接,直到它击中保存按钮不是将其关闭或取消按钮一C​​hildWindow。这时如果整个应用得到封闭,将数据库连接关闭瞬间?或者,它必须等待的超时而将自动关闭?

编辑:

所以,我想保持实时连接打开登录我的应用程序的所有错误:

 公共应用程序()
{

   ErrorHelper errorHelper =新ErrorHelper(); //将打开数据库连接
   AppDomain中currentDomain = AppDomain.CurrentDomain;
   currentDomain.UnhandledException + =新UnhandledExceptionEventHandler(currentDomain_UnhandledException);

}

///<总结>
///对于捕获所有异常,并把它们放到日志
///< /总结>
无效currentDomain_UnhandledException(对象发件人,UnhandledExceptionEventArgs E)
{

    errorHelper.WriteError(e.ExceptionObject为例外);

}
 

由于我不喜欢我怎么打开的连接,每次登录一个错误,所以我想保持连接,打开所有的时间。这是类似的OP我形容。在这种situration,它保持连接打开所有的时间。但是,将数据库连接紧密立即退出后?

解决方案
  

由于我不喜欢我怎么打开的连接,每次登录一个错误,所以我想保持连接,打开所有的时间。

这就是连接池是。你测量你的表现还是有你确定了强有力的证据,这是一个问题吗?

打开和关闭与使用块的连接,让连接池做的工作。

如果你的进程退出,您的连接将被关闭。

This is something I has been always wonder.... will it?

Because whenever I write code to use DB connection, I always somehow have to ensure is closed before process to next piece.

But when if the I have a ChildWindow that open a connection in its constructor and not close it until it hits the Save Button or Cancel Button. Then if the whole Application got closed, will the DB connection close instantly? Or it has to wait for the timeout and will close automatically?

EDIT:

So I am trying to keep a live connection open for logging all errors on my application:

public App()
{

   ErrorHelper errorHelper = new ErrorHelper(); // Will open DB connection
   AppDomain currentDomain = AppDomain.CurrentDomain;
   currentDomain.UnhandledException += new UnhandledExceptionEventHandler(currentDomain_UnhandledException);

}

/// <summary>
/// For catch all exception and put them into log
/// </summary>
void currentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{

    errorHelper.WriteError(e.ExceptionObject as Exception);            

}

Because I don't like how I open connection everytime log a single error, so I want to keep connection open all the time. This is similar as the OP I was describe. In this situration, it keeps connection open all the time. But will the DB connection close instantly after it exits?

解决方案

Because I don't like how I open connection everytime log a single error, so I want to keep connection open all the time.

That's what connection pooling is for. Have you measured your performance or have you identified strong evidence that this is a problem?

Open and close the connection with a using block and let the connection pool do it's job.

If your process exits, your connection will be closed.

这篇关于当应用程序关闭,将DB连接紧密瞬间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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