Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5708


Ignore:
Timestamp:
03/16/11 13:32:39 (13 years ago)
Author:
cneumuel
Message:

#1233

  • changed the way transactions are handled
Location:
branches/HeuristicLab.Hive-3.4/sources
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.LifecycleClient/Program.cs

    r5636 r5708  
    66    private static void Main(string[] args) {
    77      using (var factory = ClientFactory.CreateChannelFactory<IHiveService>("wsHttpBinding_IHiveService", null, "hiveslave", "hiveslave")) {
    8         IHiveService client = factory.Obj.CreateChannel();
     8        IHiveService client = factory.CreateChannel();
    99        client.TriggerLifecycle(true);
    1010      }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/TransactionManager.cs

    r5055 r5708  
    11using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
     2using System.Transactions;
    53using HeuristicLab.Services.Hive.Common;
    6 using System.Transactions;
    7 using HeuristicLab.Common;
    8 using HeuristicLab.Clients.Common;
    94
    105namespace HeuristicLab.Services.Hive.DataAccess {
    116  public class TransactionManager {
    12     public Disposable<TransactionScope> OpenTransaction() {
    13       TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = ApplicationConstants.IsolationLevelScope });
    14       var disposable = new Disposable<TransactionScope>(transaction);
    15       disposable.OnDisposing += new EventHandler<EventArgs<object>>(disposable_OnDisposing);
    16       return disposable;
     7    public void UseTransaction(Action call) {
     8      TransactionScope transaction = CreateTransaction();
     9      try {
     10        call();
     11        transaction.Complete();
     12      }
     13      finally {
     14        transaction.Dispose();
     15      }
    1716    }
    1817
    19     void disposable_OnDisposing(object sender, EventArgs<object> e) {
    20       TransactionScope scope = (TransactionScope)e.Value;
    21       scope.Complete();
    22       scope.Dispose();
     18    public T UseTransaction<T>(Func<T> call) {
     19      TransactionScope transaction = CreateTransaction();
     20      try {
     21        T result = call();
     22        transaction.Complete();
     23        return result;
     24      }
     25      finally {
     26        transaction.Dispose();
     27      }
     28    }
     29
     30    private static TransactionScope CreateTransaction() {
     31      return new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = ApplicationConstants.IsolationLevelScope });
    2332    }
    2433  }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.4/HiveService.cs

    r5636 r5708  
    3535    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    3636    public Guid AddJob(Job job, JobData jobData, IEnumerable<Guid> resourceIds) {
    37       using (trans.OpenTransaction()) {
     37      return trans.UseTransaction(() => {
    3838        job.Id = dao.AddJob(job);
    3939        jobData.JobId = job.Id;
     
    4949        dao.UpdateJobState(job.Id, JobState.Waiting, null, auth.UserId, null);
    5050        return jobData.JobId;
    51       }
     51      });
    5252    }
    5353
     
    5555    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    5656    public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
    57       using (trans.OpenTransaction()) {
     57      return trans.UseTransaction(() => {
    5858        job.ParentJobId = parentJobId;
    5959        return AddJob(job, jobData, dao.GetAssignedResources(parentJobId).Select(x => x.Id));
    60       }
     60      });
    6161    }
    6262
     
    9797    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    9898    public void UpdateJob(Job job) {
    99       using (trans.OpenTransaction()) {
     99      trans.UseTransaction(() => {
    100100        dao.UpdateJob(job);
    101       }
     101      });
    102102    }
    103103
     
    106106    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    107107    public void UpdateJobData(Job job, JobData jobData) {
    108       using (trans.OpenTransaction()) {
     108      trans.UseTransaction(() => {
    109109        jobData.LastUpdate = DateTime.Now;
    110110        dao.UpdateJob(job);
    111111        dao.UpdateJobData(jobData);
    112       }
     112      });
    113113    }
    114114
     
    117117    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    118118    public void DeleteJob(Guid jobId) {
    119       using (trans.OpenTransaction()) {
     119      trans.UseTransaction(() => {
    120120        dao.DeleteJob(jobId);
    121       }
     121      });
    122122    }
    123123
     
    126126    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    127127    public void DeleteChildJobs(Guid parentJobId) {
    128       using (trans.OpenTransaction()) {
     128      trans.UseTransaction(() => {
    129129        var jobs = GetChildJobs(parentJobId, true, false);
    130130        foreach (var job in jobs) {
     
    132132          dao.DeleteJobData(job.Id);
    133133        };
    134       }
     134      });
    135135    }
    136136
     
    139139    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    140140    public Job UpdateJobState(Guid jobId, JobState jobState, Guid? slaveId, Guid? userId, string exception) {
    141       using (trans.OpenTransaction()) {
    142         return dao.UpdateJobState(jobId, jobState, slaveId, userId, exception);
    143       }
     141      return trans.UseTransaction(() => dao.UpdateJobState(jobId, jobState, slaveId, userId, exception));
    144142    }
    145143    #endregion
     
    150148    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    151149    public void StopJob(Guid jobId) {
    152       using (trans.OpenTransaction()) {
     150      trans.UseTransaction(() => {
    153151        throw new NotImplementedException();
    154       }
     152      });
    155153    }
    156154
     
    159157    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    160158    public void PauseJob(Guid jobId) {
    161       using (trans.OpenTransaction()) {
     159      trans.UseTransaction(() => {
    162160        throw new NotImplementedException();
    163       }
     161      });
    164162    }
    165163    #endregion
     
    189187    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    190188    public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
    191       using (trans.OpenTransaction()) {
     189      return trans.UseTransaction(() => {
    192190        hiveExperimentDto.OwnerUserId = auth.UserId;
    193191        hiveExperimentDto.DateCreated = DateTime.Now;
    194192        return dao.AddHiveExperiment(hiveExperimentDto);
    195       }
     193      });
    196194    }
    197195
     
    199197    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    200198    public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
    201       using (trans.OpenTransaction()) {
     199      trans.UseTransaction(() => {
    202200        dao.UpdateHiveExperiment(hiveExperimentDto);
    203       }
     201      });
    204202    }
    205203
     
    207205    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    208206    public void DeleteHiveExperiment(Guid hiveExperimentId) {
    209       using (trans.OpenTransaction()) {
     207      trans.UseTransaction(() => {
    210208        HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
    211209        dao.DeleteHiveExperiment(hiveExperimentId); // child jobs will be deleted by db-trigger
    212       }
     210      });
    213211    }
    214212    #endregion
     
    217215    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    218216    public void Hello(Slave slaveInfo) {
    219       using (trans.OpenTransaction()) {
     217      trans.UseTransaction(() => {
    220218        var slave = dao.GetSlave(slaveInfo.Id);
    221219
     
    225223          dao.UpdateSlave(slaveInfo);
    226224        }
    227       }
     225      });
    228226    }
    229227
    230228    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
    231229    public void GoodBye(Guid slaveId) {
    232       using (trans.OpenTransaction()) {
     230      trans.UseTransaction(() => {
    233231        var slave = dao.GetSlave(slaveId);
    234232        if (slave != null) {
     
    236234          dao.UpdateSlave(slave);
    237235        }
    238       }
     236      });
    239237    }
    240238    #endregion
     
    244242    public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
    245243      TriggerLifecycle(false);
    246 
    247       using (trans.OpenTransaction()) {
    248         return heartbeatManager.ProcessHeartbeat(heartbeat);
    249       }
     244      return trans.UseTransaction(() => heartbeatManager.ProcessHeartbeat(heartbeat));
    250245    }
    251246    #endregion
     
    255250    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    256251    public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
    257       using (trans.OpenTransaction()) {
     252      return trans.UseTransaction(() => {
    258253        plugin.UserId = auth.UserId;
    259254        plugin.DateCreated = DateTime.Now;
     
    264259        }
    265260        return pluginId;
    266       }
     261      });
    267262    }
    268263
     
    280275      List<PluginData> pluginDatas = new List<PluginData>();
    281276
    282       using (trans.OpenTransaction()) {
     277      return trans.UseTransaction(() => {
    283278        foreach (Guid guid in pluginIds) {
    284279          List<PluginData> pluginData = dao.GetPluginDatas(x => x.PluginId == guid).ToList();
     
    290285        }
    291286        return pluginDatas;
    292       }
     287      });
    293288    }
    294289
     
    299294    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    300295    public Guid AddSlave(Slave slave) {
    301       using (trans.OpenTransaction()) {
    302         return dao.AddSlave(slave);
    303       }
     296      return trans.UseTransaction(() => dao.AddSlave(slave));
    304297    }
    305298
     
    307300    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    308301    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
    309       using (trans.OpenTransaction()) {
    310         return dao.AddSlaveGroup(slaveGroup);
    311       }
     302      return trans.UseTransaction(() => dao.AddSlaveGroup(slaveGroup));
    312303    }
    313304
     
    337328    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    338329    public void UpdateSlave(Slave slave) {
    339       using (trans.OpenTransaction()) {
     330      trans.UseTransaction(() => {
    340331        dao.UpdateSlave(slave);
    341       }
     332      });
    342333    }
    343334
     
    345336    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    346337    public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
    347       using (trans.OpenTransaction()) {
     338      trans.UseTransaction(() => {
    348339        dao.UpdateSlaveGroup(slaveGroup);
    349       }
     340      });
    350341    }
    351342
     
    353344    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    354345    public void DeleteSlave(Guid slaveId) {
    355       using (trans.OpenTransaction()) {
     346      trans.UseTransaction(() => {
    356347        dao.DeleteSlave(slaveId);
    357       }
     348      });
    358349    }
    359350
     
    361352    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    362353    public void DeleteSlaveGroup(Guid slaveGroupId) {
    363       using (trans.OpenTransaction()) {
     354      trans.UseTransaction(() => {
    364355        dao.DeleteSlaveGroup(slaveGroupId);
    365       }
     356      });
    366357    }
    367358
     
    369360    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    370361    public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
    371       using (trans.OpenTransaction()) {
     362      trans.UseTransaction(() => {
    372363        var resource = dao.GetResource(resourceId);
    373364        resource.ParentResourceId = slaveGroupId;
    374365        dao.UpdateResource(resource);
    375       }
     366      });
    376367    }
    377368
     
    379370    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    380371    public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
    381       using (trans.OpenTransaction()) {
     372      trans.UseTransaction(() => {
    382373        var resource = dao.GetResource(resourceId);
    383374        resource.ParentResourceId = null;
    384375        dao.UpdateResource(resource);
    385       }
     376      });
    386377    }
    387378
     
    389380    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
    390381    public Guid GetResourceId(string resourceName) {
    391       using (trans.OpenTransaction()) {
     382      return trans.UseTransaction(() => {
    392383        var resource = dao.GetResources(x => x.Name == resourceName).FirstOrDefault();
    393384        if (resource != null) {
     
    396387          return Guid.Empty;
    397388        }
    398       }
     389      });
    399390    }
    400391
    401392    public void TriggerLifecycle(bool force) {
    402       using (trans.OpenTransaction()) {
     393      trans.UseTransaction(() => {
    403394        DateTime lastCleanup = dao.GetLastCleanup();
    404395        if (force || DateTime.Now - lastCleanup > TimeSpan.FromSeconds(59)) {
     
    406397          lifecycleManager.Cleanup();
    407398        }
    408       }
     399      });
    409400    }
    410401    #endregion
     
    433424    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
    434425    public Guid AddAppointment(Appointment appointment) {
    435       using (trans.OpenTransaction()) {
    436         return dao.AddAppointment(appointment);
    437       }
     426      return trans.UseTransaction(() => dao.AddAppointment(appointment));
    438427    }
    439428
    440429    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
    441430    public void DeleteAppointment(Guid appointmentId) {
    442       using (trans.OpenTransaction()) {
     431      trans.UseTransaction(() => {
    443432        dao.DeleteAppointment(appointmentId);
    444       }
     433      });
    445434    }
    446435
    447436    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
    448437    public void UpdateAppointment(Appointment appointment) {
    449       using (trans.OpenTransaction()) {
     438      trans.UseTransaction(() => {
    450439        dao.UpdateAppointment(appointment);
    451       }
     440      });
    452441    }
    453442
    454443    // [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
    455444    public IEnumerable<Appointment> GetScheduleForResource(Guid resourceId) {
    456       using (trans.OpenTransaction()) {
    457         return dao.GetAppointments(x => x.ResourceId == resourceId);
    458       }
     445      return trans.UseTransaction(() => dao.GetAppointments(x => x.ResourceId == resourceId));
    459446    }
    460447    #endregion
Note: See TracChangeset for help on using the changeset viewer.