[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.DataAccess.Interfaces;
|
---|
| 27 | using HeuristicLab.PluginInfrastructure;
|
---|
| 28 | using System.Data.Common;
|
---|
[1496] | 29 | using System.Threading;
|
---|
[1468] | 30 |
|
---|
| 31 | namespace HeuristicLab.DataAccess.ADOHelper {
|
---|
| 32 | public class Session: ISession {
|
---|
| 33 | private static DiscoveryService discoveryService =
|
---|
| 34 | new DiscoveryService();
|
---|
| 35 |
|
---|
| 36 | private SessionFactory factory;
|
---|
| 37 |
|
---|
[1488] | 38 | private Transaction transaction;
|
---|
[1468] | 39 |
|
---|
| 40 | private DbConnection connection;
|
---|
| 41 |
|
---|
[1496] | 42 | private Thread ownerThread;
|
---|
| 43 |
|
---|
[1468] | 44 | private IDictionary<Guid, object> adapters =
|
---|
| 45 | new Dictionary<Guid, object>();
|
---|
| 46 |
|
---|
[1488] | 47 | public Session(SessionFactory factory) {
|
---|
[1468] | 48 | this.factory = factory;
|
---|
[1496] | 49 | this.ownerThread = Thread.CurrentThread;
|
---|
[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 |
|
---|
[1468] | 72 | #region ISession Members
|
---|
| 73 | public ITransaction BeginTransaction() {
|
---|
[1496] | 74 | CheckThread();
|
---|
| 75 |
|
---|
[1488] | 76 | if (transaction == null) {
|
---|
| 77 | transaction = new Transaction(this);
|
---|
| 78 | transaction.Connection = Connection;
|
---|
| 79 | }
|
---|
[1468] | 80 |
|
---|
[1488] | 81 | return transaction;
|
---|
[1468] | 82 | }
|
---|
| 83 |
|
---|
[1488] | 84 | public ITransaction GetCurrentTransaction() {
|
---|
[1496] | 85 | CheckThread();
|
---|
| 86 |
|
---|
[1488] | 87 | return transaction;
|
---|
[1468] | 88 | }
|
---|
| 89 |
|
---|
| 90 | public IDataAdapter<ObjT> GetDataAdapter<ObjT>()
|
---|
| 91 | where ObjT : IPersistableObject {
|
---|
[1496] | 92 | CheckThread();
|
---|
| 93 |
|
---|
[1488] | 94 | Guid adapterId = typeof(IDataAdapter<ObjT>).GUID;
|
---|
[1468] | 95 |
|
---|
[1488] | 96 | if (!adapters.ContainsKey(adapterId)) {
|
---|
| 97 | IDataAdapter<ObjT> adapter =
|
---|
| 98 | discoveryService.GetInstances<IDataAdapter<ObjT>>()[0];
|
---|
[1468] | 99 |
|
---|
[1488] | 100 | adapter.Session = this;
|
---|
[1468] | 101 |
|
---|
[1488] | 102 | adapters.Add(adapterId, adapter);
|
---|
| 103 | }
|
---|
[1468] | 104 |
|
---|
[1488] | 105 | return adapters[adapterId] as IDataAdapter<ObjT>;
|
---|
[1468] | 106 | }
|
---|
| 107 |
|
---|
| 108 | public T GetDataAdapter<ObjT, T>()
|
---|
| 109 | where ObjT : IPersistableObject
|
---|
| 110 | where T : class, IDataAdapter<ObjT>
|
---|
| 111 | {
|
---|
[1496] | 112 | CheckThread();
|
---|
[1468] | 113 |
|
---|
[1496] | 114 | Guid adapterId = typeof(T).GUID;
|
---|
[1468] | 115 |
|
---|
[1496] | 116 | if (!adapters.ContainsKey(adapterId)) {
|
---|
| 117 | T adapter =
|
---|
| 118 | discoveryService.GetInstances<T>()[0];
|
---|
[1468] | 119 |
|
---|
[1496] | 120 | adapter.Session = this;
|
---|
[1468] | 121 |
|
---|
[1496] | 122 | adapters.Add(adapterId, adapter);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | return adapters[adapterId] as T;
|
---|
[1468] | 126 | }
|
---|
| 127 |
|
---|
| 128 | public void EndSession() {
|
---|
[1496] | 129 | CheckThread();
|
---|
| 130 |
|
---|
[1488] | 131 | if (transaction != null) {
|
---|
| 132 | transaction.Rollback();
|
---|
| 133 | transaction = null;
|
---|
| 134 | }
|
---|
| 135 | if (connection.State == System.Data.ConnectionState.Open) {
|
---|
[1468] | 136 | connection.Close();
|
---|
[1488] | 137 | connection = null;
|
---|
| 138 | }
|
---|
[1468] | 139 | factory.EndSession(this);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | #endregion
|
---|
| 143 | }
|
---|
| 144 | }
|
---|