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