Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/25/09 11:32:31 (15 years ago)
Author:
svonolfe
Message:

Refactored DAL, avoided possible race conditions (#372)

Location:
trunk/sources/HeuristicLab.Hive.Server.ADODataAccess
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/CachedDataAdapter.cs

    r1166 r1175  
    227227    public override void Update(ObjT obj) {
    228228      if (obj != null) {
    229         RowT row =
    230           GetRowById(obj.Id);
     229        RowT row = null;
     230        long locked = default(long);
     231
     232        if (obj.Id != default(long)) {
     233          LockRow(obj.Id);
     234          locked = obj.Id;
     235
     236          row = GetRowById(obj.Id);
     237        }
    231238
    232239        if (row == null) {
     
    238245
    239246          UpdateRow(row);
    240         }
    241 
    242         obj.Id = (long)row[row.Table.PrimaryKey[0]];
    243         LockRow(obj.Id);
     247          obj.Id = (long)row[row.Table.PrimaryKey[0]];
     248        }
     249
     250        if (locked == default(long)) {
     251          LockRow(obj.Id);
     252          locked = obj.Id;
     253        }
    244254
    245255        ConvertObj(obj, row);
     
    256266        }
    257267
    258         UnlockRow(obj.Id);
     268        UnlockRow(locked);
    259269      }
    260270    }
  • trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/ClientAdapter.cs

    r1166 r1175  
    2727using HeuristicLab.Hive.Contracts.BusinessObjects;
    2828using System.Linq.Expressions;
    29 using System.Runtime.CompilerServices;
    3029
    3130namespace HeuristicLab.Hive.Server.ADODataAccess {
     
    7978
    8079    private void Preprocess(ClientInfo client) {
    81       if (client != null) {
    82         if (client.Id == default(long)) {
    83           ClientInfo found = GetById(client.ClientId);
    84           if (found != null)
    85             client.Id = found.Id;
    86         }
     80      if (client != null && client.Id == default(long)) {
     81        ClientInfo found = GetById(client.ClientId);
     82        if (found != null)
     83          client.Id = found.Id;
    8784      }
    8885    }
     
    215212
    216213    #region IClientAdapter Members
    217     [MethodImpl(MethodImplOptions.Synchronized)]
    218214    public override void Update(ClientInfo client) {
    219215      if (client != null) {
     216        Guid locked = Guid.Empty;
     217        if (client.ClientId != Guid.Empty) {
     218          LockRow(client.ClientId);
     219          locked = client.ClientId;
     220        }
     221
    220222        Preprocess(client);
    221223
     
    223225
    224226        base.Update(client);
     227
     228        if (locked != Guid.Empty) {
     229          UnlockRow(locked);
     230        }
    225231      }
    226232    }
     
    248254    }
    249255
    250     [MethodImpl(MethodImplOptions.Synchronized)]
    251     public override bool Delete(ClientInfo client) {     
     256    public override bool Delete(ClientInfo client) {
     257      bool success = false;
     258      Guid locked = Guid.Empty;
     259     
    252260      if (client != null) {
     261        if (client.ClientId != Guid.Empty) {
     262          LockRow(client.ClientId);
     263          locked = client.ClientId;
     264        }
     265
    253266        Preprocess(client);
    254267
     
    264277          }
    265278
    266           return base.Delete(client) &&
     279          success = base.Delete(client) &&
    267280            ResAdapter.Delete(client);
    268281        }
    269282      }
    270283
    271       return false;
     284      if (locked != Guid.Empty) {
     285        UnlockRow(locked);
     286      }
     287
     288      return success;
    272289    }
    273290
  • trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/ClientGroupAdapter.cs

    r1166 r1175  
    2727using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
    2828using HeuristicLab.Hive.Contracts.BusinessObjects;
    29 using System.Runtime.CompilerServices;
    3029using System.Data;
    3130
     
    233232
    234233    #region IClientGroupAdapter Members
    235     [MethodImpl(MethodImplOptions.Synchronized)]
    236234    public override void Update(ClientGroup group) {
    237235      if (group != null) {
     
    273271    }
    274272
    275     [MethodImpl(MethodImplOptions.Synchronized)]
    276273    public override bool Delete(ClientGroup group) {
    277274      if (group != null) {
  • trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/DataAdapterBase.cs

    r1134 r1175  
    3737      new Mutex();
    3838
    39     private static IDictionary<long, Mutex> lockers =
    40       new Dictionary<long, Mutex>();
    41 
    42     private static IDictionary<long, int> lockCount =
    43       new Dictionary<long, int>();
    44 
    45     protected void LockRow(long id) {
     39    private static IDictionary<object, Mutex> lockers =
     40      new Dictionary<object, Mutex>();
     41
     42    private static IDictionary<object, int> lockCount =
     43      new Dictionary<object, int>();
     44
     45    protected void LockRow(object id) {
    4646      Mutex rowLock = null;
    4747
     
    6262    }
    6363
    64     protected void UnlockRow(long id) {
     64    protected void UnlockRow(object id) {
    6565      Mutex rowLock = lockers[id];
    6666      rowLock.ReleaseMutex();
     
    163163    public virtual void Update(ObjT obj) {
    164164      if (obj != null) {
    165         RowT row =
    166           GetRowById(obj.Id);
     165        RowT row = null;
     166        long locked = default(long);
     167       
     168        if (obj.Id != default(long)) {
     169           LockRow(obj.Id);
     170           locked = obj.Id;
     171
     172           row = GetRowById(obj.Id);
     173        }         
    167174
    168175        if (row == null) {
    169176          row = InsertNewRow(obj);
    170177          UpdateRow(row);
     178
     179          obj.Id = (long)row[row.Table.PrimaryKey[0]];
    171180        }
    172181
    173         obj.Id = (long)row[row.Table.PrimaryKey[0]];
    174         LockRow(obj.Id);
     182        if (locked == default(long)) {
     183          LockRow(obj.Id);
     184          locked = obj.Id;
     185        }
    175186
    176187        ConvertObj(obj, row);
    177188        UpdateRow(row);
    178189
    179         UnlockRow(obj.Id);
     190        UnlockRow(locked);
    180191      }
    181192    }
  • trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/JobAdapter.cs

    r1169 r1175  
    2727using HeuristicLab.Hive.Contracts.BusinessObjects;
    2828using System.Linq.Expressions;
    29 using System.Runtime.CompilerServices;
    3029
    3130namespace HeuristicLab.Hive.Server.ADODataAccess {
     
    119118          job.DateCalculated = DateTime.MinValue;
    120119
    121         job.Priority = row.Priority;
     120        if (!row.IsPriorityNull())
     121          job.Priority = row.Priority;
     122        else
     123          job.Priority = default(int);
    122124
    123125        return job;
     
    332334    }
    333335
    334     [MethodImpl(MethodImplOptions.Synchronized)]
    335336    public override bool Delete(Job job) {
    336337      if (job != null) {
  • trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/PermissionOwnerAdapter.cs

    r1166 r1175  
    2727using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
    2828using HeuristicLab.Hive.Contracts.BusinessObjects;
    29 using System.Runtime.CompilerServices;
    3029
    3130namespace HeuristicLab.Hive.Server.ADODataAccess {
  • trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/ResourceAdapter.cs

    r1166 r1175  
    2626using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
    2727using HeuristicLab.Hive.Contracts.BusinessObjects;
    28 using System.Runtime.CompilerServices;
    2928
    3029namespace HeuristicLab.Hive.Server.ADODataAccess {
  • trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/UserAdapter.cs

    r1166 r1175  
    2727using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
    2828using HeuristicLab.Hive.Contracts.BusinessObjects;
    29 using System.Runtime.CompilerServices;
    3029
    3130namespace HeuristicLab.Hive.Server.ADODataAccess {
     
    136135    #region IUserAdapter Members
    137136
    138     [MethodImpl(MethodImplOptions.Synchronized)]
    139137    public override void Update(User user) {
    140138      if (user != null) {
     
    156154    }
    157155
    158     [MethodImpl(MethodImplOptions.Synchronized)]
    159156    public override bool Delete(User user) {
    160157      if (user != null) {
  • trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/UserGroupAdapter.cs

    r1166 r1175  
    2727using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
    2828using HeuristicLab.Hive.Contracts.BusinessObjects;
    29 using System.Runtime.CompilerServices;
    3029using System.Data;
    3130
     
    234233
    235234    #region IUserGroupAdapter Members
    236     [MethodImpl(MethodImplOptions.Synchronized)]
    237235    public override void Update(UserGroup group) {
    238236      if (group != null) {
     
    273271    }
    274272
    275     [MethodImpl(MethodImplOptions.Synchronized)]
    276273    public override bool Delete(UserGroup group) {
    277274      if (group != null) {
Note: See TracChangeset for help on using the changeset viewer.