Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/23/09 11:35:41 (15 years ago)
Author:
svonolfe
Message:

Avoid out of memory exceptions related to job objects (#372)

File:
1 edited

Legend:

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

    r2004 r2082  
    8282
    8383    protected delegate IEnumerable<RowT> Selector();
     84    protected delegate object TransactionalAction();
    8485
    8586    protected ObjT Convert(RowT row, ObjT obj) {
     
    109110
    110111    protected ObjT FindSingle(Selector selector) {
    111       ITransaction trans =
    112        session.GetCurrentTransaction();
    113       bool transactionExists = trans != null;
    114       if (!transactionExists) {
    115         trans = session.BeginTransaction();
    116       }
    117 
    118       try {
    119         RowT row = FindSingleRow(selector);
    120 
    121         ObjT result;
    122         if (row != null) {
    123           ObjT obj = new ObjT();
    124           obj = Convert(row, obj);
    125 
    126           result = obj;
    127         } else {
    128           result = default(ObjT);
    129         }
    130 
    131         return result;
    132       }
    133       finally {
    134         if (!transactionExists && trans != null) {
    135           trans.Commit();
    136         }
    137       }
     112      return
     113        (ObjT)doInTransaction(
     114         delegate() {
     115          RowT row = FindSingleRow(selector);
     116
     117          ObjT result;
     118          if (row != null) {
     119            ObjT obj = new ObjT();
     120            obj = Convert(row, obj);
     121
     122            result = obj;
     123          } else {
     124            result = default(ObjT);
     125          }
     126
     127          return result;
     128        });
    138129    }
    139130
    140131    private ICollection<ObjT> FindMultiple(Selector selector,
    141132      int from, int size) {
    142       ITransaction trans =
    143        session.GetCurrentTransaction();
    144       bool transactionExists = trans != null;
    145       if (!transactionExists) {
    146         trans = session.BeginTransaction();
    147       }
    148 
    149       try {
    150         IEnumerable<RowT> found =
    151           selector();
    152 
    153         if (from > 0 && size > 0)
    154           found = found.Skip<RowT>(from).Take<RowT>(size);
    155 
    156         IList<ObjT> result =
    157           new List<ObjT>();
    158 
    159         foreach (RowT row in found) {
    160           ObjT obj = new ObjT();
    161           obj = Convert(row, obj);
    162           if (obj != null)
    163             result.Add(obj);
    164         }
    165 
    166         return result;
    167       }
    168       finally {
    169         if (!transactionExists && trans != null) {
    170           trans.Commit();
    171         }
    172       }     
     133      return (ICollection<ObjT>)doInTransaction(
     134        delegate() {
     135          IEnumerable<RowT> found =
     136            selector();
     137
     138          if (from > 0 && size > 0)
     139            found = found.Skip<RowT>(from).Take<RowT>(size);
     140
     141          IList<ObjT> result =
     142            new List<ObjT>();
     143
     144          foreach (RowT row in found) {
     145            ObjT obj = new ObjT();
     146            obj = Convert(row, obj);
     147            if (obj != null)
     148              result.Add(obj);
     149          }
     150
     151          return result;
     152        });   
    173153    }
    174154
     
    185165    }
    186166
    187     protected virtual void doUpdate(ObjT obj) {
    188       if (obj != null) {
    189         RowT row = null;
    190 
    191         if (obj.Id != Guid.Empty) {
    192           row = GetRowById(obj.Id);
    193         } else {
    194           obj.Id = Guid.NewGuid();
    195         }
    196 
    197         if (row == null) {
    198           row = dataAdapter.InsertNewRow(obj);
    199         }
    200 
    201         ConvertObj(obj, row);
    202         dataAdapter.UpdateRow(row);
    203       }
    204     }
    205 
    206     public void Update(ObjT obj) {
     167    protected object doInTransaction(TransactionalAction action) {
    207168      ITransaction trans =
    208169        session.GetCurrentTransaction();
     
    213174
    214175      try {
    215         doUpdate(obj);
     176        bool result = (bool)action();
     177
     178        if (!transactionExists && trans != null) {
     179            trans.Commit();
     180        }
     181
     182        return result;
     183      }
     184      catch (Exception e) {
     185        if (!transactionExists && trans != null) {
     186          trans.Rollback();
     187        }
     188
     189        throw e;
     190      }
     191    }
     192
     193    protected virtual void doUpdate(ObjT obj) {
     194      if (obj != null) {
     195        RowT row = null;
     196
     197        if (obj.Id != Guid.Empty) {
     198          row = GetRowById(obj.Id);
     199        } else {
     200          obj.Id = Guid.NewGuid();
     201        }
     202
     203        if (row == null) {
     204          row = dataAdapter.InsertNewRow(obj);
     205        }
     206
     207        ConvertObj(obj, row);
     208        dataAdapter.UpdateRow(row);
     209      }
     210    }
     211
     212    public void Update(ObjT obj) {
     213      try {
     214        doInTransaction(
     215          delegate() {
     216            doUpdate(obj);
     217            return true;
     218          });       
    216219      }
    217220      catch (DBConcurrencyException ex) {
     
    234237        //otherwise: row was deleted in the meantime - nothing to do
    235238      }
    236       finally {
    237         if (!transactionExists && trans != null) {
    238           trans.Commit();
    239         }
    240       }
    241239    }
    242240
     
    282280
    283281    public bool Delete(ObjT obj) {
    284       ITransaction trans =
    285         session.GetCurrentTransaction();
    286       bool transactionExists = trans != null;
    287       if (!transactionExists) {
    288         trans = session.BeginTransaction();
    289       }
    290 
    291282      try {
    292         return doDelete(obj);
     283        return (bool)doInTransaction(
     284          delegate() {
     285            return doDelete(obj);
     286          });
    293287      }
    294288      catch (DBConcurrencyException) {
     
    303297        }
    304298      }
    305       finally {
    306         if (!transactionExists && trans != null) {
    307           trans.Commit();
    308         }
    309       } 
    310299    }
    311300  }
Note: See TracChangeset for help on using the changeset viewer.