Index: branches/OaaS/HeuristicLab.Services.Hive/3.3/HiveDao.cs
===================================================================
--- branches/OaaS/HeuristicLab.Services.Hive/3.3/HiveDao.cs	(revision 8228)
+++ branches/OaaS/HeuristicLab.Services.Hive/3.3/HiveDao.cs	(revision 8326)
@@ -25,30 +25,67 @@
 using System.Linq.Expressions;
 using DT = HeuristicLab.Services.Hive.DataTransfer;
+using System.ServiceModel;
+using HeuristicLab.Services.Hive.Interfaces;
+using System.Data.Common;
 
 namespace HeuristicLab.Services.Hive.DataAccess {
   public class HiveDao : IHiveDao {
-    public static HiveDataContext CreateContext(bool longRunning = false) {
-      var context = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
+    
+    public HiveDataContext CreateContext(bool longRunning = false) {
+      //var context = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
+      var context = new HiveDataContext(provider.GetOpenConnection(Settings.Default.HiveConnectionStringName));      
       if (longRunning) context.CommandTimeout = (int)Settings.Default.LongRunningDatabaseCommandTimeout.TotalSeconds;
       return context;
     }
 
-    public HiveDao() { }
+    private IConnectionProvider provider;
+
+    public HiveDao(IConnectionProvider provider) { this.provider = provider; }
+
+    private delegate T ExecuteWithContextDelegate<T>(HiveDataContext context);
+    private delegate void ExecuteWithContextDelegate(HiveDataContext context);
+
+    private void ExecuteWithContext(ExecuteWithContextDelegate call) {
+      DbConnection con = null;
+      try {
+        using (var db = CreateContext()) {
+          con = db.Connection;
+          call(db);
+        }
+      }
+      finally {
+        provider.ReleaseConnection(con);
+      }
+    }
+
+    private T ExecuteWithContext<T>(ExecuteWithContextDelegate<T> call) {
+      DbConnection con = null;
+      try {
+        using (var db = CreateContext()) {
+          con = db.Connection;
+          T result = call(db);
+          return result;
+        }
+      }
+      finally {
+        provider.ReleaseConnection(con);
+      }
+    }
 
     #region Task Methods
     public DT.Task GetTask(Guid id) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<DT.Task>((db) => {
         return DT.Convert.ToDto(db.Tasks.SingleOrDefault(x => x.TaskId == id));
-      }
+      });
     }
 
     public IEnumerable<DT.Task> GetTasks(Expression<Func<Task, bool>> predicate) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.Task>>((db) => {
         return db.Tasks.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
-      }
+      });
     }
 
     public Guid AddTask(DT.Task dto) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<Guid>((db) => {
         var entity = DT.Convert.ToEntity(dto);
         db.Tasks.InsertOnSubmit(entity);
@@ -59,9 +96,9 @@
         db.SubmitChanges();
         return entity.TaskId;
-      }
+      });
     }
 
     public void UpdateTask(DT.Task dto) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Tasks.FirstOrDefault(x => x.TaskId == dto.Id);
         if (entity == null) db.Tasks.InsertOnSubmit(DT.Convert.ToEntity(dto));
@@ -73,13 +110,13 @@
         }
         db.SubmitChanges();
-      }
+      });
     }
 
     public void DeleteTask(Guid id) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Tasks.FirstOrDefault(x => x.TaskId == id);
         if (entity != null) db.Tasks.DeleteOnSubmit(entity);
         db.SubmitChanges(); // taskData and child tasks are deleted by db-trigger
-      }
+      });
     }
 
