Index: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/ContextFactory.cs
===================================================================
--- branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/ContextFactory.cs	(revision 4629)
+++ 	(revision )
@@ -1,139 +1,0 @@
-﻿#region License Information
-/* HeuristicLab
- * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
- *
- * This file is part of HeuristicLab.
- *
- * HeuristicLab is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * HeuristicLab is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
- */
-#endregion
-
-using System;
-using System.Collections.Generic;
-using System.Data.Linq;
-using System.Threading;
-using System.Transactions;
-using HeuristicLab.Tracing;
-using HeuristicLab.Services.Hive.Common;
-using HeuristicLab.Services.Hive.DataAccess.Properties;
-
-
-namespace HeuristicLab.Services.Hive.DataAccess {
-  public delegate T CreateDataContext<T>() where T : DataContext;
-
-  /// <summary>
-  /// This class handles creates one Context for each Thread which asks for one.
-  /// If one Thread calls GetContext several times, it always gets the same Context object.
-  /// A context object is removed from the cache when it is disposed. 
-  /// 
-  /// Every context has a DB-Transaction over its livetime. After using the context object
-  /// it has to be disposed in order to finish the corresponding transaction.
-  /// </summary>
-  public class ContextFactory<T> : IContextFactory<T> where T : DataContext {
-    private static object locker = new object();
-    private static IDictionary<int, Disposable<T>> contexts = new Dictionary<int, Disposable<T>>();
-    private static IDictionary<int, TransactionScope> transactions = new Dictionary<int, TransactionScope>();
-    private CreateDataContext<T> createDataContext;
-
-    public ContextFactory(CreateDataContext<T> createDataContext) {
-      this.createDataContext = createDataContext;
-    }
-
-    public IDisposable GetContext() {
-      return GetContext(true);
-    }
-
-    public IDisposable GetContext(bool withTransaction) {
-      lock (locker) {
-        int threadId = Thread.CurrentThread.ManagedThreadId;
-
-        if (contexts.ContainsKey(threadId)) {
-          Logger.Error("ERROR: Context for this Thread already defined. Will dispose it and then create a new one.");
-          RemoveContext();
-          RemoveAndCompleteTransaction();
-        }
-
-        T context = createDataContext();
-       
-        Disposable<T> disposableContext = new Disposable<T>(context);
-        disposableContext.OnDisposing += new EventHandler(context_OnDisposing);
-        contexts.Add(threadId, disposableContext);
-
-        if (withTransaction) {
-          TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE });
-          transactions.Add(threadId, transaction);
-        }
-        return disposableContext;
-      }
-    }
-
-    void context_OnDisposing(object sender, EventArgs e) {
-      lock (locker) {
-        RemoveContext();
-        RemoveAndCompleteTransaction();
-      }
-    }
-
-    public void RollbackTransaction() {
-      int threadId = Thread.CurrentThread.ManagedThreadId;
-
-      TransactionScope transaction = transactions[threadId];
-      transaction.Dispose();
-    }
-
-    public void RemoveContext() {
-      lock (locker) {
-        int threadId = Thread.CurrentThread.ManagedThreadId;
-        contexts[threadId].Obj.Dispose();
-        contexts.Remove(threadId);
-        // context gets disposed implicitly, when it is used as IDisposable
-      }
-    }
-
-    public void RemoveAndCompleteTransaction() {
-      lock (locker) {
-        int threadId = Thread.CurrentThread.ManagedThreadId;
-
-        // context does not always have an associated transaction
-        if (transactions.ContainsKey(threadId)) {
-          try {
-            transactions[threadId].Complete();
-            transactions[threadId].Dispose();
-          }
-          catch (Exception) { }
-          finally {
-            transactions.Remove(threadId);
-          }
-        }
-      }
-    }
-
-    public T CurrentContext {
-      get {
-        lock (locker) {
-          int threadId = Thread.CurrentThread.ManagedThreadId;
-          if (contexts.ContainsKey(threadId)) {
-            return contexts[threadId].Obj;
-          } else {
-            return default(T);
-          }
-        }
-      }
-    }
-
-    public static HiveDataContext CreateHiveContext() {
-      return new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
-    }
-  }
-}
Index: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/Convert.cs
===================================================================
--- branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/Convert.cs	(revision 4629)
+++ branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/Convert.cs	(revision 4649)
@@ -24,21 +24,49 @@
 using System.Linq;
 using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
+using System;
 
 namespace HeuristicLab.Services.Hive.DataAccess {
   public static class Convert {
-
-   
     #region Job
     public static DT.Job ToDto(Job source) {
       if (source == null) return null;
-      return new DT.Job { Id = source.JobId };// todo
+      return new DT.Job {
+        Id = source.JobId,
+        CoresNeeded = source.CoresNeeded,
+        DateCalculated = source.DateCalculated,
+        DateCreated = source.DateCreated,
+        DateFinished = source.DateFinished,
+        Exception = source.Exception,
+        ExecutionTime = source.ExecutionTime,
+        MemoryNeeded = source.MemoryNeeded,
+        ParentJobId = source.ParentJobId,
+        Priority = source.Priority,
+        ProjectId = source.ProjectId,
+        SlaveId = source.SlaveId,
+        JobState = source.JobState,
+        UserId = source.UserId
+      };
     }
     public static Job ToEntity(DT.Job source) {
       if (source == null) return null;
-      return new Job { JobId = source.Id };// todo
+      var entity = new Job(); ToEntity(source, entity);
+      return entity;
     }
     public static void ToEntity(DT.Job source, Job target) {
       if ((source != null) && (target != null)) {
-        target.JobId = source.Id;// todo
+        target.JobId = source.Id;
+        target.CoresNeeded = source.CoresNeeded;
+        target.DateCalculated = source.DateCalculated;
+        target.DateCreated = source.DateCreated;
+        target.DateFinished = source.DateFinished;
+        target.Exception = source.Exception;
+        target.ExecutionTime = source.ExecutionTime;
+        target.MemoryNeeded = source.MemoryNeeded;
+        target.ParentJobId = source.ParentJobId;
+        target.Priority = source.Priority;
+        target.ProjectId = source.ProjectId;
+        target.SlaveId = source.SlaveId;
+        target.JobState = source.JobState;
+        target.UserId = source.UserId;
       }
     }
@@ -48,29 +76,31 @@
     public static DT.JobData ToDto(JobData source) {
       if (source == null) return null;
-      return new DT.JobData { JobId = source.JobId };// todo
+      return new DT.JobData { JobId = source.JobId, Data = source.Data.ToArray() };
     }
     public static JobData ToEntity(DT.JobData source) {
       if (source == null) return null;
-      return new JobData { JobId = source.Id };// todo
+      var entity = new JobData(); ToEntity(source, entity);
+      return entity;
     }
     public static void ToEntity(DT.JobData source, JobData target) {
       if ((source != null) && (target != null)) {
-        target.JobId = source.Id;// todo
+        target.JobId = source.JobId; target.Data = new Binary(source.Data);
       }
     }
     #endregion
-    
+
     #region HiveExperiment
     public static DT.HiveExperiment ToDto(HiveExperiment source) {
       if (source == null) return null;
-      return new DT.HiveExperiment { Id = source.HiveExperimentId };// todo
+      return new DT.HiveExperiment { Id = source.HiveExperimentId, Description = source.Description, Name = source.Name, RootJobId = source.RootJobId, UserId = source.UserId };
     }
     public static HiveExperiment ToEntity(DT.HiveExperiment source) {
       if (source == null) return null;
-      return new HiveExperiment { HiveExperimentId = source.Id };// todo
+      var entity = new HiveExperiment(); ToEntity(source, entity);
+      return entity;
     }
     public static void ToEntity(DT.HiveExperiment source, HiveExperiment target) {
       if ((source != null) && (target != null)) {
-        target.HiveExperimentId = source.Id;// todo
+        target.HiveExperimentId = source.Id; target.Description = source.Description; target.Name = source.Name; target.RootJobId = source.RootJobId; target.UserId = source.UserId;
       }
     }
@@ -80,13 +110,14 @@
     public static DT.Slave ToDto(Slave source) {
       if (source == null) return null;
-      return new DT.Slave { Id = source.ResourceId };// todo
+      return new DT.Slave { Id = source.ResourceId, CalendarSyncState = source.CalendarSyncState, Cores = source.Cores, CpuSpeed = source.CpuSpeed, FreeCores = source.FreeCores, FreeMemory = source.FreeMemory, IsAllowedToCalculate = source.IsAllowedToCalculate, Memory = source.Memory, Name = source.Name, SlaveConfigId = source.SlaveConfigId, SlaveState = source.SlaveState };
     }
     public static Slave ToEntity(DT.Slave source) {
       if (source == null) return null;
-      return new Slave { ResourceId = source.Id };// todo
+      var entity = new Slave(); ToEntity(source, entity);
+      return entity;
     }
     public static void ToEntity(DT.Slave source, Slave target) {
       if ((source != null) && (target != null)) {
-        target.ResourceId = source.Id;// todo
+        target.ResourceId = source.Id; target.CalendarSyncState = source.CalendarSyncState; target.Cores = source.Cores; target.CpuSpeed = source.CpuSpeed; target.FreeCores = source.FreeCores; target.FreeMemory = source.FreeMemory; target.IsAllowedToCalculate = source.IsAllowedToCalculate; target.Memory = source.Memory; target.Name = source.Name; target.SlaveConfigId = source.SlaveConfigId; target.SlaveState = source.SlaveState;
       }
     }
@@ -96,13 +127,17 @@
     public static DT.SlaveGroup ToDto(SlaveGroup source) {
       if (source == null) return null;
-      return new DT.SlaveGroup { Id = source.ResourceId };// todo
+      return new DT.SlaveGroup { Id = source.ResourceId, Name = source.Name, ChildResourcesIds = source.SlaveGroup_Resources_Children.Select(x => x.ResourceId).ToList(), ParentResourcesIds = source.SlaveGroup_Resources_Parents.Select(x => x.ResourceId).ToList() };
     }
     public static SlaveGroup ToEntity(DT.SlaveGroup source) {
       if (source == null) return null;
-      return new SlaveGroup { ResourceId = source.Id };// todo
+      var entity = new SlaveGroup(); ToEntity(source, entity);
+      return entity;
     }
     public static void ToEntity(DT.SlaveGroup source, SlaveGroup target) {
       if ((source != null) && (target != null)) {
-        target.ResourceId = source.Id;// todo
+        target.ResourceId = source.Id;
+        target.Name = source.Name; 
+        //target.SlaveGroup_Resources_Children = // TODO: see how working with groups will work, then decide how to handle enumerations in here
+        //target.SlaveGroup_Resources_Parents = 
       }
     }
@@ -112,13 +147,14 @@
     public static DT.Resource ToDto(Resource source) {
       if (source == null) return null;
-      return new DT.Resource { Id = source.ResourceId };// todo
+      return new DT.Resource { Id = source.ResourceId, Name = source.Name };
     }
     public static Resource ToEntity(DT.Resource source) {
       if (source == null) return null;
-      return new Resource { ResourceId = source.Id };// todo
+      var entity = new Resource(); ToEntity(source, entity);
+      return entity;
     }
     public static void ToEntity(DT.Resource source, Resource target) {
       if ((source != null) && (target != null)) {
-        target.ResourceId = source.Id;// todo
+        target.ResourceId = source.Id; target.Name = source.Name;
       }
     }
Index: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/HeuristicLab.Services.Hive.DataAccess-3.3.csproj
===================================================================
--- branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/HeuristicLab.Services.Hive.DataAccess-3.3.csproj	(revision 4629)
+++ branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/HeuristicLab.Services.Hive.DataAccess-3.3.csproj	(revision 4649)
@@ -61,5 +61,4 @@
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="ContextFactory.cs" />
     <Compile Include="Convert.cs" />
     <Compile Include="HeuristicLabServicesHiveDataAccessPlugin.cs" />
@@ -82,4 +81,5 @@
     </Compile>
     <Compile Include="Tools\CreateHiveDatabaseApplication.cs" />
+    <Compile Include="TransactionManager.cs" />
   </ItemGroup>
   <ItemGroup>
Index: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/HiveDao.cs
===================================================================
--- branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/HiveDao.cs	(revision 4629)
+++ branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/HiveDao.cs	(revision 4649)
@@ -8,63 +8,78 @@
   using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
   using HeuristicLab.Services.Hive.Common.DataTransfer;
+  using HeuristicLab.Services.Hive.DataAccess.Properties;
 
   public class HiveDao : IHiveDao {
-    private IContextFactory<HiveDataContext> contextFactory;
-    private DataAccess.HiveDataContext db { get { return contextFactory.CurrentContext; } }
-
-    public HiveDao(IContextFactory<HiveDataContext> contextFactory) {
-      this.contextFactory = contextFactory;
+    public HiveDataContext CreateContext() {
+      return new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
+    }
+
+    public HiveDao() {
     }
 
     #region Job Methods
     public DT.Job GetJob(Guid id) {
-      return Convert.ToDto(db.Jobs.SingleOrDefault(x => x.JobId == id));
+      using (var db = CreateContext()) {
+        return Convert.ToDto(db.Jobs.SingleOrDefault(x => x.JobId == id));
+      }
     }
 
     public IEnumerable<DT.Job> GetJobs(Expression<Func<Job, bool>> predicate) {
-      return db.Jobs.Where(predicate).Select(x => Convert.ToDto(x));
+      using (var db = CreateContext()) {
+        return db.Jobs.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
+      }
     }
 
     public Guid AddJob(DT.Job dto) {
-      var entity = Convert.ToEntity(dto);
-      db.Jobs.InsertOnSubmit(entity);
-      db.SubmitChanges();
-      return entity.JobId;
+      using (var db = CreateContext()) {
+        var entity = Convert.ToEntity(dto);
+        db.Jobs.InsertOnSubmit(entity);
+        db.SubmitChanges();
+        return entity.JobId;
+      }
     }
 
     public void UpdateJob(DT.Job dto) {
-      var entity = db.Jobs.FirstOrDefault(x => x.JobId == dto.Id);
-      if (entity == null) db.Jobs.InsertOnSubmit(Convert.ToEntity(dto));
-      else Convert.ToEntity(dto, entity);
-      db.SubmitChanges();
+      using (var db = CreateContext()) {
+        var entity = db.Jobs.FirstOrDefault(x => x.JobId == dto.Id);
+        if (entity == null) db.Jobs.InsertOnSubmit(Convert.ToEntity(dto));
+        else Convert.ToEntity(dto, entity);
+        db.SubmitChanges();
+      }
     }
 
     public void DeleteJob(Guid id) {
-      var entity = db.Jobs.FirstOrDefault(x => x.JobId == id);
-      if (entity != null) db.Jobs.DeleteOnSubmit(entity);
-      db.SubmitChanges();
+      using (var db = CreateContext()) {
+        var entity = db.Jobs.FirstOrDefault(x => x.JobId == id);
+        if (entity != null) db.Jobs.DeleteOnSubmit(entity);
+        db.SubmitChanges();
+      }
     }
 
     public IEnumerable<DT.Job> GetAvailableParentJobs(Guid slaveId) {
-      // todo: slaveId is unused!
-      var query = from ar in db.AssignedResources
-                  where ar.Job.JobState == JobState.WaitForChildJobs &&
-                    (from child in db.Jobs
-                     where child.ParentJobId == ar.Job.JobId
-                     select child.JobState == JobState.Finished).All(x => x) &&
-                    (from child in db.Jobs // avoid returning WaitForChildJobs jobs where no child-jobs exist (yet)
-                     where child.ParentJobId == ar.Job.JobId
-                     select child).Count() > 0
-                  orderby ar.Job.Priority descending
-                  select Convert.ToDto(ar.Job);
-      return query;
+      using (var db = CreateContext()) {
+        // todo: slaveId is unused!
+        var query = from ar in db.AssignedResources
+                    where ar.Job.JobState == JobState.WaitingForChildJobs &&
+                      (from child in db.Jobs
+                       where child.ParentJobId == ar.Job.JobId
+                       select child.JobState == JobState.Finished).All(x => x) &&
+                      (from child in db.Jobs // avoid returning WaitForChildJobs jobs where no child-jobs exist (yet)
+                       where child.ParentJobId == ar.Job.JobId
+                       select child).Count() > 0
+                    orderby ar.Job.Priority descending
+                    select Convert.ToDto(ar.Job);
+        return query;
+      }
     }
 
     public IEnumerable<DT.Job> GetAvailableJobs(DT.Slave slave) {
-      var query = from j in db.Jobs
-                  where j.JobState == JobState.Waiting && j.CoresNeeded <= slave.FreeCores && j.MemoryNeeded <= slave.FreeMemory
-                  orderby j.Priority descending
-                  select Convert.ToDto(j);
-      return query.Union(GetAvailableParentJobs(slave.Id)).OrderByDescending(x => x.Priority).ToArray();
+      using (var db = CreateContext()) {
+        var query = from j in db.Jobs
+                    where j.JobState == JobState.Waiting && j.CoresNeeded <= slave.FreeCores && j.MemoryNeeded <= slave.FreeMemory
+                    orderby j.Priority descending
+                    select Convert.ToDto(j);
+        return query.Union(GetAvailableParentJobs(slave.Id)).OrderByDescending(x => x.Priority).ToArray();
+      }
     }
     #endregion
@@ -73,29 +88,39 @@
 
     public DT.JobData GetJobData(Guid id) {
-      return Convert.ToDto(db.JobDatas.SingleOrDefault(x => x.JobId == id));
+      using (var db = CreateContext()) {
+        return Convert.ToDto(db.JobDatas.SingleOrDefault(x => x.JobId == id));
+      }
     }
 
     public IEnumerable<DT.JobData> GetJobDatas(Expression<Func<JobData, bool>> predicate) {
-      return db.JobDatas.Where(predicate).Select(x => Convert.ToDto(x));
+      using (var db = CreateContext()) {
+        return db.JobDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
+      }
     }
 
     public Guid AddJobData(DT.JobData dto) {
-      var entity = Convert.ToEntity(dto);
-      db.JobDatas.InsertOnSubmit(entity);
-      db.SubmitChanges();
-      return entity.JobId;
+      using (var db = CreateContext()) {
+        var entity = Convert.ToEntity(dto);
+        db.JobDatas.InsertOnSubmit(entity);
+        db.SubmitChanges();
+        return entity.JobId;
+      }
     }
 
     public void UpdateJobData(DT.JobData dto) {
-      var entity = db.JobDatas.FirstOrDefault(x => x.JobId == dto.Id);
-      if (entity == null) db.JobDatas.InsertOnSubmit(Convert.ToEntity(dto));
-      else Convert.ToEntity(dto, entity);
-      db.SubmitChanges();
+      using (var db = CreateContext()) {
+        var entity = db.JobDatas.FirstOrDefault(x => x.JobId == dto.JobId);
+        if (entity == null) db.JobDatas.InsertOnSubmit(Convert.ToEntity(dto));
+        else Convert.ToEntity(dto, entity);
+        db.SubmitChanges();
+      }
     }
 
     public void DeleteJobData(Guid id) {
-      var entity = db.JobDatas.FirstOrDefault(x => x.JobId == id); // check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
-      if (entity != null) db.JobDatas.DeleteOnSubmit(entity);
-      db.SubmitChanges();
+      using (var db = CreateContext()) {
+        var entity = db.JobDatas.FirstOrDefault(x => x.JobId == id); // check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
+        if (entity != null) db.JobDatas.DeleteOnSubmit(entity);
+        db.SubmitChanges();
+      }
     }
     #endregion
@@ -103,60 +128,79 @@
     #region HiveExperiment Methods
     public DT.HiveExperiment GetHiveExperiment(Guid id) {
-      return Convert.ToDto(db.HiveExperiments.SingleOrDefault(x => x.HiveExperimentId == id));
+      using (var db = CreateContext()) {
+        return Convert.ToDto(db.HiveExperiments.SingleOrDefault(x => x.HiveExperimentId == id));
+      }
     }
 
     public IEnumerable<DT.HiveExperiment> GetHiveExperiments(Expression<Func<HiveExperiment, bool>> predicate) {
-      return db.HiveExperiments.Where(predicate).Select(x => Convert.ToDto(x));
+      using (var db = CreateContext()) {
+        return db.HiveExperiments.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
+      }
     }
 
     public Guid AddHiveExperiment(DT.HiveExperiment dto) {
-      var entity = Convert.ToEntity(dto);
-      db.HiveExperiments.InsertOnSubmit(entity);
-      db.SubmitChanges();
-      return entity.HiveExperimentId;
+      using (var db = CreateContext()) {
+        var entity = Convert.ToEntity(dto);
+        db.HiveExperiments.InsertOnSubmit(entity);
+        db.SubmitChanges();
+        return entity.HiveExperimentId;
+      }
     }
 
     public void UpdateHiveExperiment(DT.HiveExperiment dto) {
-      var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == dto.Id);
-      if (entity == null) db.HiveExperiments.InsertOnSubmit(Convert.ToEntity(dto));
-      else Convert.ToEntity(dto, entity);
-      db.SubmitChanges();
+      using (var db = CreateContext()) {
+        var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == dto.Id);
+        if (entity == null) db.HiveExperiments.InsertOnSubmit(Convert.ToEntity(dto));
+        else Convert.ToEntity(dto, entity);
+        db.SubmitChanges();
+      }
     }
 
     public void DeleteHiveExperiment(Guid id) {
-      var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == id);
-      if (entity != null) db.HiveExperiments.DeleteOnSubmit(entity);
-      db.SubmitChanges();
-    }
-    #endregion
-
+      using (var db = CreateContext()) {
+        var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == id);
+        if (entity != null) db.HiveExperiments.DeleteOnSubmit(entity);
+        db.SubmitChanges();
+      }
+    }
+    #endregion
 
     #region Slave Methods
     public DT.Slave GetSlave(Guid id) {
-      return Convert.ToDto(db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id));
+      using (var db = CreateContext()) {
+        return Convert.ToDto(db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id));
+      }
     }
 
     public IEnumerable<DT.Slave> GetSlaves(Expression<Func<Slave, bool>> predicate) {
-      return db.Resources.OfType<Slave>().Where(predicate).Select(x => Convert.ToDto(x));
+      using (var db = CreateContext()) {
+        return db.Resources.OfType<Slave>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
+      }
     }
 
     public Guid AddSlave(DT.Slave dto) {
-      var entity = Convert.ToEntity(dto);
-      db.Resources.InsertOnSubmit(entity);
-      db.SubmitChanges();
-      return entity.ResourceId;
+      using (var db = CreateContext()) {
+        var entity = Convert.ToEntity(dto);
+        db.Resources.InsertOnSubmit(entity);
+        db.SubmitChanges();
+        return entity.ResourceId;
+      }
     }
 
     public void UpdateSlave(DT.Slave dto) {
-      var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == dto.Id);
-      if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
-      else Convert.ToEntity(dto, entity);
-      db.SubmitChanges();
+      using (var db = CreateContext()) {
+        var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == dto.Id);
+        if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
+        else Convert.ToEntity(dto, entity);
+        db.SubmitChanges();
+      }
     }
 
     public void DeleteSlave(Guid id) {
-      var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == id);
-      if (entity != null) db.Resources.DeleteOnSubmit(entity);
-      db.SubmitChanges();
+      using (var db = CreateContext()) {
+        var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == id);
+        if (entity != null) db.Resources.DeleteOnSubmit(entity);
+        db.SubmitChanges();
+      }
     }
     #endregion
