- Timestamp:
- 06/19/11 23:21:21 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/TransactionManager.cs
r6431 r6444 27 27 public static class TransactionManager { 28 28 public static void UseTransaction(Action call, bool serializable = false, bool longRunning = false) { 29 TransactionScope transaction = CreateTransaction(serializable, longRunning); 30 try { 31 call(); 32 transaction.Complete(); 33 } 34 finally { 35 transaction.Dispose(); 29 int n = 10; 30 while (n > 0) { 31 TransactionScope transaction = CreateTransaction(serializable, longRunning); 32 try { 33 call(); 34 transaction.Complete(); 35 n = 0; 36 } 37 catch (System.Data.SqlClient.SqlException e) { 38 n--; // probably deadlock situation, let it roll back and repeat the transaction n times 39 LogFactory.GetLogger(typeof(TransactionManager).Namespace).Log(string.Format("Exception occured, repeating transaction {0} more times. Details: {1}", n, e.ToString())); 40 if (n <= 0) throw; 41 } 42 finally { 43 transaction.Dispose(); 44 } 36 45 } 37 46 } 38 47 39 48 public static T UseTransaction<T>(Func<T> call, bool serializable = false, bool longRunning = false) { 40 TransactionScope transaction = CreateTransaction(serializable, longRunning); 41 try { 42 T result = call(); 43 transaction.Complete(); 44 return result; 49 int n = 10; 50 while (n > 0) { 51 TransactionScope transaction = CreateTransaction(serializable, longRunning); 52 try { 53 T result = call(); 54 transaction.Complete(); 55 n = 0; 56 return result; 57 } 58 catch (System.Data.SqlClient.SqlException e) { 59 n--; // probably deadlock situation, let it roll back and repeat the transaction n times 60 LogFactory.GetLogger(typeof(TransactionManager).Namespace).Log(string.Format("Exception occured, repeating transaction {0} more times. Details: {1}", n, e.ToString())); 61 if (n <= 0) throw; 62 } 63 finally { 64 transaction.Dispose(); 65 } 45 66 } 46 finally { 47 transaction.Dispose(); 48 } 67 throw new Exception("This code should not be reached"); 49 68 } 50 69 51 70 private static TransactionScope CreateTransaction(bool serializable, bool longRunning) { 52 71 var options = new TransactionOptions(); … … 58 77 if (longRunning) 59 78 options.Timeout = ApplicationConstants.LongRunningDatabaseCommandTimeout; 60 79 61 80 return new TransactionScope(TransactionScopeOption.Required, options); 62 81 }
Note: See TracChangeset
for help on using the changeset viewer.