Solved

sync lock for datatables

Posted on 2014-03-16
2
568 Views
Last Modified: 2014-05-17
I have a dataset with several datatables which are variably updated and refilled from the database. From time to time I get exceptions about corrupted indexes and I suspect that they are related to simultaneous modification of the datatables from different threads.

So, I have a couple of questions:
1. What is the best way to synchronize these activities?
2. When using sync lock, what object should I lock? should I use the datatable itself? Is there any benefit in using datatable.syncroot?

For example (ds is a dataset containing several tables; da is dataadapter)

'in response to user action
SyncLock ds.Tables("Master")
     ' modify rows in table Master
End SyncLock

'elsewhere, runs once every 60 seconds
SyncLock ds.Tables("Master")
     da.Fill(ds, "Master")
End SyncLock

Open in new window



Here is an example error
System.ArgumentNullException occurred
  HResult=-2147467261
  Message=Value cannot be null.
Parameter name: key
  ParamName=key
  Source=mscorlib
  StackTrace:
       at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       at System.Data.DataView.MaintainDataView(ListChangedType changedType, DataRow row, Boolean trackAddRemove)
       at System.Data.Listeners`1.Notify[T1,T2,T3](T1 arg1, T2 arg2, T3 arg3, Action`4 action)
       at System.Data.Index.MaintainDataView(ListChangedType changedType, Int32 record, Boolean trackAddRemove)
       at System.Data.Index.InsertRecord(Int32 record, Boolean fireEvent)
       at System.Data.Index.ApplyChangeAction(Int32 record, Int32 action, Int32 changeRecord)
       at System.Data.DataTable.RecordStateChanged(Int32 record1, DataViewRowState oldState1, DataViewRowState newState1, Int32 record2, DataViewRowState oldState2, DataViewRowState newState2)
       at System.Data.DataTable.SetNewRecordWorker(DataRow row, Int32 proposedRecord, DataRowAction action, Boolean isInMerge, Boolean suppressEnsurePropertyChanged, Int32 position, Boolean fireEvent, Exception& deferredException)
       at System.Data.DataTable.LoadDataRow(Object[] values, Boolean fAcceptChanges)
       at System.Data.ProviderBase.SchemaMapping.LoadDataRow()
       at System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping)
       at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
       at System.Data.Common.DataAdapter.Fill(DataSet dataSet, String srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
       at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
       at Documenter.clsDB2.DAFill(DBTable dbt) in C:\Users\mankowitzs\Documents\Visual Studio 2010\Projects\Documenter\Documenter\ClassDB2.vb:line 1207
  InnerException: 

Open in new window

0
Comment
Question by:mankowitz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39936779
Datatable level locking should work.
0
 
LVL 24

Author Closing Comment

by:mankowitz
ID: 40072784
ok
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question