2

Possible Duplicate:
Close and Dispose - which to call?

Many of the functions in my data layer are not protected by try-catch or using clauses.

My GUI layer has try-catch clauses. Will this be enough?

Can I rely on the dbConnection and other objects being disposed and closed if an exception is raised? The GUI layer will handle the exceptions.

Community
  • 1
  • 1
CJ7
  • 22,579
  • 65
  • 193
  • 321

3 Answers3

8

Will this be enough?

No. Nothing is ever automatically disposed, except via using. Objects are not collected when they go out of scope - gargage collection happens later, intermittently, and is non-deterministic.

If you are creating connections, use using unless that is impossible (async callbacks, for example). Otherwise you could easily end up leaving lots of open connection objects hanging around, saturating the server.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Ok, so if I use `using`, can I still let the exceptions "bubble up" to the GUI layer to be handled? – CJ7 Aug 20 '12 at 07:43
  • @CJ7 yes, that will happen automatically; you don't need to change anything for that to keep happening, and it won't be impacted by adding `using`, **unless** there is a bad `Dispose()` implementation (WCF is notorious for this) – Marc Gravell Aug 20 '12 at 07:44
  • Is it actually a real problem? Can't the server handle lots of connections? Surely the gargage collection will produce a good outcome, unless you are doing something really wierd like opening connections in a big `for` loop. – CJ7 Aug 20 '12 at 08:32
  • @CJ7 lots? yes; insane amounts? no. If I left connections open in our code, I estimate we have about 6 seconds before the pool saturates and the system crashes. Note that this is the per-node connection pool, not the physical sql server limit; but... why would you expose your sql server to unnecessary load? Not handling these correctly is just lazy. – Marc Gravell Aug 20 '12 at 08:40
0

Maybe can probably do that, but that does not mean it is a good idea.

In general, I would definitely use a try-catch-finally clause (or using) around the logic for handling a DB-Connection. Not only will this be safer for the reasons you are asking about, but it will also be clearer that things are handled well, and it will make it harder to accidentally mess something up if you should decide to change something in your GUI-layer later.

You should handle logic (and the creation and disposing of instances) of this type as close to where you need it as possible.

Note: A Finally clause will let you ensure a connection is closed, but still let any exception "bubble up" to the top, if that is what you need.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
0

As already mentioned, garbage collection is independent of variables going out of scope. As far as close and dispose is concerned for DbConnection, in case of SqlConnection if you call Dispose, if closes the connection and then disposes it. Although there are other providers where we need to explicitly close and then dispose. Dispose, for instance, in ODP.Net's OracleConnection, does not closes the connection.

You need to understand that DbConnection is an abstract class and it totally depends on the concrete class when it comes to actual behavior.

danish
  • 5,550
  • 2
  • 25
  • 28