@@ -92,5 +129,5 @@
     /// <returns></returns>
     public IEnumerable<DT.Task> GetParentTasks(IEnumerable<Guid> resourceIds, int count, bool finished) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.Task>>((db) => {
         var query = from ar in db.AssignedResources
                     where resourceIds.Contains(ar.ResourceId)
@@ -109,9 +146,9 @@
                     select DT.Convert.ToDto(ar.Task);
         return count == 0 ? query.ToArray() : query.Take(count).ToArray();
-      }
+      });
     }
 
     public IEnumerable<DT.Task> GetWaitingTasks(DT.Slave slave, int count) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.Task>>((db) => {
         var resourceIds = GetParentResources(slave.Id).Select(r => r.Id);
         //Originally we checked here if there are parent tasks which should be calculated (with GetParentTasks(resourceIds, count, false);).
@@ -129,9 +166,9 @@
         var waitingTasks = (count == 0 ? query : query.Take(count)).ToArray();
         return waitingTasks;
-      }
+      });
     }
 
     public DT.Task UpdateTaskState(Guid taskId, TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<DT.Task>((db) => {
         var job = db.Tasks.SingleOrDefault(x => x.TaskId == taskId);
         job.State = taskState;
@@ -147,5 +184,5 @@
         job = db.Tasks.SingleOrDefault(x => x.TaskId == taskId);
         return DT.Convert.ToDto(job);
-      }
+      });
     }
     #endregion
@@ -153,39 +190,39 @@
     #region TaskData Methods
     public DT.TaskData GetTaskData(Guid id) {
-      using (var db = CreateContext(true)) {
+      return ExecuteWithContext<DT.TaskData>((db) => {
         return DT.Convert.ToDto(db.TaskDatas.SingleOrDefault(x => x.TaskId == id));
-      }
+      });
     }
 
     public IEnumerable<DT.TaskData> GetTaskDatas(Expression<Func<TaskData, bool>> predicate) {
-      using (var db = CreateContext(true)) {
+      return ExecuteWithContext<IEnumerable<DT.TaskData>>((db) => {
         return db.TaskDatas.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
-      }
+      });
     }
 
     public Guid AddTaskData(DT.TaskData dto) {
-      using (var db = CreateContext(true)) {
+      return ExecuteWithContext<Guid>((db) => {
         var entity = DT.Convert.ToEntity(dto);
         db.TaskDatas.InsertOnSubmit(entity);
         db.SubmitChanges();
         return entity.TaskId;
-      }
+      });
     }
 
     public void UpdateTaskData(DT.TaskData dto) {
-      using (var db = CreateContext(true)) {
+      ExecuteWithContext((db) => {
         var entity = db.TaskDatas.FirstOrDefault(x => x.TaskId == dto.TaskId);
         if (entity == null) db.TaskDatas.InsertOnSubmit(DT.Convert.ToEntity(dto));
         else DT.Convert.ToEntity(dto, entity);
         db.SubmitChanges();
-      }
+      });
     }
 
     public void DeleteTaskData(Guid id) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.TaskDatas.FirstOrDefault(x => x.TaskId == id); // check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
         if (entity != null) db.TaskDatas.DeleteOnSubmit(entity);
         db.SubmitChanges();
-      }
+      });
     }
     #endregion
@@ -193,39 +230,39 @@
     #region StateLog Methods
     public DT.StateLog GetStateLog(Guid id) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<DT.StateLog>((db) => {
         return DT.Convert.ToDto(db.StateLogs.SingleOrDefault(x => x.StateLogId == id));
-      }
+      });
     }
 
     public IEnumerable<DT.StateLog> GetStateLogs(Expression<Func<StateLog, bool>> predicate) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.StateLog>>((db) => {
         return db.StateLogs.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
-      }
+      });
     }
 
     public Guid AddStateLog(DT.StateLog dto) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<Guid>((db) => {
         var entity = DT.Convert.ToEntity(dto);
         db.StateLogs.InsertOnSubmit(entity);
         db.SubmitChanges();
         return entity.StateLogId;
-      }
+      });
     }
 
     public void UpdateStateLog(DT.StateLog dto) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.StateLogs.FirstOrDefault(x => x.StateLogId == dto.Id);
         if (entity == null) db.StateLogs.InsertOnSubmit(DT.Convert.ToEntity(dto));
         else DT.Convert.ToEntity(dto, entity);
         db.SubmitChanges();
