c# - Remembering SQL connection state in .net? -


beside's old known fact connection.close() vs connection.dispose() same - except running close() on disposed connection raises exception while running close() on closed connection - ok - still have question :

assuming connection pooling on , (default) - why important remember state of connection ?

i've read this question here shows - avoiding opening , closing connection saves performance.

this seems logic , problem connection never closed ! marked close.

even if use under using scope - dispose closes connection , put in pool.

even if wanted , couldn't leave open ( because i'd want others use it). had close/dispose it.

looking @ dapper implements behavior:

public static async task<ienumerable<t>> queryasync<t>(this...)         {          //...             bool wasclosed = cnn.state == connectionstate.closed;             using (var cmd = (dbcommand)command.setupcommand(cnn, info.paramreader))             {                 try                 {                     if (wasclosed) await ((dbconnection)cnn).openasync()...                   //...                 }                                 {                     if (wasclosed) cnn.close();                 }             }         } 

as can see "memorization" implemented here.

nb , asked marc related topic - why in dapper samples uses both getclosedconneciton , getopenconnection , got answer show - dapper can deal both scenarios. current question why re-set connections state.

question :

looking @ dapper code seems remembers state , re-set state after operation. ( know behavior old sqldataadapter class)

the question - why ? if got closed connection - then, need open it. great. why have close condition ? why not close ? it's not going hurt performance since connection not closed - returned pool.

the other way around - if got open connection , i'd work , keep open (huh??)

as see , i'm missing here. can please shed light ?

why not close ?

the user doing lots of work on connection. associated transaction (closing orphan it). have temporary tables (closing destroy them), or other connection-preserved state (set options, impersonation, etc).

closing connection here (if open originally) unusual , unexpected thing multiple nasty side-effects.


Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -