Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.DataAccess.ADOHelper/3.2/SessionFactory.cs @ 6049

Last change on this file since 6049 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 3.0 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 System.Threading;
28using System.Timers;
29using System.Data.Common;
30
31namespace HeuristicLab.DataAccess.ADOHelper {
32  public class SessionFactory: ISessionFactory {
33    private IDictionary<Thread, ISession> sessions =
34      new Dictionary<Thread, ISession>();
35
36    public Type DbConnectionType { get;  set; }
37    public String DbConnectionString { get;  set; }
38
39    public SessionFactory() {
40      System.Timers.Timer timer =
41        new System.Timers.Timer();
42
43      TimeSpan interval =
44        new TimeSpan(0, 1, 0);
45
46      timer.Interval = interval.TotalMilliseconds;
47      timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
48      timer.Start();
49    }
50
51    void timer_Elapsed(object sender, ElapsedEventArgs e) {
52      lock (this) {
53        ICollection<Thread> toBeRemoved =
54          new List<Thread>();
55
56        foreach (Thread t in sessions.Keys) {
57          if (t.ThreadState == ThreadState.Stopped) {
58            toBeRemoved.Add(t);
59          }
60        }
61
62        foreach (Thread t in toBeRemoved) {
63          sessions.Remove(t);
64        }
65      }
66    }
67
68    public DbConnection CreateConnection() {
69      DbConnection conn =
70        Activator.CreateInstance(DbConnectionType) as DbConnection;
71
72      conn.ConnectionString = DbConnectionString;
73
74      return conn;
75    }
76
77    #region ISessionFactory Members
78
79    public ISession GetSessionForCurrentThread() {
80      lock (this) {
81        Thread current = Thread.CurrentThread;
82
83        if (!sessions.ContainsKey(current)) {
84          sessions[current] =
85            new Session(this);
86        }
87
88        return sessions[current];
89      }
90    }
91
92    public void EndSession(ISession session) {
93      lock (this) {
94        ICollection<Thread> toBeRemoved =
95          new List<Thread>();
96
97        foreach(Thread t in sessions.Keys) {
98          if(sessions[t].Equals(session)) {
99            toBeRemoved.Add(t);
100          }
101        }
102
103        foreach (Thread t in toBeRemoved) {
104          sessions.Remove(t);
105        }
106      }
107    }
108
109    #endregion
110  }
111}
Note: See TracBrowser for help on using the repository browser.