-      }
+      });
     }
 
     public void DeleteStateLog(Guid id) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.StateLogs.FirstOrDefault(x => x.StateLogId == id);
         if (entity != null) db.StateLogs.DeleteOnSubmit(entity);
         db.SubmitChanges();
-      }
+      });
     }
     #endregion
@@ -233,7 +270,7 @@
     #region Job Methods
     public DT.Job GetJob(Guid id) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<DT.Job>((db) => {
         return AddStatsToJob(db, DT.Convert.ToDto(db.Jobs.SingleOrDefault(x => x.JobId == id)));
-      }
+      });
     }
 
@@ -250,33 +287,33 @@
 
     public IEnumerable<DT.Job> GetJobs(Expression<Func<Job, bool>> predicate) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.Job>>((db) => {
         return db.Jobs.Where(predicate).Select(x => AddStatsToJob(db, DT.Convert.ToDto(x))).ToArray();
-      }
+      });
     }
 
     public Guid AddJob(DT.Job dto) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<Guid>((db) => {
         var entity = DT.Convert.ToEntity(dto);
         db.Jobs.InsertOnSubmit(entity);
         db.SubmitChanges();
         return entity.JobId;
-      }
+      });
     }
 
     public void UpdateJob(DT.Job dto) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Jobs.FirstOrDefault(x => x.JobId == dto.Id);
         if (entity == null) db.Jobs.InsertOnSubmit(DT.Convert.ToEntity(dto));
         else DT.Convert.ToEntity(dto, entity);
         db.SubmitChanges();
-      }
+      });
     }
 
     public void DeleteJob(Guid id) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Jobs.FirstOrDefault(x => x.JobId == id);
         if (entity != null) db.Jobs.DeleteOnSubmit(entity);
         db.SubmitChanges();
-      }
+      });
     }
     #endregion
@@ -284,38 +321,38 @@
     #region JobPermission Methods
     public DT.JobPermission GetJobPermission(Guid jobId, Guid grantedUserId) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<DT.JobPermission>((db) => {
         return DT.Convert.ToDto(db.JobPermissions.SingleOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId));
-      }
+      });
     }
 
     public IEnumerable<DT.JobPermission> GetJobPermissions(Expression<Func<JobPermission, bool>> predicate) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.JobPermission>>((db) => {
         return db.JobPermissions.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
-      }
+      });
     }
 
     public void AddJobPermission(DT.JobPermission dto) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = DT.Convert.ToEntity(dto);
         db.JobPermissions.InsertOnSubmit(entity);
         db.SubmitChanges();
-      }
+      });
     }
 
     public void UpdateJobPermission(DT.JobPermission dto) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.JobPermissions.FirstOrDefault(x => x.JobId == dto.JobId && x.GrantedUserId == dto.GrantedUserId);
         if (entity == null) db.JobPermissions.InsertOnSubmit(DT.Convert.ToEntity(dto));
         else DT.Convert.ToEntity(dto, entity);
         db.SubmitChanges();
-      }
+      });
     }
 
     public void DeleteJobPermission(Guid jobId, Guid grantedUserId) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.JobPermissions.FirstOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId);
         if (entity != null) db.JobPermissions.DeleteOnSubmit(entity);
         db.SubmitChanges();
-      }
+      });
     }
 
@@ -324,5 +361,5 @@
     /// </summary>
     public void SetJobPermission(Guid jobId, Guid grantedByUserId, Guid grantedUserId, Permission permission) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         JobPermission jobPermission = db.JobPermissions.SingleOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId);
         if (jobPermission != null) {
@@ -330,10 +367,12 @@
             // not allowed, delete
             db.JobPermissions.DeleteOnSubmit(jobPermission);
-          } else {
+          }
+          else {
             // update
             jobPermission.Permission = permission;
             jobPermission.GrantedByUserId = grantedByUserId; // update grantedByUserId, always the last "granter" is stored
           }
