[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 |
|
---|
[2117] | 44 | private int usageCounter = 0;
|
---|
[2095] | 45 |
|
---|
[1468] | 46 | private IDictionary<Guid, object> adapters =
|
---|
| 47 | new Dictionary<Guid, object>();
|
---|
| 48 |
|
---|
[1488] | 49 | public Session(SessionFactory factory) {
|
---|
[1468] | 50 | this.factory = factory;
|
---|
[1496] | 51 | this.ownerThread = Thread.CurrentThread;
|
---|
[2117] | 52 | this.usageCounter = 0;
|
---|
[1468] | 53 | }
|
---|
| 54 |
|
---|
[1496] | 55 | public void CheckThread() {
|
---|
| 56 | if (!Thread.CurrentThread.Equals(ownerThread)) {
|
---|
| 57 | throw new Exception("Session is owned by another thread");
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[1468] | 61 | public DbConnection Connection {
|
---|
| 62 | get {
|
---|
| 63 | if (connection == null) {
|
---|
| 64 | connection = factory.CreateConnection();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | return connection;
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[1488] | 71 | public void DetachTrasaction() {
|
---|
| 72 | this.transaction = null;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[2096] | 75 | public void IncrementUsageCounter() {
|
---|
[2117] | 76 | this.usageCounter++;
|
---|
[2095] | 77 | }
|
---|
| 78 |
|
---|
[1468] | 79 | #region ISession Members
|
---|
[2117] | 80 | public ISessionFactory Factory {
|
---|
| 81 | get {
|
---|
| 82 | return this.factory;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[2123] | 86 | public ITransaction BeginTransaction(TransactionIsolationLevel isolationLevel) {
|
---|
[1496] | 87 | CheckThread();
|
---|
| 88 |
|
---|
[1488] | 89 | if (transaction == null) {
|
---|
[2123] | 90 | transaction = new Transaction(this, isolationLevel);
|
---|
| 91 | transaction.Connection = Connection;
|
---|
[1488] | 92 | }
|
---|
[1468] | 93 |
|
---|
[2117] | 94 | transaction.IncrementUsageCounter();
|
---|
| 95 |
|
---|
[1488] | 96 | return transaction;
|
---|
[1468] | 97 | }
|
---|
| 98 |
|
---|
[2123] | 99 | public ITransaction BeginTransaction() {
|
---|
| 100 | return BeginTransaction(TransactionIsolationLevel.Default);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[1488] | 103 | public ITransaction GetCurrentTransaction() {
|
---|
[1496] | 104 | CheckThread();
|
---|
| 105 |
|
---|
[1488] | 106 | return transaction;
|
---|
[1468] | 107 | }
|
---|
| 108 |
|
---|
| 109 | public IDataAdapter<ObjT> GetDataAdapter<ObjT>()
|
---|
| 110 | where ObjT : IPersistableObject {
|
---|
[1496] | 111 | CheckThread();
|
---|
| 112 |
|
---|
[1488] | 113 | Guid adapterId = typeof(IDataAdapter<ObjT>).GUID;
|
---|
[1468] | 114 |
|
---|
[1488] | 115 | if (!adapters.ContainsKey(adapterId)) {
|
---|
| 116 | IDataAdapter<ObjT> adapter =
|
---|
| 117 | discoveryService.GetInstances<IDataAdapter<ObjT>>()[0];
|
---|
[1468] | 118 |
|
---|
[1488] | 119 | adapter.Session = this;
|
---|
[1468] | 120 |
|
---|
[1488] | 121 | adapters.Add(adapterId, adapter);
|
---|
| 122 | }
|
---|
[1468] | 123 |
|
---|
[1488] | 124 | return adapters[adapterId] as IDataAdapter<ObjT>;
|
---|
[1468] | 125 | }
|
---|
| 126 |
|
---|
| 127 | public T GetDataAdapter<ObjT, T>()
|
---|
| 128 | where ObjT : IPersistableObject
|
---|
| 129 | where T : class, IDataAdapter<ObjT>
|
---|
| 130 | {
|
---|
[1496] | 131 | CheckThread();
|
---|
[1468] | 132 |
|
---|
[1496] | 133 | Guid adapterId = typeof(T).GUID;
|
---|
[1468] | 134 |
|
---|
[1496] | 135 | if (!adapters.ContainsKey(adapterId)) {
|
---|
| 136 | T adapter =
|
---|
| 137 | discoveryService.GetInstances<T>()[0];
|
---|
[1468] | 138 |
|
---|
[1496] | 139 | adapter.Session = this;
|
---|
[1468] | 140 |
|
---|
[1496] | 141 | adapters.Add(adapterId, adapter);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | return adapters[adapterId] as T;
|
---|
[1468] | 145 | }
|
---|
| 146 |
|
---|
| 147 | public void EndSession() {
|
---|
[2117] | 148 | this.usageCounter--;
|
---|
[1496] | 149 |
|
---|
[2117] | 150 | if (usageCounter <= 0) {
|
---|
[2095] | 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);
|
---|
[1488] | 162 | }
|
---|
[1468] | 163 | }
|
---|
| 164 |
|
---|
| 165 | #endregion
|
---|
| 166 | }
|
---|
| 167 | }
|
---|