Multi-threaded DataTable causes NullReferenceException

K

Koji Ishii

Hello,

I'm using a single DataTable in multi-threaded scenario. The DataTable is
not connected to any database, just created and filled in from local XML
file.

Since multiple threads can modify this single DataTable, I use C# lock
statements whenever I make changes to the DataTable, but sometimes, where
repro-steps are not exactly clear, ADO.NET causes NullReferenceException.
Here's stack trace:
at System.Data.DataTable.get_LiveIndexes()
at System.Data.DataTable.SetNewRecord(DataRow row, Int32 proposedRecord,
DataRowAction action, Boolean isInMerge)
at System.Data.DataRow.EndEdit()
at System.Data.DataRow.set_Item(DataColumn column, Object value)

I don't remember where but I remember I read notes saying ADO.NET supports
multi-thread only in DataSet/DataTable, so my understanding is that as long
as I serialize modify-operations (new, add, delete, change), it should work.
But since the exception occurs deep inside of the ADO.NET, I'm guessing this
is ADO.NET bug.

Has anyone experienced errors like this? Any pointers to workarounds are
greatly appreciated.

Note that similar errors were seen since .NET Framework 1.0, but stack
traces were different. This is from .NET Framework 1.1.


Thanks,
Koji Ishii
 
A

Alvin Bruney

run your application, just before this error occurs, go to the debugger and
press CTRL ALT E, change the option to break into debugger. let the
execution proceed. when the exception is thrown, it will take you to the
offending line of code as well as the complete call stack. bring your dinner
and start debugging. remember to turn this feature off when you are done.
 
K

Koji Ishii

Thank you for your reply.

I have the complete call stack, I just trimmed off before posting because it
gives you no clue.

It's a regular code that sets a value to an item, and thus it calls into
DataRow.set_Item. The code runs fine in 99% cases, so I'm assuming this is a
threading issue. Actually, although I'm running my app 24x7, I can never see
this error by myself; some of my users have reported the call stacks, so
it's probably even more rare than 99%...

Are there any known issues using DataSet/DataTable in multi-threaded
environment? In Google, I found two topics; one recommends to lock the table
when you set DataView.RowFilter, since it may modify some fields in
DataTable. I did that but the error still happens. The other just
recommended not to use DataSet/DataTable, but build your own if multi-thread
is requirement. That can't be a choice for me. So, there seems to be at
least three persons including me who are in trouble with DataTable and
multi-thread. I'm just wondering there are more, and someone may be able to
provide clear statements of what can do or cannot, hopefully along with
workaround.

Are there?


Thank you
Koji Ishii
 
A

Alvin Bruney

one more thing came to mind. are these threads part of the same application.
if so all is well. lock will work. however, if the threads are coming from
other applications, lock has no effect. you will need to use something more
potent to synchronize at a lower level. lock is a critical section safe
within the housing application only.
 
K

Koji Ishii

No, this is a single application with single appdomain.

I'm locking DataTable, but is this the cause of problem? I can't find any
documentation about what to lock, do I need to lock DataView as well? Or
whole DataSet?


Thanks,
Koji Ishii
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top