[6983] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11576] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6983] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Transactions;
|
---|
| 24 |
|
---|
[9257] | 25 | namespace HeuristicLab.Services.Hive.DataAccess {
|
---|
| 26 | public class TransactionManager : ITransactionManager {
|
---|
| 27 | public void UseTransaction(Action call, bool repeatableRead = false, bool longRunning = false) {
|
---|
[6983] | 28 | int n = 10;
|
---|
| 29 | while (n > 0) {
|
---|
[9257] | 30 | TransactionScope transaction = CreateTransaction(repeatableRead, longRunning);
|
---|
[6983] | 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
|
---|
[9257] | 38 | LogFactory.GetLogger(typeof(TransactionManager).Namespace).Log(string.Format("Exception occured, repeating transaction {0} more times. Details: {1}", n, e.ToString()));
|
---|
[6983] | 39 | if (n <= 0) throw;
|
---|
| 40 | }
|
---|
| 41 | finally {
|
---|
| 42 | transaction.Dispose();
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[9257] | 47 | public T UseTransaction<T>(Func<T> call, bool repeatableRead = false, bool longRunning = false) {
|
---|
[6983] | 48 | int n = 10;
|
---|
| 49 | while (n > 0) {
|
---|
[9257] | 50 | TransactionScope transaction = CreateTransaction(repeatableRead, longRunning);
|
---|
[6983] | 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 |
|
---|
[9257] | 69 | private TransactionScope CreateTransaction(bool repeatableRead, bool longRunning) {
|
---|
[6983] | 70 | var options = new TransactionOptions();
|
---|
[9257] | 71 | if (repeatableRead)
|
---|
| 72 | options.IsolationLevel = IsolationLevel.RepeatableRead;
|
---|
[6983] | 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 | }
|
---|