Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataAccess.ADOHelper/TransactionManager.cs @ 1468

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

Added transaction management (#527)

File size: 3.2 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 System.Timers;
27using HeuristicLab.DataAccess.Interfaces;
28using System.Threading;
29using System.Data;
30
31namespace HeuristicLab.DataAccess.ADOHelper {
32  class TransactionManager: ITransactionManager {
33    private IDictionary<Thread, ITransaction> transactions =
34      new Dictionary<Thread, ITransaction>();
35
36    public TransactionManager() {
37      System.Timers.Timer timer =
38        new System.Timers.Timer();
39
40      TimeSpan interval =
41        new TimeSpan(0, 1, 0);
42
43      timer.Interval = interval.TotalMilliseconds;
44      timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
45      timer.Start();
46    }
47
48    void timer_Elapsed(object sender, ElapsedEventArgs e) {
49      lock (this) {
50        ICollection<Thread> tobeRemoved =
51          new List<Thread>();
52
53        foreach (Thread t in transactions.Keys) {
54          if (t.ThreadState == ThreadState.Stopped) {
55            try {
56              transactions[t].Rollback();
57            }
58            catch (Exception ex) {
59              //TODO: Error handling
60            }
61            finally {
62              tobeRemoved.Add(t);
63            }
64          }
65        }
66
67        foreach (Thread t in tobeRemoved) {
68          transactions.Remove(t);
69        }
70      }
71    }
72
73    #region ITransactionManager Members
74    public ITransaction BeginTransaction() {
75      lock (this) {
76        Thread current = Thread.CurrentThread;
77
78        if (!transactions.ContainsKey(current)) {
79          ITransaction trans = new Transaction(this);
80          transactions[current] = trans;
81        }
82
83        return transactions[current];
84      }
85    }
86
87    public ITransaction GetTransactionForCurrentThread() {
88      lock (this) {
89        Thread current = Thread.CurrentThread;
90
91        if(transactions.ContainsKey(current))
92          return transactions[current];
93        else
94          return null;
95      }
96    }
97
98    internal void RemoveTransaction(ITransaction trans) {
99      lock (this) {
100        ICollection<Thread> tobeRemoved =
101          new List<Thread>();
102
103        foreach (Thread t in transactions.Keys) {
104          if (transactions[t].Equals(trans)) {
105            tobeRemoved.Add(t);
106          }
107        }
108
109        foreach (Thread t in tobeRemoved) {
110          transactions.Remove(t);
111        }
112      }
113    }
114
115    #endregion
116  }
117}
Note: See TracBrowser for help on using the repository browser.