[10013] | 1 | using System;
|
---|
| 2 | using System.Configuration;
|
---|
| 3 | using System.Data.SqlClient;
|
---|
| 4 | using System.Diagnostics;
|
---|
| 5 | using System.Transactions;
|
---|
| 6 | using HeuristicLab.Services.Optimization.Billing.Interfaces;
|
---|
| 7 |
|
---|
| 8 | namespace 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 | }
|
---|