-        } else {
+        }
+        else {
           // insert
           if (permission != Permission.NotAllowed) {
@@ -343,5 +382,5 @@
         }
         db.SubmitChanges();
-      }
+      });
     }
     #endregion
@@ -349,39 +388,39 @@
     #region Plugin Methods
     public DT.Plugin GetPlugin(Guid id) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<DT.Plugin>((db) => {
         return DT.Convert.ToDto(db.Plugins.SingleOrDefault(x => x.PluginId == id));
-      }
+      });
     }
 
     public IEnumerable<DT.Plugin> GetPlugins(Expression<Func<Plugin, bool>> predicate) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.Plugin>>((db) => {
         return db.Plugins.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
-      }
+      });
     }
 
     public Guid AddPlugin(DT.Plugin dto) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<Guid>((db) => {
         var entity = DT.Convert.ToEntity(dto);
         db.Plugins.InsertOnSubmit(entity);
         db.SubmitChanges();
         return entity.PluginId;
-      }
+      });
     }
 
     public void UpdatePlugin(DT.Plugin dto) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Plugins.FirstOrDefault(x => x.PluginId == dto.Id);
         if (entity == null) db.Plugins.InsertOnSubmit(DT.Convert.ToEntity(dto));
         else DT.Convert.ToEntity(dto, entity);
         db.SubmitChanges();
-      }
+      });
     }
 
     public void DeletePlugin(Guid id) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Plugins.FirstOrDefault(x => x.PluginId == id);
         if (entity != null) db.Plugins.DeleteOnSubmit(entity);
         db.SubmitChanges();
-      }
+      });
     }
     #endregion
@@ -389,39 +428,39 @@
     #region PluginData Methods
     public DT.PluginData GetPluginData(Guid id) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<DT.PluginData>((db) => {
         return DT.Convert.ToDto(db.PluginDatas.SingleOrDefault(x => x.PluginDataId == id));
-      }
+      });
     }
 
     public IEnumerable<DT.PluginData> GetPluginDatas(Expression<Func<PluginData, bool>> predicate) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext <IEnumerable<DT.PluginData>>((db) => {
         return db.PluginDatas.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
-      }
+      });
     }
 
     public Guid AddPluginData(DT.PluginData dto) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<Guid>((db) => {
         var entity = DT.Convert.ToEntity(dto);
         db.PluginDatas.InsertOnSubmit(entity);
         db.SubmitChanges();
         return entity.PluginDataId;
-      }
+      });
     }
 
     public void UpdatePluginData(DT.PluginData dto) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.PluginDatas.FirstOrDefault(x => x.PluginId == dto.PluginId);
         if (entity == null) db.PluginDatas.InsertOnSubmit(DT.Convert.ToEntity(dto));
         else DT.Convert.ToEntity(dto, entity);
         db.SubmitChanges();
-      }
+      });
     }
 
     public void DeletePluginData(Guid id) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.PluginDatas.FirstOrDefault(x => x.PluginDataId == id);
         if (entity != null) db.PluginDatas.DeleteOnSubmit(entity);
         db.SubmitChanges();
-      }
+      });
     }
     #endregion
@@ -429,39 +468,39 @@
     #region Slave Methods
     public DT.Slave GetSlave(Guid id) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<DT.Slave>((db) => {
         return DT.Convert.ToDto(db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id));
-      }
+      });
     }
 
     public IEnumerable<DT.Slave> GetSlaves(Expression<Func<Slave, bool>> predicate) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.Slave>>((db) => {
         return db.Resources.OfType<Slave>().Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
-      }
+      });
     }
 
     public Guid AddSlave(DT.Slave dto) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<Guid>((db) => {
         var entity = DT.Convert.ToEntity(dto);
         db.Resources.InsertOnSubmit(entity);
         db.SubmitChanges();
         return entity.ResourceId;
-      }
+      });
     }
 
     public void UpdateSlave(DT.Slave dto) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == dto.Id);
         if (entity == null) db.Resources.InsertOnSubmit(DT.Convert.ToEntity(dto));
         else DT.Convert.ToEntity(dto, entity);
         db.SubmitChanges();
