Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2117 was 2117, checked in by svonolfe, 15 years ago

Streaming of Jobs and JobsResults directly from/to the DB (#680)

File size: 4.1 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 usageCounter = 0;
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.usageCounter = 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.usageCounter++;
77    }
78
79    #region ISession Members
80    public ISessionFactory Factory {
81      get {
82        return this.factory;
83      }
84    }
85
86    public ITransaction BeginTransaction() {
87      CheckThread();
88
89      if (transaction == null) {
90         transaction = new Transaction(this);
91         transaction.Connection = Connection;
92      }
93
94      transaction.IncrementUsageCounter();
95
96      return transaction;
97    }
98
99    public ITransaction GetCurrentTransaction() {
100      CheckThread();
101
102      return transaction;
103    }
104
105    public IDataAdapter<ObjT> GetDataAdapter<ObjT>()
106      where ObjT : IPersistableObject {
107      CheckThread();
108
109      Guid adapterId = typeof(IDataAdapter<ObjT>).GUID;
110
111      if (!adapters.ContainsKey(adapterId)) {
112        IDataAdapter<ObjT> adapter =
113          discoveryService.GetInstances<IDataAdapter<ObjT>>()[0];
114
115        adapter.Session = this;
116
117        adapters.Add(adapterId, adapter);
118      }
119
120      return adapters[adapterId] as IDataAdapter<ObjT>;
121    }
122
123    public T GetDataAdapter<ObjT, T>()
124      where ObjT : IPersistableObject
125      where T : class, IDataAdapter<ObjT>
126    {
127      CheckThread();
128
129      Guid adapterId = typeof(T).GUID;
130
131      if (!adapters.ContainsKey(adapterId)) {
132        T adapter =
133          discoveryService.GetInstances<T>()[0];
134
135        adapter.Session = this;
136
137        adapters.Add(adapterId, adapter);
138      }
139
140      return adapters[adapterId] as T;
141    }
142
143    public void EndSession() {
144      this.usageCounter--;
145
146      if (usageCounter <= 0) {
147        CheckThread();
148
149        if (transaction != null) {
150          transaction.Rollback();
151          transaction = null;
152        }
153        if (connection.State == System.Data.ConnectionState.Open) {
154          connection.Close();
155          connection = null;
156        }
157        factory.EndSession(this);
158      }
159    }
160
161    #endregion
162  }
163}
Note: See TracBrowser for help on using the repository browser.