Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3068 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
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.Data;
28using System.Data.Common;
29
30namespace HeuristicLab.DataAccess.ADOHelper {
31  class Transaction: ITransaction {
32    private DbTransaction transaction;
33
[1488]34    private Session session;
[1468]35
[2117]36    private int usageCounter = 0;
37
[2123]38    private TransactionIsolationLevel isolationLevel;
39
40    public Transaction(Session session, TransactionIsolationLevel isolationLevel) {
[1488]41      this.session = session;
[2123]42      this.isolationLevel = isolationLevel;
[1468]43    }
44
[2117]45    public void IncrementUsageCounter() {
46      this.usageCounter++;
47    }
48
[1468]49    public DbConnection Connection {
50      set {
51        if (value != null &&
52          (transaction == null ||
[1496]53           !(transaction.Connection != null &&
[1468]54             transaction.Connection.Equals(value)))) {
[1496]55          if (value.State != System.Data.ConnectionState.Open)
56            value.Open();
[1468]57
[2123]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);
[1468]70        }
71      }
72    }
73
[1496]74    #region ITransaction Members
[1468]75    public void Commit() {
[1496]76      this.session.CheckThread();
77
[2117]78      usageCounter--;
79
80      if (transaction != null && usageCounter <= 0) {
[1468]81        DbConnection conn =
82          transaction.Connection;
83
84        transaction.Commit();
85
[1488]86        transaction = null;
87        session.DetachTrasaction();
88
[1468]89        if (conn != null &&
90            conn.State == System.Data.ConnectionState.Open)
91          conn.Close();
92      }
93    }
94
95    public void Rollback() {
[1496]96      this.session.CheckThread();
97
[2117]98      usageCounter = 0;
99
[1488]100      if (transaction != null) {
101        DbConnection conn =
102            transaction.Connection;
[1468]103
[1488]104        transaction.Rollback();
[1468]105
[1488]106        transaction = null;
107        session.DetachTrasaction();
108
109        if (conn != null &&
110            conn.State == System.Data.ConnectionState.Open)
111          conn.Close();
112      }
[1468]113    }
114
115    public object InnerTransaction {
116      get {
[1496]117        this.session.CheckThread();
118
[1468]119        return transaction;
120      }
121    }
122
123    #endregion
124  }
125}
Note: See TracBrowser for help on using the repository browser.