@@ -164,29 +208,39 @@
     #region SlaveGroup Methods
     public DT.SlaveGroup GetSlaveGroup(Guid id) {
-      return Convert.ToDto(db.Resources.OfType<SlaveGroup>().SingleOrDefault(x => x.ResourceId == id));
+      using (var db = CreateContext()) {
+        return Convert.ToDto(db.Resources.OfType<SlaveGroup>().SingleOrDefault(x => x.ResourceId == id));
+      }
     }
 
     public IEnumerable<DT.SlaveGroup> GetSlaveGroups(Expression<Func<SlaveGroup, bool>> predicate) {
-      return db.Resources.OfType<SlaveGroup>().Where(predicate).Select(x => Convert.ToDto(x));
+      using (var db = CreateContext()) {
+        return db.Resources.OfType<SlaveGroup>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
+      }
     }
 
     public Guid AddSlaveGroup(DT.SlaveGroup dto) {
-      var entity = Convert.ToEntity(dto);
-      db.Resources.InsertOnSubmit(entity);
-      db.SubmitChanges();
-      return entity.ResourceId;
+      using (var db = CreateContext()) {
+        var entity = Convert.ToEntity(dto);
+        db.Resources.InsertOnSubmit(entity);
+        db.SubmitChanges();
+        return entity.ResourceId;
+      }
     }
 
     public void UpdateSlaveGroup(DT.SlaveGroup dto) {
-      var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == dto.Id);
-      if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
-      else Convert.ToEntity(dto, entity);
-      db.SubmitChanges();
+      using (var db = CreateContext()) {
+        var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == dto.Id);
+        if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
+        else Convert.ToEntity(dto, entity);
+        db.SubmitChanges();
+      }
     }
 
     public void DeleteSlaveGroup(Guid id) {
-      var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == id);
-      if (entity != null) db.Resources.DeleteOnSubmit(entity);
-      db.SubmitChanges();
+      using (var db = CreateContext()) {
+        var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == id);
+        if (entity != null) db.Resources.DeleteOnSubmit(entity);
+        db.SubmitChanges();
+      }
     }
     #endregion
