Index: branches/HivePerformance/sources/HeuristicLab.Services.Hive/3.3/Convert.cs
===================================================================
--- branches/HivePerformance/sources/HeuristicLab.Services.Hive/3.3/Convert.cs	(revision 9391)
+++ branches/HivePerformance/sources/HeuristicLab.Services.Hive/3.3/Convert.cs	(revision 9393)
@@ -112,5 +112,7 @@
     public static void ToEntity(DT.TaskData source, DB.TaskData target) {
       if ((source != null) && (target != null)) {
-        target.TaskId = source.TaskId; target.Data = new Binary(source.Data); target.LastUpdate = source.LastUpdate;
+        target.TaskId = source.TaskId;
+        target.Data = source.Data;
+        target.LastUpdate = source.LastUpdate;
       }
     }
@@ -214,5 +216,8 @@
     public static void ToEntity(DT.PluginData source, DB.PluginData target) {
       if ((source != null) && (target != null)) {
-        target.PluginDataId = source.Id; target.PluginId = source.PluginId; target.Data = new Binary(source.Data); target.FileName = source.FileName;
+        target.PluginDataId = source.Id;
+        target.PluginId = source.PluginId;
+        target.Data = source.Data;
+        target.FileName = source.FileName;
       }
     }
Index: branches/HivePerformance/sources/HeuristicLab.Services.Hive/3.3/HiveDao.cs
===================================================================
--- branches/HivePerformance/sources/HeuristicLab.Services.Hive/3.3/HiveDao.cs	(revision 9393)
+++ branches/HivePerformance/sources/HeuristicLab.Services.Hive/3.3/HiveDao.cs	(revision 9393)
@@ -0,0 +1,245 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2012 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.Linq;
+using DT = HeuristicLab.Services.Hive.DataTransfer;
+
+namespace HeuristicLab.Services.Hive.DataAccess {
+  public class HiveDao : IHiveDao {
+    private HiveDataContext Db { get { return HiveOperationContext.Current.DataContext; } }
+
+    #region Task Methods
+    public Task GetTaskById(Guid id) {
+      return GetTaskByIdQuery(Db, id).SingleOrDefault();
+    }
+
+    private static Func<HiveDataContext, Guid, IQueryable<Task>> GetTaskByIdQuery = CompiledQuery.Compile((HiveDataContext db, Guid id) =>
+      from t in db.Tasks
+      where t.TaskId == id
+      select t
+    );
+
+    public void UpdateTaskAndPlugins(Task task) {       /*
+      foreach (var pluginId in task.RequiredPlugins.Select(p => p.PluginId)) {
+        if (db.RequiredPlugins.Count(p => p.PluginId == pluginId) == 0) {
+          db.RequiredPlugins.InsertOnSubmit(new RequiredPlugin() { TaskId = task.TaskId, PluginId = pluginId });
+        }
+      }
+      db.SubmitChanges();                                   */
+    }
+
+    public void UpdateTask(Task task) {
+      Db.SubmitChanges();
+    }
+
+    public IQueryable<TaskInfoForScheduler> GetWaitingTasks(Slave slave) {
+      var parentResources = GetParentResources(slave.ResourceId);
+      var resourceIds = parentResources.Select(x => x.ResourceId);
+      //Originally we checked here if there are parent tasks which should be calculated (with GetParentTasks(resourceIds, count, false);).
+      //Because there is at the moment no case where this makes sense (there don't exist parent tasks which need to be calculated), 
+      //we skip this step because it's wasted runtime
+
+      return from ar in Db.AssignedResources
+             where resourceIds.Contains(ar.ResourceId)
+                 && !(ar.Task.IsParentTask && ar.Task.FinishWhenChildJobsFinished)
+                 && ar.Task.State == TaskState.Waiting
+                 && ar.Task.CoresNeeded <= slave.FreeCores
+                 && ar.Task.MemoryNeeded <= slave.FreeMemory
+             select new TaskInfoForScheduler() { TaskId = ar.Task.TaskId, JobId = ar.Task.JobId, Priority = ar.Task.Priority };
+    }
+
+    /*private static Func<HiveDataContext, Guid, Slave, IQueryable<TaskInfoForScheduler>> GetWaitingTasksQuery = CompiledQuery.Compile((HiveDataContext db, Guid id, Slave slave) =>
+      from ar in db.AssignedResources
+      where ar.ResourceId == id
+          && !(ar.Task.IsParentTask && ar.Task.FinishWhenChildJobsFinished)
+          && ar.Task.State == TaskState.Waiting
+          && ar.Task.CoresNeeded <= slave.FreeCores
+          && ar.Task.MemoryNeeded <= slave.FreeMemory
+      select new TaskInfoForScheduler() { TaskId = ar.Task.TaskId, JobId = ar.Task.JobId, Priority = ar.Task.Priority }
+    );*/
+
+    public Task UpdateTaskState(Guid taskId, TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
+      Db.StateLogs.InsertOnSubmit(new StateLog {
+        TaskId = taskId,
+        State = taskState,
+        SlaveId = slaveId,
+        UserId = userId,
+        Exception = exception,
+        DateTime = DateTime.Now
+      });
+
+      var task = GetTaskById(taskId);
+      task.State = taskState;
+
+      Db.SubmitChanges();
+
+      return task;
+    }
+    #endregion
+
+    #region TaskData Methods
+    public TaskData GetTaskDataById(Guid id) {
+      return GetTaskDataByIdQuery(Db, id).SingleOrDefault();
+    }
+
+    private static Func<HiveDataContext, Guid, IQueryable<TaskData>> GetTaskDataByIdQuery = CompiledQuery.Compile((HiveDataContext db, Guid id) =>
+      from t in db.TaskDatas
+      where t.TaskId == id
+      select t
+    );
+
+    public TaskData GetTaskDataByDto(DT.TaskData dto) {
+      var taskData = GetTaskDataById(dto.TaskId);
+      DT.Convert.ToEntity(dto, taskData);
+      return taskData;
+    }
+
+    public void UpdateTaskData(TaskData taskData) {
+      Db.SubmitChanges();
+    }
+    #endregion
+
+    #region StateLog Methods
+
+    #endregion
+
+    #region Job Methods
+
+    #endregion
+
+    #region JobPermission Methods
+
+    #endregion
+
+    #region Plugin Methods
+
+    #endregion
+
+    #region PluginData Methods
+
+    #endregion
+
+    #region Slave Methods
+    public Slave GetSlaveById(Guid id) {
+      return GetSlaveByIdQuery(Db, id).SingleOrDefault();
+    }
+
+    private static Func<HiveDataContext, Guid, IQueryable<Slave>> GetSlaveByIdQuery = CompiledQuery.Compile((HiveDataContext db, Guid slaveId) =>
+      from s in db.Resources.OfType<Slave>()
+      where s.ResourceId == slaveId
+      select s
+    );
+
+    public void UpdateSlave(Slave slave) {
+      Db.SubmitChanges();
+    }
+    #endregion
+
+    #region SlaveGroup Methods
+
+    #endregion
+
+    #region Resource Methods
+    public IQueryable<Guid> GetAssignedResourcesIds(Guid taskId) {
+      return GetAssignedResourcesIdQuery(Db, taskId);
+    }
+
+    private static Func<HiveDataContext, Guid, IQueryable<Guid>> GetAssignedResourcesIdQuery = CompiledQuery.Compile((HiveDataContext db, Guid taskId) =>
+      from ar in db.AssignedResources
+      where ar.TaskId == taskId
+      select ar.ResourceId
+    );
+
+    public IEnumerable<Resource> GetParentResources(Guid resourceId) {
+      var child = Db.Resources.Single(r => r.ResourceId == resourceId);
+
+      yield return child;
+      while (child.ParentResource != null) {
+        child = child.ParentResource;
+        yield return child;
+      }
+    }
+
+    public IEnumerable<Guid> GetParentResourcesIDs(Guid resourceId) {
+      var child = Db.Resources.Single(r => r.ResourceId == resourceId);
+
+      yield return resourceId;
+      while (child.ParentResource != null) {
+        child = child.ParentResource;
+        yield return child.ResourceId;
+      }
+    }
+
+    private static void CollectParentResources(ICollection<Resource> resources, Resource resource) {
+      if (resource == null) return;
+      resources.Add(resource);
+      CollectParentResources(resources, resource.ParentResource);
+    }
+    #endregion
+
+    #region ResourcePermission Methods
+
+    #endregion
+
+    #region Authorization Methods
+
+    #endregion
+
+    #region Lifecycle Methods
+
+    #endregion
+
+    #region Downtime Methods
+    public IQueryable<int> GetNumberOfDowntimesFromParentResourcesAtCurrentTime(Guid resourceId, DowntimeType type) {
+      var ids = GetParentResourcesIDs(resourceId).ToArray();
+
+      return from r in Db.Resources
+             where ids.Contains(r.ResourceId)
+             select (from d in Db.Downtimes
+                     where d.ResourceId == r.ResourceId && d.DowntimeType == type && (DateTime.Now >= d.StartDate) && (DateTime.Now <= d.EndDate)
+                     select d).Count();
+    }
+
+    /*private static Func<HiveDataContext, Guid, DowntimeType, int> GetNumberOfDowntimesAtCurrentTimeQuery =
+      CompiledQuery.Compile((HiveDataContext db, Guid ids, DowntimeType type) =>
+        (from d in db.Downtimes
+         where d.ResourceId == ids && d.DowntimeType == type && (DateTime.Now >= d.StartDate) && (DateTime.Now <= d.EndDate)
+         select d).Count()
+    );*/
+
+    #endregion
+
+    #region Statistics Methods
+
+    #endregion
+
+    #region UserPriority Methods
+
+    #endregion
+
+    #region Helpers
+
+    #endregion
+  }
+}
Index: branches/HivePerformance/sources/HeuristicLab.Services.Hive/3.3/HiveOperationContext.cs
===================================================================
--- branches/HivePerformance/sources/HeuristicLab.Services.Hive/3.3/HiveOperationContext.cs	(revision 9391)
+++ branches/HivePerformance/sources/HeuristicLab.Services.Hive/3.3/HiveOperationContext.cs	(revision 9393)
@@ -39,13 +39,23 @@
     }
 
