Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/sources/HeuristicLab.DataAccess.ADOHelper/3.2/Transaction.cs @ 3494

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

Avoided possible race conditions when streaming data from/into the DB (#680)

File size: 3.8 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    private int usageCounter = 0;
37
38    private TransactionIsolationLevel isolationLevel;
39
40    public Transaction(Session session, TransactionIsolationLevel isolationLevel) {
41      this.session = session;
42      this.isolationLevel = isolationLevel;
43    }
44
45    public void IncrementUsageCounter() {
46      this.usageCounter++;
47    }
48
49    public DbConnection Connection {
50      set {
51        if (value != null &&
52          (transaction == null ||
53           !(transaction.Connection != null &&
54             transaction.Connection.Equals(value)))) {
55          if (value.State != System.Data.ConnectionState.Open)
56            value.Open();
57
58          if (isolationLevel == TransactionIsolationLevel.Default)
59            transaction = value.BeginTransaction(IsolationLevel.ReadCommitted);
60          else if (isolationLevel == TransactionIsolationLevel.ReadUncommitted)
61            transaction = value.BeginTransaction(IsolationLevel.ReadUncommitted);
62          else if (isolationLevel == TransactionIsolationLevel.ReadCommitted)
63            transaction = value.BeginTransaction(IsolationLevel.ReadCommitted);
64          else if (isolationLevel == TransactionIsolationLevel.RepeatableRead)
65            transaction = value.BeginTransaction(IsolationLevel.RepeatableRead);
66          else if (isolationLevel == TransactionIsolationLevel.Serializable)
67            transaction = value.BeginTransaction(IsolationLevel.Serializable);
68          else
69            transaction = value.BeginTransaction(IsolationLevel.ReadCommitted);
70        }
71      }
72    }
73
74    #region ITransaction Members
75    public void Commit() {
76      this.session.CheckThread();
77
78      usageCounter--;
79
80      if (transaction != null && usageCounter <= 0) {
81        DbConnection conn =
82          transaction.Connection;
83
84        transaction.Commit();
85
86        transaction = null;
87        session.DetachTrasaction();
88
89        if (conn != null &&
90            conn.State == System.Data.ConnectionState.Open)
91          conn.Close();
92      }
93    }
94
95    public void Rollback() {
96      this.session.CheckThread();
97
98      usageCounter = 0;
99
100      if (transaction != null) {
101        DbConnection conn =
102            transaction.Connection;
103
104        transaction.Rollback();
105
106        transaction = null;
107        session.DetachTrasaction();
108
109        if (conn != null &&
110            conn.State == System.Data.ConnectionState.Open)
111          conn.Close();
112      }
113    }
114
115    public object InnerTransaction {
116      get {
117        this.session.CheckThread();
118
119        return transaction;
120      }
121    }
122
123    #endregion
124  }
125}
Note: See TracBrowser for help on using the repository browser.