Hi Angel.
Thanks for the fast reply.
The issue is not urgent since we have the workaround. I mostly wanted to
verify that it is in fact a bug and perhaps save someone some headaches if
they encounter the same situation.
I did a quick benchmark consisting of 20000 typical (for our app) command
executions. First round I used single instances, locking on them as in the
code I posted. In the second I created the commands for each execution. The
result was a dead heat, the 'locking' scenario won but with a margin of less
than a 10th of a percent.
I also did a test doing the same number of command executions, with and
without the locks, on 10 threads doing 2000 invocations each. I would have
expected the 'lock' version to fare poorly compared to the one which creates
the command each time but they ended up being a dead heat as well. Reason
being that our typical sql commands execute very fast creating almost no
contention for the commands when locking on them.
I added a Sleep(50) for each of the commands (within the lock in the 'lock'
version) to simulate them taking longer to execute and in doing so the
contention for the shared command instances became very apparent. The 'lock'
version took three times longer (300%) to execute compared to the version
creating the instances each time.
So it seems the conclusion would be that sharing the command instances
imposes a penalty compared to creating the SqlCommand instances each time if
the application is highly multi threaded with execution of the sql being
expensive. In the other scenarios there does not seem to be a significant
difference.
However sharing the instances does not provide a significant benefit in any
of the scenarios why this approach probably should be avoided unless there
are very good reasons to use it.
Best Regards
Fredrik Sjodin
"Angel Saenz-Badillos[MS]" wrote:
> Base64,
> This looks like a bug to me, will have to take a look at it. The fact that
> it has a simple work arround and that it no longer repros in whidbey beta1
> probably means that it is going to take me a while to get to it though. If
> this is urgent you want to talk to PSS. I really appreciate the fact that
> you are including full repro code.
>
> I am not the biggest fan of using ado.net objects across threads, I assume
> that you want to prepare a complex command with parameters and then use
> these commands across threads? I would have thought that locking the command
> would be more expensive than this, have you not found it so?
> --
> Angel Saenz-Badillos [MS] Managed Providers
> This posting is provided "AS IS", with no warranties, and confers no
> rights.Please do not send email directly to this alias.
> This alias is for newsgroup purposes only.
> I am now blogging about ADO.NET: http://weblogs.asp.net/angelsb/
>
>
>
>
> "David Browne" <davidbaxterbrowne no potted (E-Mail Removed)> wrote in
> message news:(E-Mail Removed)...
> >
> > "David Browne" <davidbaxterbrowne no potted (E-Mail Removed)> wrote in
> > message news:%(E-Mail Removed)...
> > >
> > > "Base64" <(E-Mail Removed)> wrote in message
> > > news:18BCAD0C-8354-4BC1-9F0A-(E-Mail Removed)...
> > >> Doh.
> > >> >> I have a situation where a SqlCommand incorrectly throws an
> > >>> InvalidOperationException when two threads are using the same
> SqlCommand
> > >>> instance at different times. As you can see below the application
> > >>> synchronizes on the shared SqlCommand instances why thread
> > >>> synchronization is
> > >>> not the issue.
> > >>>
> > >
> >
> > Oops,
> >
> > That doesn't work either (it just happend to a couple of times).
> >
> > I guess you cannot reuse a SqlCommand while it's SqlConnection is
> fetching.
> >
> > David
> >
> >
> >
>
>
>