Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated transaction management (#527)

File size: 3.3 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;
29
30namespace HeuristicLab.DataAccess.ADOHelper {
31  public class Session: ISession {
32    private static DiscoveryService discoveryService =
33      new DiscoveryService();
34
35    private SessionFactory factory;
36
37    private Transaction transaction;
38
39    private DbConnection connection;
40
41    private IDictionary<Guid, object> adapters =
42      new Dictionary<Guid, object>();
43
44    public Session(SessionFactory factory) {
45      this.factory = factory;
46    }
47
48    public DbConnection Connection {
49      get {
50        if (connection == null) {
51          connection = factory.CreateConnection();
52        }
53
54        return connection;
55      }
56    }
57
58    public void DetachTrasaction() {
59      this.transaction = null;
60    }
61
62    #region ISession Members
63    public ITransaction BeginTransaction() {
64      if (transaction == null) {
65         transaction = new Transaction(this);
66         transaction.Connection = Connection;
67      }
68
69      return transaction;
70    }
71
72    public ITransaction GetCurrentTransaction() {
73      return transaction;
74    }
75
76    public IDataAdapter<ObjT> GetDataAdapter<ObjT>()
77      where ObjT : IPersistableObject {
78      Guid adapterId = typeof(IDataAdapter<ObjT>).GUID;
79
80      if (!adapters.ContainsKey(adapterId)) {
81        IDataAdapter<ObjT> adapter =
82          discoveryService.GetInstances<IDataAdapter<ObjT>>()[0];
83
84        adapter.Session = this;
85
86        adapters.Add(adapterId, adapter);
87      }
88
89      return adapters[adapterId] as IDataAdapter<ObjT>;
90    }
91
92    public T GetDataAdapter<ObjT, T>()
93      where ObjT : IPersistableObject
94      where T : class, IDataAdapter<ObjT>
95    {
96        Guid adapterId = typeof(T).GUID;
97
98        if (!adapters.ContainsKey(adapterId)) {
99          T adapter =
100            discoveryService.GetInstances<T>()[0];
101
102          adapter.Session = this;
103
104          adapters.Add(adapterId, adapter);
105        }
106
107        return adapters[adapterId] as T;
108    }
109
110    public void EndSession() {
111      if (transaction != null) {
112        transaction.Rollback();
113        transaction = null;
114      }
115      if (connection.State == System.Data.ConnectionState.Open) {
116        connection.Close();
117        connection = null;
118      }
119      factory.EndSession(this);
120    }
121
122    #endregion
123  }
124}
Note: See TracBrowser for help on using the repository browser.