Changeset 4120 for branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server.LINQDataAccess/3.3/ContextFactory.cs
- Timestamp:
- 07/29/10 16:58:35 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server.LINQDataAccess/3.3/ContextFactory.cs
r4092 r4120 23 23 /// </summary> 24 24 class ContextFactory : IContextFactory { 25 private static IDictionary<object, HiveDataContext> contexts = new Dictionary<object, HiveDataContext>(); 26 private static IDictionary<object, TransactionScope> transactions = new Dictionary<object, TransactionScope>(); 25 private static object locker = new object(); 26 private static IDictionary<int, HiveDataContext> contexts = new Dictionary<int, HiveDataContext>(); 27 private static IDictionary<int, TransactionScope> transactions = new Dictionary<int, TransactionScope>(); 27 28 28 29 private static IContextFactory instance = null; … … 42 43 43 44 public IDisposable GetContext(bool withTransaction) { 44 Logger.Debug("opening transaction"); 45 object obj = Thread.CurrentThread; 45 lock (locker) { 46 Logger.Debug("opening transaction"); 47 int threadId = Thread.CurrentThread.ManagedThreadId; 46 48 47 if (contexts.ContainsKey(obj)) { 48 throw new DataAccessException("Context for this Thread already defined"); 49 if (contexts.ContainsKey(threadId)) { 50 throw new DataAccessException("Context for this Thread already defined"); 51 } 52 53 Logger.Debug("creating context"); 54 DisposableHiveDataContext context = CreateContext(); 55 context.OnDisposing += new EventHandler(context_OnDisposing); 56 contexts.Add(threadId, context); 57 58 if (withTransaction) { 59 TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE }); 60 Logger.Debug("creating transaction"); 61 62 transactions.Add(threadId, transaction); 63 } 64 return context; 49 65 } 50 51 Logger.Debug("creating context");52 DisposableHiveDataContext context = CreateContext();53 context.OnDisposing += new EventHandler(context_OnDisposing);54 contexts.Add(obj, context);55 56 if (withTransaction) {57 TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE });58 Logger.Debug("creating transaction");59 60 transactions.Add(obj, transaction);61 }62 return context;63 66 } 64 67 … … 74 77 public void RollbackTransaction() { 75 78 Logger.Debug("rolling back transaction"); 76 object obj = Thread.CurrentThread;79 int threadId = Thread.CurrentThread.ManagedThreadId; 77 80 78 TransactionScope transaction = transactions[ obj];81 TransactionScope transaction = transactions[threadId]; 79 82 transaction.Dispose(); 80 83 } 81 84 82 85 public void RemoveContext() { 83 Logger.Debug("removing context"); 84 object obj = Thread.CurrentThread; 86 lock (locker) { 87 Logger.Debug("removing context"); 88 int threadId = Thread.CurrentThread.ManagedThreadId; 85 89 86 contexts.Remove(obj); 87 // context gets disposed implicitly, when it is used as IDisposable 90 contexts.Remove(threadId); 91 // context gets disposed implicitly, when it is used as IDisposable 92 } 88 93 } 89 94 90 95 public void RemoveAndCompleteTransaction() { 91 Logger.Debug("completing transaction"); 92 object obj = Thread.CurrentThread; 96 lock (locker) { 97 Logger.Debug("completing transaction"); 98 int threadId = Thread.CurrentThread.ManagedThreadId; 93 99 94 // context does not always have an associated transaction 95 if (transactions.ContainsKey(obj)) { 96 transactions[obj].Complete(); 97 transactions[obj].Dispose(); 98 transactions.Remove(obj); 100 // context does not always have an associated transaction 101 if (transactions.ContainsKey(threadId)) { 102 transactions[threadId].Complete(); 103 transactions[threadId].Dispose(); 104 transactions.Remove(threadId); 105 } 99 106 } 100 107 } … … 102 109 public DataContext CurrentContext { 103 110 get { 104 object obj = Thread.CurrentThread;105 if (contexts.ContainsKey( obj)) {106 return contexts[ obj];111 int threadId = Thread.CurrentThread.ManagedThreadId; 112 if (contexts.ContainsKey(threadId)) { 113 return contexts[threadId]; 107 114 } else { 108 115 return null;
Note: See TracChangeset
for help on using the changeset viewer.