Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataAccess.ADOHelper/3.2/SessionFactory.cs @ 2472

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

Updated SessionFactory (#372)

File size: 3.1 KB
RevLine 
[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
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 {
[2095]33    private IDictionary<Thread, Session> sessions =
34      new Dictionary<Thread, Session>();
[1468]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] =
[1488]85            new Session(this);
[1468]86        }
87
[2096]88        sessions[current].IncrementUsageCounter();
[2095]89
[1468]90        return sessions[current];
91      }
92    }
93
94    public void EndSession(ISession session) {
95      lock (this) {
96        ICollection<Thread> toBeRemoved =
97          new List<Thread>();
98
99        foreach(Thread t in sessions.Keys) {
100          if(sessions[t].Equals(session)) {
101            toBeRemoved.Add(t);
102          }
103        }
104
105        foreach (Thread t in toBeRemoved) {
106          sessions.Remove(t);
107        }
108      }
109    }
110
111    #endregion
112  }
113}
Note: See TracBrowser for help on using the repository browser.