Free cookie consent management tool by TermsFeed Policy Generator

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

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

Avoided possible race conditions when streaming data from/into the DB (#680)

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