I have a Windows service (C#) where I create multiple threads which will try to update status in my database (SQL Server 2005).
I have one SELECT initially and an UPDATE later that has to be executed. When I don’t use lock to synchronize my threads, I am getting error
There is already an open DataReader associated with this Connection
which must be closed first
But when I use lock (on static object) the update is very slow.
Can anyone help me out in solving this issue .
Each command object that you’re using in your application should have it’s own connection object, that you open immediately before executing the command, and close immediately afterwards. (Preferably, wrapped in a
usingstatement)Trust the connection pooling tech to deal with the actual underlying connections. This will stop multiple threads from trying to execute multiple commands on the same connection simultaneously (which is what the error you’re seeing is talking about).
The only thing that should be shared between the threads is the connection string.