-      }
+      });
     }
 
     public void DeleteSlave(Guid id) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == id);
         if (entity != null) db.Resources.DeleteOnSubmit(entity);
         db.SubmitChanges();
-      }
+      });
     }
     #endregion
@@ -469,17 +508,17 @@
     #region SlaveGroup Methods
     public DT.SlaveGroup GetSlaveGroup(Guid id) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<DT.SlaveGroup>((db) => {
         return DT.Convert.ToDto(db.Resources.OfType<SlaveGroup>().SingleOrDefault(x => x.ResourceId == id));
-      }
+      });
     }
 
     public IEnumerable<DT.SlaveGroup> GetSlaveGroups(Expression<Func<SlaveGroup, bool>> predicate) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.SlaveGroup>>((db) => {
         return db.Resources.OfType<SlaveGroup>().Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
-      }
+      });
     }
 
     public Guid AddSlaveGroup(DT.SlaveGroup dto) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<Guid>((db) => {
         if (dto.Id == Guid.Empty)
           dto.Id = Guid.NewGuid();
@@ -488,18 +527,18 @@
         db.SubmitChanges();
         return entity.ResourceId;
-      }
+      });
     }
 
     public void UpdateSlaveGroup(DT.SlaveGroup dto) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == dto.Id);
         if (entity == null) db.Resources.InsertOnSubmit(DT.Convert.ToEntity(dto));
         else DT.Convert.ToEntity(dto, entity);
         db.SubmitChanges();
-      }
+      });
     }
 
     public void DeleteSlaveGroup(Guid id) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == id);
         if (entity != null) {
@@ -510,5 +549,5 @@
         }
         db.SubmitChanges();
-      }
+      });
     }
     #endregion
@@ -516,54 +555,54 @@
     #region Resource Methods
     public DT.Resource GetResource(Guid id) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<DT.Resource>((db) => {
         return DT.Convert.ToDto(db.Resources.SingleOrDefault(x => x.ResourceId == id));
-      }
+      });
     }
 
     public IEnumerable<DT.Resource> GetResources(Expression<Func<Resource, bool>> predicate) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.Resource>>((db) => {
         return db.Resources.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
-      }
+      });
     }
 
     public Guid AddResource(DT.Resource dto) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<Guid>((db) => {
         var entity = DT.Convert.ToEntity(dto);
         db.Resources.InsertOnSubmit(entity);
         db.SubmitChanges();
         return entity.ResourceId;
-      }
+      });
     }
 
     public void UpdateResource(DT.Resource dto) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Resources.FirstOrDefault(x => x.ResourceId == dto.Id);
         if (entity == null) db.Resources.InsertOnSubmit(DT.Convert.ToEntity(dto));
         else DT.Convert.ToEntity(dto, entity);
         db.SubmitChanges();
-      }
+      });
     }
 
     public void DeleteResource(Guid id) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Resources.FirstOrDefault(x => x.ResourceId == id);
         if (entity != null) db.Resources.DeleteOnSubmit(entity);
         db.SubmitChanges();
-      }
+      });
     }
 
     public void AssignJobToResource(Guid jobId, Guid resourceId) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var job = db.Tasks.Where(x => x.TaskId == jobId).Single();
         job.AssignedResources.Add(new AssignedResource() { TaskId = jobId, ResourceId = resourceId });
         db.SubmitChanges();
-      }
+      });
     }
 
     public IEnumerable<DT.Resource> GetAssignedResources(Guid jobId) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext <IEnumerable<DT.Resource>>((db) => {
         var job = db.Tasks.Where(x => x.TaskId == jobId).Single();
         return job.AssignedResources.Select(x => DT.Convert.ToDto(x.Resource)).ToArray();
-      }
+      });
     }
 
