-1
using (IDbConnection dbConn = new System.Data.SqlClient.SqlConnection(ConnectionString))
{
    dbConn.Open();
    ...
    return result;
}
  1. Will the dbConn.Open() inside the using will use a different connection pool?
  2. If the question number 1 is true and if the code reaches the return, will this code leaves 2 open connection hanging?
jarlh
  • 42,561
  • 8
  • 45
  • 63
cypher-lad
  • 15
  • 3
  • Different than what? – nvoigt Jul 16 '20 at 14:34
  • for the question #1, I mean will it open up another connection? – cypher-lad Jul 16 '20 at 14:35
  • @juharr if the connection is open you get an exception – apomene Jul 16 '20 at 14:36
  • 1
    @RaoHammas using statement ensures that the connection will be closed and disposed, no need to close – apomene Jul 16 '20 at 14:41
  • so even if you return before the closing bracket it will close/dispose the connection? – cypher-lad Jul 16 '20 at 14:42
  • 1
    @Mrky Correct, it will close and dispose when it goes out of scope, thanks to the using statement. I suggest you read more about [IDisposable](https://learn.microsoft.com/en-us/dotnet/api/system.idisposable?view=netcore-3.1). – mason Jul 16 '20 at 14:44
  • @mason Thanks for reminding. idk why i ignored that. – Hammas Jul 16 '20 at 14:45
  • the using block is syntactic sugar for "try-finally" with finally block calling the Dispose method. If there's no dispose (for any specific class), the using block doesn't makes affects anything. In this case, there will be an exception at the Open call and then the original connection will get disposed. – NitinSingh Jul 16 '20 at 14:55

2 Answers2

1

Will the dbConn.Open() inside the using will use a different connection pool?

There is only one "connection pool".

If the question number 1 is true and if the code reaches the return, will this code leaves 2 open connection hanging?

Well question 1 is false, but here's what actually happens:

  • The code in the using statement creates the connection object.
  • When you call Open(), the connection object asks for an existing database connection from the pool, and creates one if the pool does not have one (perhaps the pool actually creates it, but that's an implementation detail)
  • When the block ends (or an exception is thrown), the using block goes out of scope and the connection object is disposed of, which closes and releases the database connection.

So there is only one connection object and one database connection. It is automatically disposed of when the using block goes out of scope (either by completing or my throwing an exception).

D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

It will create a new connection which will then be disposed of when the using block has reached the end. It's a good idea to close you connection when you no longer need it, if you have more going on in the using block but the using block will handle that.

When the code reaches the return, it exits out of the using statement so it then does the disposing. It's not like it returns and doesn't finish in case thats what you are asking. The dispose is called as soon as the execution leaves that scope. Even if you get an exception the Dispose will be called.

A using block gets compiled into a try catch finally block and the finally is always executed and the finally contains the Dispose

C-Sharpie
  • 195
  • 10
  • It will create a new connection object, but you're ignoring the pooling aspect of the question. – mason Jul 16 '20 at 14:51