@@ -194,29 +248,39 @@
     #region Resource Methods
     public DT.Resource GetResource(Guid id) {
-      return Convert.ToDto(db.Resources.SingleOrDefault(x => x.ResourceId == id));
+      using (var db = CreateContext()) {
+        return Convert.ToDto(db.Resources.SingleOrDefault(x => x.ResourceId == id));
+      }
     }
 
     public IEnumerable<DT.Resource> GetResources(Expression<Func<Resource, bool>> predicate) {
-      return db.Resources.Where(predicate).Select(x => Convert.ToDto(x));
+      using (var db = CreateContext()) {
+        return db.Resources.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
+      }
     }
 
     public Guid AddResource(DT.Resource dto) {
-      var entity = Convert.ToEntity(dto);
-      db.Resources.InsertOnSubmit(entity);
-      db.SubmitChanges();
-      return entity.ResourceId;
+      using (var db = CreateContext()) {
+        var entity = Convert.ToEntity(dto);
+        db.Resources.InsertOnSubmit(entity);
+        db.SubmitChanges();
+        return entity.ResourceId;
+      }
     }
 
     public void UpdateResource(DT.Resource dto) {
-      var entity = db.Resources.FirstOrDefault(x => x.ResourceId == dto.Id);
-      if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
-      else Convert.ToEntity(dto, entity);
-      db.SubmitChanges();
+      using (var db = CreateContext()) {
+        var entity = db.Resources.FirstOrDefault(x => x.ResourceId == dto.Id);
+        if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
+        else Convert.ToEntity(dto, entity);
+        db.SubmitChanges();
+      }
     }
 
     public void DeleteResource(Guid id) {
-      var entity = db.Resources.FirstOrDefault(x => x.ResourceId == id);
-      if (entity != null) db.Resources.DeleteOnSubmit(entity);
-      db.SubmitChanges();
+      using (var db = CreateContext()) {
+        var entity = db.Resources.FirstOrDefault(x => x.ResourceId == id);
+        if (entity != null) db.Resources.DeleteOnSubmit(entity);
+        db.SubmitChanges();
+      }
     }
     #endregion