@@ -572,9 +611,9 @@
     /// </summary>
     public IEnumerable<DT.Resource> GetParentResources(Guid resourceId) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.Resource>>((db) => {
         var resources = new List<Resource>();
         CollectParentResources(resources, db.Resources.Where(r => r.ResourceId == resourceId).Single());
         return resources.Select(r => DT.Convert.ToDto(r)).ToArray();
-      }
+      });
     }
 
@@ -589,5 +628,5 @@
     /// </summary>
     public IEnumerable<DT.Resource> GetChildResources(Guid resourceId) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.Resource>>((db) => {
         var childs = new List<DT.Resource>();
         foreach (var child in db.Resources.Where(x => x.ParentResourceId == resourceId)) {
@@ -596,9 +635,9 @@
         }
         return childs;
-      }
+      });
     }
 
     public IEnumerable<DT.Task> GetJobsByResourceId(Guid resourceId) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.Task>>((db) => {
         var resources = GetChildResources(resourceId).Select(x => x.Id).ToList();
         resources.Add(resourceId);
@@ -609,5 +648,5 @@
           resources.Contains(j.StateLogs.OrderByDescending(x => x.DateTime).First().SlaveId.Value));
         return jobs.Select(j => DT.Convert.ToDto(j)).ToArray();
-      }
+      });
     }
     #endregion
@@ -615,37 +654,37 @@
     #region ResourcePermission Methods
     public DT.ResourcePermission GetResourcePermission(Guid resourceId, Guid grantedUserId) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext <DT.ResourcePermission> ((db) => {
         return DT.Convert.ToDto(db.ResourcePermissions.SingleOrDefault(x => x.ResourceId == resourceId && x.GrantedUserId == grantedUserId));
-      }
+      });
     }
 
     public IEnumerable<DT.ResourcePermission> GetResourcePermissions(Expression<Func<ResourcePermission, bool>> predicate) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.ResourcePermission>>((db) => {
         return db.ResourcePermissions.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
-      }
+      });
     }
 
     public void AddResourcePermission(DT.ResourcePermission dto) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.ResourcePermissions.SingleOrDefault(x => x.ResourceId == dto.ResourceId && x.GrantedUserId == dto.GrantedUserId);
         if (entity == null) { db.ResourcePermissions.InsertOnSubmit(DT.Convert.ToEntity(dto)); db.SubmitChanges(); }
-      }
+      });
     }
 
     public void UpdateResourcePermission(DT.ResourcePermission dto) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.ResourcePermissions.FirstOrDefault(x => x.ResourceId == dto.ResourceId && x.GrantedUserId == dto.GrantedUserId);
         if (entity == null) db.ResourcePermissions.InsertOnSubmit(DT.Convert.ToEntity(dto));
         else DT.Convert.ToEntity(dto, entity);
         db.SubmitChanges();
-      }
+      });
     }
 
     public void DeleteResourcePermission(Guid resourceId, Guid grantedUserId) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.ResourcePermissions.FirstOrDefault(x => x.ResourceId == resourceId && x.GrantedUserId == grantedUserId);
         if (entity != null) db.ResourcePermissions.DeleteOnSubmit(entity);
         db.SubmitChanges();
-      }
+      });
     }
     #endregion
@@ -653,11 +692,11 @@
     #region Authorization Methods
     public Permission GetPermissionForTask(Guid taskId, Guid userId) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<Permission>((db) => {
         return GetPermissionForJob(GetJobForTask(taskId), userId);
-      }
+      });
     }
 
     public Permission GetPermissionForJob(Guid jobId, Guid userId) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<Permission>((db) => {
         Job job = db.Jobs.SingleOrDefault(x => x.JobId == jobId);
         if (job == null) return Permission.NotAllowed;
@@ -665,11 +704,11 @@
         JobPermission permission = db.JobPermissions.SingleOrDefault(p => p.JobId == jobId && p.GrantedUserId == userId);
         return permission != null ? permission.Permission : Permission.NotAllowed;
-      }
+      });
     }
 
     public Guid GetJobForTask(Guid taskId) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<Guid>((db) => {
         return db.Tasks.Single(j => j.TaskId == taskId).JobId;
-      }
+      });
     }
     #endregion
