Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3043 was 2591, checked in by gkronber, 15 years ago

Copied refactored plugin infrastructure from branch and merged changeset r2586:2589 from branch into the trunk. #799

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