Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataAccess.ADOHelper/3.2/Session.cs @ 1568

Last change on this file since 1568 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.DataAccess.Interfaces;
27using HeuristicLab.PluginInfrastructure;
28using System.Data.Common;
29using System.Threading;
30
31namespace HeuristicLab.DataAccess.ADOHelper {
32  public class Session: ISession {
33    private static DiscoveryService discoveryService =
34      new DiscoveryService();
35
36    private SessionFactory factory;
37
38    private Transaction transaction;
39
40    private DbConnection connection;
41
42    private Thread ownerThread;
43
44    private IDictionary<Guid, object> adapters =
45      new Dictionary<Guid, object>();
46
47    public Session(SessionFactory factory) {
48      this.factory = factory;
49      this.ownerThread = Thread.CurrentThread;
50    }
51
52    public void CheckThread() {
53      if (!Thread.CurrentThread.Equals(ownerThread)) {
54        throw new Exception("Session is owned by another thread");
55      }
56    }
57
58    public DbConnection Connection {
59      get {
60        if (connection == null) {
61          connection = factory.CreateConnection();
62        }
63
64        return connection;
65      }
66    }
67
68    public void DetachTrasaction() {
69      this.transaction = null;
70    }
71
72    #region ISession Members
73    public ITransaction BeginTransaction() {
74      CheckThread();
75
76      if (transaction == null) {
77         transaction = new Transaction(this);
78         transaction.Connection = Connection;
79      }
80
81      return transaction;
82    }
83
84    public ITransaction GetCurrentTransaction() {
85      CheckThread();
86
87      return transaction;
88    }
89
90    public IDataAdapter<ObjT> GetDataAdapter<ObjT>()
91      where ObjT : IPersistableObject {
92      CheckThread();
93
94      Guid adapterId = typeof(IDataAdapter<ObjT>).GUID;
95
96      if (!adapters.ContainsKey(adapterId)) {
97        IDataAdapter<ObjT> adapter =
98          discoveryService.GetInstances<IDataAdapter<ObjT>>()[0];
99
100        adapter.Session = this;
101
102        adapters.Add(adapterId, adapter);
103      }
104
105      return adapters[adapterId] as IDataAdapter<ObjT>;
106    }
107
108    public T GetDataAdapter<ObjT, T>()
109      where ObjT : IPersistableObject
110      where T : class, IDataAdapter<ObjT>
111    {
112      CheckThread();
113
114      Guid adapterId = typeof(T).GUID;
115
116      if (!adapters.ContainsKey(adapterId)) {
117        T adapter =
118          discoveryService.GetInstances<T>()[0];
119
120        adapter.Session = this;
121
122        adapters.Add(adapterId, adapter);
123      }
124
125      return adapters[adapterId] as T;
126    }
127
128    public void EndSession() {
129      CheckThread();
130
131      if (transaction != null) {
132        transaction.Rollback();
133        transaction = null;
134      }
135      if (connection.State == System.Data.ConnectionState.Open) {
136        connection.Close();
137        connection = null;
138      }
139      factory.EndSession(this);
140    }
141
142    #endregion
143  }
144}
Note: See TracBrowser for help on using the repository browser.