sql server - .NET, the SqlConnection object, and multi-threading -


we have application uses sql server 2008 r2 database. within application, calls database made using sqlconnection object.

this sqlconnection object initialized once, first time accessed, , re-used throughout application. action use following:

protected _cn sqlconnection = nothing  ...  protected sub open()     if _cn nothing         _cn = new sqlconnection(_sqlconn)     end if      if _cn.state = connectionstate.closed orelse _cn.state = connectionstate.broken         _cn.open()     end if end sub 

this works fine during normal execution of program. however, there few portions of application executed in multi-threaded fashion. when 1 of these parts executing, frequent errors occur if other actions made.

after digging bit, realised because there times 2 different threads both attempted use same sqlconnection object.

so, after identifying problem, need find solutions. obvious solution re-create sqlconnection object every time database call requires 1 - in case, never shared. there reason not this? assumed had 1 connection object per session of application performance reasons, case?

if need keep 1 connection object open, suggested solution? should put in place sort of timer keep cycling until connection object available, , access it?

the obvious solution re-create sqlconnection object every time database call requires 1 - in case, never shared. there reason not this?

on contrary, that's absolutely should do. that's behaviour sqlconnection designed for. should use using statement automatically close connection @ end of block you're using for, , connection pool mechanism automatically handle real underlying connections database.


Comments

Popular posts from this blog

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

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

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