Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1888_OaaS/HeuristicLab.Services.Hive.DataAccess/3.3/TransactionManager.cs @ 16189

Last change on this file since 16189 was 9363, checked in by spimming, 12 years ago

#1888:

  • Merged revisions from trunk
File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Transactions;
24
25namespace HeuristicLab.Services.Hive.DataAccess {
26  public class TransactionManager : ITransactionManager {
27    public void UseTransaction(Action call, bool repeatableRead = false, bool longRunning = false) {
28      int n = 10;
29      while (n > 0) {
30        TransactionScope transaction = CreateTransaction(repeatableRead, longRunning);
31        try {
32          call();
33          transaction.Complete();
34          n = 0;
35        }
36        catch (System.Data.SqlClient.SqlException e) {
37          n--; // probably deadlock situation, let it roll back and repeat the transaction n times
38          LogFactory.GetLogger(typeof(TransactionManager).Namespace).Log(string.Format("Exception occured, repeating transaction {0} more times. Details: {1}", n, e.ToString()));
39          if (n <= 0) throw;
40        }
41        finally {
42          transaction.Dispose();
43        }
44      }
45    }
46
47    public T UseTransaction<T>(Func<T> call, bool repeatableRead = false, bool longRunning = false) {
48      int n = 10;
49      while (n > 0) {
50        TransactionScope transaction = CreateTransaction(repeatableRead, longRunning);
51        try {
52          T result = call();
53          transaction.Complete();
54          n = 0;
55          return result;
56        }
57        catch (System.Data.SqlClient.SqlException e) {
58          n--; // probably deadlock situation, let it roll back and repeat the transaction n times
59          LogFactory.GetLogger(typeof(TransactionManager).Namespace).Log(string.Format("Exception occured, repeating transaction {0} more times. Details: {1}", n, e.ToString()));
60          if (n <= 0) throw;
61        }
62        finally {
63          transaction.Dispose();
64        }
65      }
66      throw new Exception("This code should not be reached");
67    }
68
69    private TransactionScope CreateTransaction(bool repeatableRead, bool longRunning) {
70      var options = new TransactionOptions();
71      if (repeatableRead)
72        options.IsolationLevel = IsolationLevel.RepeatableRead;
73      else
74        options.IsolationLevel = IsolationLevel.ReadUncommitted;
75
76      if (longRunning)
77        options.Timeout = Settings.Default.LongRunningDatabaseCommandTimeout;
78
79      return new TransactionScope(TransactionScopeOption.Required, options);
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.