@@ -224,12 +288,12 @@
     #region Authorization Methods
     public bool IsUserAuthorizedForJobs(Guid userId, params Guid[] jobIds) {
-      var userIds = from job in db.Jobs // this needs to be fast!
-                    where jobIds.Contains(job.JobId)
-                    select job.UserId;
-      return userIds.All(x => x == userId);
-    }
-    #endregion
-
-
+      using (var db = CreateContext()) {
+        var userIds = from job in db.Jobs // this needs to be fast!
+                      where jobIds.Contains(job.JobId)
+                      select job.UserId;
+        return userIds.All(x => x == userId);
+      }
+    }
+    #endregion
   }
 }
Index: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/HiveDataContext.dbml
===================================================================
--- branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/HiveDataContext.dbml	(revision 4629)
+++ branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/HiveDataContext.dbml	(revision 4649)
@@ -70,13 +70,13 @@
       <Association Name="Resource_UptimeCalendar" Member="UptimeCalendars" ThisKey="ResourceId" OtherKey="ResourceId" Type="UptimeCalendar" />
       <Type Name="Slave" InheritanceCode="Slave" IsInheritanceDefault="true">
-        <Column Name="CPUSpeed" Type="System.Int32" DbType="Int" CanBeNull="true" />
+        <Column Name="CpuSpeed" Storage="_CPUSpeed" Type="System.Int32" DbType="Int" CanBeNull="true" />
         <Column Name="Memory" Type="System.Int32" DbType="Int" CanBeNull="true" />
         <Column Name="Login" Type="System.DateTime" DbType="DateTime" CanBeNull="true" />
