1

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?

King Chan
  • 4,212
  • 10
  • 46
  • 78
  • 1
    @Oded, I don't see anything hypothetical about the behavior of an exiting application. – mikerobi Mar 21 '12 at 19:56
  • @Oded as requested, I putted something I am working on. But like mikerobi said, I want to know about the behavior of exiting the application. Not workaround to ensure the connection close. – King Chan Mar 21 '12 at 20:05

2 Answers2

2

Short and simple answer: always use your connection in a using statement:

using (var db = new Connection())
{
    ...
}

and then you won't have to worry - it will just close when it goes out of scope, be it end of method, exception, or application shut down.

Ofer Zelig
  • 17,068
  • 9
  • 59
  • 93
  • 2
    True, but not answering the question. – H H Mar 21 '12 at 20:03
  • But your question is not clear - what are you trying to accomplish? Knowing IF the connection will close? Knowing WHEN the connection will close? You told to @Oded that your question is not hypothetical. Be specific. – Ofer Zelig Mar 21 '12 at 20:08
  • @OferZelig I just want to know if the application close, all the opened DB connection will instantly be close? So I don't have to worry about if there is chance the DB connection will holds for long time. (Say the application ran by 1000 users, if the connection will not be close instantly after application exit, but wait for a while to timeout and close. There are chance that many unused connection will be hold.) Sorry, maybe some people will think I am complicate the problem. But I just want to have clear idea what will happen. – King Chan Mar 21 '12 at 20:23
  • 1
    I think it depends on how your application is running under IIS - in its own application pool or not, etc. What's sure is that ADO.NET handles cleanup, so eventually (sooner or later) all connections will be closed and deleted, even in the connection pool. – Ofer Zelig Mar 21 '12 at 20:36
2

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.

Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55
  • `Using` block is basically just `try{...}finally{ obj.Dispose() }`. I remember when I was using NexusDB(yeah, not famous), Open connection with ADO everytime I write is much slower than Open connection once and write everything. – King Chan Mar 21 '12 at 20:15
  • 1
    @King Chan ADO.NET has connection pooling. http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx – dotjoe Mar 21 '12 at 20:30
  • @dotjoe Ooooh, I see. After reading it. I just wrote a small program to test the performance with ADO open connection (with MsSQL) all the time and open when when it need. It around the same. Strange...then how come when I test with NexusDB is much slower... +1 – King Chan Mar 21 '12 at 20:55