Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.DataAccess.ADOHelper/3.2/Session.cs @ 4483

Last change on this file since 4483 was 2096, checked in by svonolfe, 16 years ago

Updated SessionFactory (#372)

File size: 3.9 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 int counter;
45
46    private IDictionary<Guid, object> adapters =
47      new Dictionary<Guid, object>();
48
49    public Session(SessionFactory factory) {
50      this.factory = factory;
51      this.ownerThread = Thread.CurrentThread;
52      this.counter = 0;
53    }
54
55    public void CheckThread() {
56      if (!Thread.CurrentThread.Equals(ownerThread)) {
57        throw new Exception("Session is owned by another thread");
58      }
59    }
60
61    public DbConnection Connection {
62      get {
63        if (connection == null) {
64          connection = factory.CreateConnection();
65        }
66
67        return connection;
68      }
69    }
70
71    public void DetachTrasaction() {
72      this.transaction = null;
73    }
74
75    public void IncrementUsageCounter() {
76      this.counter++;
77    }
78
79    #region ISession Members
80    public ITransaction BeginTransaction() {
81      CheckThread();
82
83      if (transaction == null) {
84         transaction = new Transaction(this);
85         transaction.Connection = Connection;
86      }
87
88      return transaction;
89    }
90
91    public ITransaction GetCurrentTransaction() {
92      CheckThread();
93
94      return transaction;
95    }
96
97    public IDataAdapter<ObjT> GetDataAdapter<ObjT>()
98      where ObjT : IPersistableObject {
99      CheckThread();
100
101      Guid adapterId = typeof(IDataAdapter<ObjT>).GUID;
102
103      if (!adapters.ContainsKey(adapterId)) {
104        IDataAdapter<ObjT> adapter =
105          discoveryService.GetInstances<IDataAdapter<ObjT>>()[0];
106
107        adapter.Session = this;
108
109        adapters.Add(adapterId, adapter);
110      }
111
112      return adapters[adapterId] as IDataAdapter<ObjT>;
113    }
114
115    public T GetDataAdapter<ObjT, T>()
116      where ObjT : IPersistableObject
117      where T : class, IDataAdapter<ObjT>
118    {
119      CheckThread();
120
121      Guid adapterId = typeof(T).GUID;
122
123      if (!adapters.ContainsKey(adapterId)) {
124        T adapter =
125          discoveryService.GetInstances<T>()[0];
126
127        adapter.Session = this;
128
129        adapters.Add(adapterId, adapter);
130      }
131
132      return adapters[adapterId] as T;
133    }
134
135    public void EndSession() {
136      this.counter--;
137
138      if (counter <= 0) {
139        CheckThread();
140
141        if (transaction != null) {
142          transaction.Rollback();
143          transaction = null;
144        }
145        if (connection.State == System.Data.ConnectionState.Open) {
146          connection.Close();
147          connection = null;
148        }
149        factory.EndSession(this);
150      }
151    }
152
153    #endregion
154  }
155}
Note: See TracBrowser for help on using the repository browser.