-        <Column Name="Status" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
-        <Column Name="CalendarSyncStatus" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
+        <Column Name="SlaveState" Storage="_Status" Type="global::HeuristicLab.Services.Hive.Common.DataTransfer.SlaveState" DbType="VarChar(15)" CanBeNull="true" />
+        <Column Name="CalendarSyncState" Storage="_CalendarSyncStatus" Type="global::HeuristicLab.Services.Hive.Common.DataTransfer.CalendarState" DbType="VarChar(MAX)" CanBeNull="true" />
         <Column Name="UseCalendarFromResourceId" Type="System.Guid" DbType="UniqueIdentifier" CanBeNull="true" />
         <Column Name="SlaveConfigId" Type="System.Guid" DbType="UniqueIdentifier" CanBeNull="true" />
-        <Column Name="NumberOfCores" Type="System.Int32" DbType="Int" CanBeNull="true" />
-        <Column Name="NumberOfFreeCores" Type="System.Int32" DbType="Int" CanBeNull="true" />
+        <Column Name="Cores" Storage="_NumberOfCores" Type="System.Int32" DbType="Int" CanBeNull="true" />
+        <Column Name="FreeCores" Storage="_NumberOfFreeCores" Type="System.Int32" DbType="Int" CanBeNull="true" />
         <Column Name="FreeMemory" Type="System.Int32" DbType="Int" CanBeNull="true" />
         <Column Name="IsAllowedToCalculate" Type="System.Boolean" DbType="Bit" CanBeNull="false" />
