Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3021 was 2606, checked in by kgrading, 15 years ago

changed the isolation level of the binary stream and added output to PersistableObject (#828)

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