Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/05/09 13:52:10 (16 years ago)
Author:
svonolfe
Message:

Improved transaction handling and locking (#527)

Location:
trunk/sources/HeuristicLab.DataAccess.ADOHelper
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.DataAccess.ADOHelper/DataAdapterBase.cs

    r1515 r1516  
    3838    private IDataAdapterWrapper<AdapterT, ObjT, RowT> dataAdapter;
    3939
    40     private static Mutex lockersMutex =
    41       new Mutex();
    42 
    43     private static IDictionary<object, Mutex> lockers =
    44       new Dictionary<object, Mutex>();
    45 
    46     private static IDictionary<object, int> lockCount =
    47       new Dictionary<object, int>();
    48 
    4940    protected DataAdapterBase(
    5041      IDataAdapterWrapper<AdapterT, ObjT, RowT> dataAdapter) {
     
    5243    }
    5344
    54     protected void LockRow(object id) {
    55       Mutex rowLock = null;
    56 
    57       /////begin critical section////
    58       lockersMutex.WaitOne();
    59 
    60       if (!lockers.ContainsKey(id)) {
    61         lockers[id] = new Mutex();
    62         lockCount[id] = 0;
    63       }
    64       rowLock = lockers[id];
    65       lockCount[id]++;
    66      
    67       lockersMutex.ReleaseMutex();
    68       /////end critical section////
    69 
    70       rowLock.WaitOne();
    71     }
    72 
    73     protected void UnlockRow(object id) {
    74       Mutex rowLock = lockers[id];
    75       rowLock.ReleaseMutex();
    76 
    77       /////begin critical section////
    78       lockersMutex.WaitOne();
    79 
    80       lockCount[id]--;
    81       if (lockCount[id] == 0)
    82         lockers.Remove(id);
    83 
    84       lockersMutex.ReleaseMutex();
    85       /////end critical section////
    86     }
    87    
    8845    protected AdapterT Adapter {
    8946      get {
     
    222179      if (obj != null) {
    223180        RowT row = null;
    224         Guid locked = Guid.Empty;
    225181
    226182        if (obj.Id != Guid.Empty) {
    227           LockRow(obj.Id);
    228           locked = obj.Id;
    229 
    230183          row = GetRowById(obj.Id);
    231184        } else {
     
    237190        }
    238191
    239         if (locked == Guid.Empty) {
    240           LockRow(obj.Id);
    241           locked = obj.Id;
    242         }
    243 
    244192        ConvertObj(obj, row);
    245193        dataAdapter.UpdateRow(row);
    246 
    247         UnlockRow(locked);
    248194      }
    249195    }
     
    259205      try {
    260206        doUpdate(obj);
     207      }
     208      catch (DBConcurrencyException ex) {
     209        DataRow row = ex.Row;
     210
     211        RowT current = GetRowById(obj.Id);
     212        if (current != null) {
     213          //find out changes
     214          for (int i = 0; i < row.ItemArray.Length; i++) {           
     215            if (!row[i, DataRowVersion.Current].Equals(
     216                 row[i, DataRowVersion.Original])) {
     217              current[i] = row[i];
     218            }
     219          }
     220
     221          ConvertRow(current, obj);
     222          //try updating again
     223          Update(obj);
     224        }
     225        //otherwise: row was deleted in the meantime - nothing to do
    261226      }
    262227      finally {
     
    283248
    284249      if (obj != null) {
    285         LockRow(obj.Id);
    286 
    287250        RowT row =
    288251          GetRowById(obj.Id);
     
    294257          success = true;
    295258        }
    296 
    297         UnlockRow(obj.Id);
    298259      }
    299260
     
    311272      try {
    312273        return doDelete(obj);
     274      }
     275      catch (DBConcurrencyException) {
     276        RowT current = GetRowById(obj.Id);
     277        if (current != null) {
     278          ConvertRow(current, obj);
     279          //try deleting again
     280          return Delete(obj);
     281        } else {
     282          //row has already been deleted
     283          return false;
     284        }
    313285      }
    314286      finally {
  • trunk/sources/HeuristicLab.DataAccess.ADOHelper/Transaction.cs

    r1496 r1516  
    4747            value.Open();
    4848
    49           transaction = value.BeginTransaction(IsolationLevel.RepeatableRead);
     49          transaction = value.BeginTransaction(IsolationLevel.ReadCommitted);
    5050        }
    5151      }
Note: See TracChangeset for help on using the changeset viewer.