Index: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/HiveDataContext.designer.cs
===================================================================
--- branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/HiveDataContext.designer.cs	(revision 4629)
+++ branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/HiveDataContext.designer.cs	(revision 4649)
@@ -1569,7 +1569,7 @@
 		private System.Nullable<System.DateTime> _Login;
 		
-		private string _Status;
-		
-		private string _CalendarSyncStatus;
+		private global::HeuristicLab.Services.Hive.Common.DataTransfer.SlaveState _Status;
+		
+		private global::HeuristicLab.Services.Hive.Common.DataTransfer.CalendarState _CalendarSyncStatus;
 		
 		private System.Nullable<System.Guid> _UseCalendarFromResourceId;
@@ -1595,22 +1595,22 @@
     partial void OnValidate(System.Data.Linq.ChangeAction action);
     partial void OnCreated();
-    partial void OnCPUSpeedChanging(System.Nullable<int> value);
-    partial void OnCPUSpeedChanged();
+    partial void OnCpuSpeedChanging(System.Nullable<int> value);
+    partial void OnCpuSpeedChanged();
     partial void OnMemoryChanging(System.Nullable<int> value);
     partial void OnMemoryChanged();
     partial void OnLoginChanging(System.Nullable<System.DateTime> value);
     partial void OnLoginChanged();
-    partial void OnStatusChanging(string value);
-    partial void OnStatusChanged();
-    partial void OnCalendarSyncStatusChanging(string value);
-    partial void OnCalendarSyncStatusChanged();
+    partial void OnSlaveStateChanging(global::HeuristicLab.Services.Hive.Common.DataTransfer.SlaveState value);
+    partial void OnSlaveStateChanged();
+    partial void OnCalendarSyncStateChanging(global::HeuristicLab.Services.Hive.Common.DataTransfer.CalendarState value);
+    partial void OnCalendarSyncStateChanged();
     partial void OnUseCalendarFromResourceIdChanging(System.Nullable<System.Guid> value);
     partial void OnUseCalendarFromResourceIdChanged();
     partial void OnSlaveConfigIdChanging(System.Nullable<System.Guid> value);
     partial void OnSlaveConfigIdChanged();
-    partial void OnNumberOfCoresChanging(System.Nullable<int> value);
-    partial void OnNumberOfCoresChanged();
-    partial void OnNumberOfFreeCoresChanging(System.Nullable<int> value);
-    partial void OnNumberOfFreeCoresChanged();
+    partial void OnCoresChanging(System.Nullable<int> value);
+    partial void OnCoresChanged();
+    partial void OnFreeCoresChanging(System.Nullable<int> value);
+    partial void OnFreeCoresChanged();
     partial void OnFreeMemoryChanging(System.Nullable<int> value);
     partial void OnFreeMemoryChanged();
@@ -1628,5 +1628,5 @@
 		
 		[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_CPUSpeed", DbType="Int")]
-		public System.Nullable<int> CPUSpeed
+		public System.Nullable<int> CpuSpeed
 		{
 			get
@@ -1638,9 +1638,9 @@
 				if ((this._CPUSpeed != value))
 				{
-					this.OnCPUSpeedChanging(value);
+					this.OnCpuSpeedChanging(value);
 					this.SendPropertyChanging();
 					this._CPUSpeed = value;
-					this.SendPropertyChanged("CPUSpeed");
-					this.OnCPUSpeedChanged();
+					this.SendPropertyChanged("CpuSpeed");
+					this.OnCpuSpeedChanged();
 				}
 			}
@@ -1687,6 +1687,6 @@
 		}
 		
