Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/29/10 16:58:35 (14 years ago)
Author:
cneumuel
Message:

further improvement and stabilisation of HiveExperiment (#1115)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server.LINQDataAccess/3.3/ContextFactory.cs

    r4092 r4120  
    2323  /// </summary>
    2424  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>();
    2728
    2829    private static IContextFactory instance = null;
     
    4243
    4344    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;
    4648
    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;
    4965      }
    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;
    6366    }
    6467
     
    7477    public void RollbackTransaction() {
    7578      Logger.Debug("rolling back transaction");
    76       object obj = Thread.CurrentThread;
     79      int threadId = Thread.CurrentThread.ManagedThreadId;
    7780
    78       TransactionScope transaction = transactions[obj];
     81      TransactionScope transaction = transactions[threadId];
    7982      transaction.Dispose();
    8083    }
    8184
    8285    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;
    8589
    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      }
    8893    }
    8994
    9095    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;
    9399
    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        }
    99106      }
    100107    }
     
    102109    public DataContext CurrentContext {
    103110      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];
    107114        } else {
    108115          return null;
Note: See TracChangeset for help on using the changeset viewer.