@@ -677,16 +716,17 @@
     #region Lifecycle Methods
     public DateTime GetLastCleanup() {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<DateTime>((db) => {
         var entity = db.Lifecycles.SingleOrDefault();
         return entity != null ? entity.LastCleanup : DateTime.MinValue;
-      }
+      });
     }
 
     public void SetLastCleanup(DateTime datetime) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Lifecycles.SingleOrDefault();
         if (entity != null) {
           entity.LastCleanup = datetime;
-        } else {
+        }
+        else {
           entity = new Lifecycle();
           entity.LifecycleId = 0; // always only one entry with ID:0
@@ -695,5 +735,5 @@
         }
         db.SubmitChanges();
-      }
+      });
     }
     #endregion
@@ -701,39 +741,39 @@
     #region Downtime Methods
     public DT.Downtime GetDowntime(Guid id) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<DT.Downtime>((db) => {
         return DT.Convert.ToDto(db.Downtimes.SingleOrDefault(x => x.DowntimeId == id));
-      }
+      });
     }
 
     public IEnumerable<DT.Downtime> GetDowntimes(Expression<Func<Downtime, bool>> predicate) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.Downtime>>((db) => {
         return db.Downtimes.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
-      }
+      });
     }
 
     public Guid AddDowntime(DT.Downtime dto) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<Guid>((db) => {
         var entity = DT.Convert.ToEntity(dto);
         db.Downtimes.InsertOnSubmit(entity);
         db.SubmitChanges();
         return entity.DowntimeId;
-      }
+      });
     }
 
     public void UpdateDowntime(DT.Downtime dto) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Downtimes.FirstOrDefault(x => x.DowntimeId == dto.Id);
         if (entity == null) db.Downtimes.InsertOnSubmit(DT.Convert.ToEntity(dto));
         else DT.Convert.ToEntity(dto, entity);
         db.SubmitChanges();
-      }
+      });
     }
 
     public void DeleteDowntime(Guid id) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Downtimes.FirstOrDefault(x => x.DowntimeId == id);
         if (entity != null) db.Downtimes.DeleteOnSubmit(entity);
         db.SubmitChanges();
-      }
+      });
     }
     #endregion
@@ -741,17 +781,17 @@
     #region Statistics Methods
     public DT.Statistics GetStatistic(Guid id) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<DT.Statistics>((db) => {
         return DT.Convert.ToDto(db.Statistics.SingleOrDefault(x => x.StatisticsId == id));
-      }
+      });
     }
 
     public IEnumerable<DT.Statistics> GetStatistics(Expression<Func<Statistics, bool>> predicate) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<IEnumerable<DT.Statistics>>((db) => {
         return db.Statistics.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
-      }
+      });
     }
 
     public Guid AddStatistics(DT.Statistics dto) {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<Guid>((db) => {
         var entity = DT.Convert.ToEntity(dto);
         db.Statistics.InsertOnSubmit(entity);
@@ -769,17 +809,17 @@
         db.SubmitChanges();
         return entity.StatisticsId;
-      }
+      });
     }
 
     public void DeleteStatistics(Guid id) {
-      using (var db = CreateContext()) {
+      ExecuteWithContext((db) => {
         var entity = db.Statistics.FirstOrDefault(x => x.StatisticsId == id);
         if (entity != null) db.Statistics.DeleteOnSubmit(entity);
         db.SubmitChanges();
-      }
+      });
     }
 
     public List<DT.UserStatistics> GetUserStatistics() {
-      using (var db = CreateContext()) {
+      return ExecuteWithContext<List<DT.UserStatistics>>((db) => {
         var userStats = new Dictionary<Guid, DT.UserStatistics>();
 
@@ -853,5 +893,5 @@
 
         return userStats.Values.ToList();
-      }
+      });
     }
     #endregion
