Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataAccess.ADOHelper/Transaction.cs @ 1516

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

Improved transaction handling and locking (#527)

File size: 2.6 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.Data;
28using System.Data.Common;
29
30namespace HeuristicLab.DataAccess.ADOHelper {
31  class Transaction: ITransaction {
32    private DbTransaction transaction;
33
34    private Session session;
35
36    public Transaction(Session session) {
37      this.session = session;
38    }
39
40    public DbConnection Connection {
41      set {
42        if (value != null &&
43          (transaction == null ||
44           !(transaction.Connection != null &&
45             transaction.Connection.Equals(value)))) {
46          if (value.State != System.Data.ConnectionState.Open)
47            value.Open();
48
49          transaction = value.BeginTransaction(IsolationLevel.ReadCommitted);
50        }
51      }
52    }
53
54    #region ITransaction Members
55    public void Commit() {
56      this.session.CheckThread();
57
58      if (transaction != null) {
59        DbConnection conn =
60          transaction.Connection;
61
62        transaction.Commit();
63
64        transaction = null;
65        session.DetachTrasaction();
66
67        if (conn != null &&
68            conn.State == System.Data.ConnectionState.Open)
69          conn.Close();
70      }
71    }
72
73    public void Rollback() {
74      this.session.CheckThread();
75
76      if (transaction != null) {
77        DbConnection conn =
78            transaction.Connection;
79
80        transaction.Rollback();
81
82        transaction = null;
83        session.DetachTrasaction();
84
85        if (conn != null &&
86            conn.State == System.Data.ConnectionState.Open)
87          conn.Close();
88      }
89    }
90
91    public object InnerTransaction {
92      get {
93        this.session.CheckThread();
94
95        return transaction;
96      }
97    }
98
99    #endregion
100  }
101}
Note: See TracBrowser for help on using the repository browser.