-		[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Status", DbType="VarChar(MAX)")]
-		public string Status
+		[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Status", DbType="VarChar(15)", CanBeNull=true)]
+		public global::HeuristicLab.Services.Hive.Common.DataTransfer.SlaveState SlaveState
 		{
 			get
@@ -1698,15 +1698,15 @@
 				if ((this._Status != value))
 				{
-					this.OnStatusChanging(value);
+					this.OnSlaveStateChanging(value);
 					this.SendPropertyChanging();
 					this._Status = value;
-					this.SendPropertyChanged("Status");
-					this.OnStatusChanged();
-				}
-			}
-		}
-		
-		[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_CalendarSyncStatus", DbType="VarChar(MAX)")]
-		public string CalendarSyncStatus
+					this.SendPropertyChanged("SlaveState");
+					this.OnSlaveStateChanged();
+				}
+			}
+		}
+		
+		[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_CalendarSyncStatus", DbType="VarChar(MAX)", CanBeNull=true)]
+		public global::HeuristicLab.Services.Hive.Common.DataTransfer.CalendarState CalendarSyncState
 		{
 			get
@@ -1718,9 +1718,9 @@
 				if ((this._CalendarSyncStatus != value))
 				{
-					this.OnCalendarSyncStatusChanging(value);
+					this.OnCalendarSyncStateChanging(value);
 					this.SendPropertyChanging();
 					this._CalendarSyncStatus = value;
-					this.SendPropertyChanged("CalendarSyncStatus");
-					this.OnCalendarSyncStatusChanged();
+					this.SendPropertyChanged("CalendarSyncState");
+					this.OnCalendarSyncStateChanged();
 				}
 			}
@@ -1772,5 +1772,5 @@
 		
 		[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_NumberOfCores", DbType="Int")]
-		public System.Nullable<int> NumberOfCores
+		public System.Nullable<int> Cores
 		{
 			get
@@ -1782,9 +1782,9 @@
 				if ((this._NumberOfCores != value))
 				{
-					this.OnNumberOfCoresChanging(value);
+					this.OnCoresChanging(value);
 					this.SendPropertyChanging();
 					this._NumberOfCores = value;
-					this.SendPropertyChanged("NumberOfCores");
-					this.OnNumberOfCoresChanged();
+					this.SendPropertyChanged("Cores");
+					this.OnCoresChanged();
 				}
 			}
@@ -1792,5 +1792,5 @@
 		
 		[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_NumberOfFreeCores", DbType="Int")]
-		public System.Nullable<int> NumberOfFreeCores
+		public System.Nullable<int> FreeCores
 		{
 			get
@@ -1802,9 +1802,9 @@
 				if ((this._NumberOfFreeCores != value))
 				{
-					this.OnNumberOfFreeCoresChanging(value);
+					this.OnFreeCoresChanging(value);
 					this.SendPropertyChanging();
 					this._NumberOfFreeCores = value;
-					this.SendPropertyChanged("NumberOfFreeCores");
-					this.OnNumberOfFreeCoresChanged();
+					this.SendPropertyChanged("FreeCores");
+					this.OnFreeCoresChanged();
 				}
 			}
Index: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/Tools/CreateHiveDatabaseApplication.cs
===================================================================
--- branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/Tools/CreateHiveDatabaseApplication.cs	(revision 4629)
+++ branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/Tools/CreateHiveDatabaseApplication.cs	(revision 4649)
@@ -27,11 +27,9 @@
 
     public override void Run() {
-      CreateDataContext<HiveDataContext> contextCreator = new CreateDataContext<HiveDataContext>(ContextFactory<HiveDataContext>.CreateHiveContext);
-      IContextFactory<HiveDataContext> contextFactory = new ContextFactory<HiveDataContext>(contextCreator);
-
-      using (contextFactory.GetContext(false)) {
-        if (contextFactory.CurrentContext.DatabaseExists())
-          contextFactory.CurrentContext.DeleteDatabase();
-        contextFactory.CurrentContext.CreateDatabase();
+      var dao = new HiveDao();
+      using (var db = dao.CreateContext()) {
+        if (db.DatabaseExists())
+          db.DeleteDatabase();
+        db.CreateDatabase();
       }
     }
Index: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/TransactionManager.cs
===================================================================
--- branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/TransactionManager.cs	(revision 4649)
+++ branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/TransactionManager.cs	(revision 4649)
@@ -0,0 +1,23 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using HeuristicLab.Services.Hive.Common;
+using System.Transactions;
+using HeuristicLab.Common;
+
+namespace HeuristicLab.Services.Hive.DataAccess {
+  public class TransactionManager {
+    public Disposable<TransactionScope> OpenTransaction() {
+      TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE });
+      var disposable = new Disposable<TransactionScope>(transaction);
+      disposable.Disposing += disposable_Disposing;
+      return disposable;
+    }
+
+    void disposable_Disposing(object sender, EventArgs<TransactionScope> scope) {
+      scope.Value.Complete();
+      scope.Value.Dispose();
+    }
+  }
+}