-    public HiveDataContext DataContext { get; private set; }
+    private HiveDataContext dataContext;
+    public HiveDataContext DataContext {
+      get {
+        if (dataContext == null) {
+          dataContext = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
+          //dataContext.Log = new DebugWriter();
+        }
+        return dataContext;
+      }
+    }
+
 
     public void Attach(OperationContext owner) {
-      DataContext = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
-      //DataContext.Log = new DebugWriter();
     }
 
     public void Detach(OperationContext owner) {
-      DataContext.Dispose();
+      if (dataContext != null) {
+        dataContext.Dispose();
+      }
     }
   }
Index: branches/HivePerformance/sources/HeuristicLab.Services.Hive/3.3/Interfaces/IHiveDao.cs
===================================================================
--- branches/HivePerformance/sources/HeuristicLab.Services.Hive/3.3/Interfaces/IHiveDao.cs	(revision 9393)
+++ branches/HivePerformance/sources/HeuristicLab.Services.Hive/3.3/Interfaces/IHiveDao.cs	(revision 9393)
@@ -0,0 +1,106 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2012 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.Linq;
+using HeuristicLab.Services.Hive.DataAccess;
+using DT = HeuristicLab.Services.Hive.DataTransfer;
+namespace HeuristicLab.Services.Hive {
+  public interface IHiveDao {
+    #region Task Methods
+    Task GetTaskById(Guid id);
+
+    IQueryable<TaskInfoForScheduler> GetWaitingTasks(Slave slave);
+
+    void UpdateTask(Task task);
+    void UpdateTaskAndPlugins(Task task);
+    Task UpdateTaskState(Guid taskId, TaskState taskState, Guid? slaveId, Guid? userId, string exception);
+
+    #endregion
+
+    #region TaskData Methods
+    TaskData GetTaskDataByDto(DT.TaskData dto);
+    void UpdateTaskData(TaskData taskData);
+    #endregion
+
+    #region StateLog Methods
+
+    #endregion
+
+    #region Job Methods
+
+    #endregion
+
+    #region JobPermission Methods
+
+    #endregion
+
+    #region Plugin Methods
+
+    #endregion
+
+    #region PluginData Methods
+
+    #endregion
+
+    #region Slave Methods
+    Slave GetSlaveById(Guid id);
+
+    void UpdateSlave(Slave slave);
+    #endregion
+
+    #region SlaveGroup Methods
+
+    #endregion
+
+    #region Resource Methods
+    IQueryable<Guid> GetAssignedResourcesIds(Guid taskId);
+    IEnumerable<Resource> GetParentResources(Guid resourceId);
+    IEnumerable<Guid> GetParentResourcesIDs(Guid resourceId);
+    #endregion
+
+    #region ResourcePermission Methods
+
+    #endregion
+
+    #region Authorization Methods
+
+    #endregion
+
+    #region Lifecycle Methods
+
+    #endregion
+
+    #region Downtime Methods
+    IQueryable<int> GetNumberOfDowntimesFromParentResourcesAtCurrentTime(Guid resourceId, DowntimeType type);
+    #endregion
+
+    #region Statistics Methods
+
+    #endregion
+
+    #region UserPriority Methods
+
+    #endregion
+  }
+}
