Solved

sync lock for datatables

Posted on 2014-03-16
2
541 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

778 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