Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1888_OaaS/HeuristicLab.Services.Optimization.Billing/Business/TransactionManager.cs @ 16226

Last change on this file since 16226 was 10013, checked in by spimming, 11 years ago

#1888:

  • enabled transactions
  • enabled tracing output
  • set correct invoice data
  • saving invoices
File size: 2.4 KB
Line 
1using System;
2using System.Configuration;
3using System.Data.SqlClient;
4using System.Diagnostics;
5using System.Transactions;
6using HeuristicLab.Services.Optimization.Billing.Interfaces;
7
8namespace HeuristicLab.Services.Optimization.Billing.Business {
9  public class TransactionManager : ITransactionManager {
10    private const int retries = 10;
11    private readonly TimeSpan timeoutInterval;
12
13    public TransactionManager() {
14      timeoutInterval = TimeSpan.Parse(ConfigurationManager.AppSettings["LongRunningDbCommandTimeout"]);
15    }
16
17    public void UseTransaction(System.Action call, bool repeatableRead = false, bool longRunning = false) {
18      int n = retries;
19      while (n > 0) {
20        TransactionScope transaction = CreateTransaction(repeatableRead, longRunning);
21        try {
22          call();
23          transaction.Complete();
24          n = 0;
25        }
26        catch (SqlException ex) {
27          n--;
28          // TODO: log exception
29          Trace.WriteLine(string.Format("Exception occured, repeating transaction {0} more times: {1}", n, ex.ToString()));
30          if (n <= 0) throw;
31        }
32        finally {
33          transaction.Dispose();
34        }
35      }
36    }
37
38    public T UseTransaction<T>(System.Func<T> call, bool repeatableRead = false, bool longRunning = false) {
39      int n = retries;
40      while (n > 0) {
41        TransactionScope transaction = CreateTransaction(repeatableRead, longRunning);
42        try {
43          T result = call();
44          transaction.Complete();
45          n = 0;
46          return result;
47        }
48        catch (SqlException ex) {
49          n--;
50          // TODO: log exception
51          Trace.WriteLine(string.Format("Exception occured, repeating transaction {0} more times: {1}", n, ex.ToString()));
52          if (n <= 0) throw;
53        }
54        finally {
55          transaction.Dispose();
56        }
57      }
58      throw new Exception("This code should not be reached");
59    }
60
61    private TransactionScope CreateTransaction(bool repeatableRead, bool longRunning) {
62      var options = new TransactionOptions();
63      if (repeatableRead) {
64        options.IsolationLevel = IsolationLevel.RepeatableRead;
65      } else {
66        options.IsolationLevel = IsolationLevel.ReadUncommitted;
67      }
68
69      if (longRunning) {
70        options.Timeout = timeoutInterval;
71      }
72
73      return new TransactionScope(TransactionScopeOption.Required, options);
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.