Changeset 4092 for branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server.LINQDataAccess/3.3/ContextFactory.cs
- Timestamp:
- 07/23/10 14:17:20 (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
r4074 r4092 3 3 using System.Linq; 4 4 using System.Text; 5 using System.Data.Common; 6 using System.Data.SqlClient; 5 using HeuristicLab.Hive.Server.DataAccess; 6 using System.Collections; 7 using System.Data.Linq; 8 using System.Transactions; 9 using HeuristicLab.PluginInfrastructure; 10 using HeuristicLab.Hive.Contracts; 7 11 using HeuristicLab.Tracing; 12 using System.Threading; 13 8 14 9 15 namespace HeuristicLab.Hive.Server.LINQDataAccess { 10 public class ContextFactory { 11 [ThreadStatic] 12 private static HiveDataContext _hiveDataContext = null; 16 /// <summary> 17 /// This class handles creates one Context for each Thread which asks for one. 18 /// If one Thread calls GetContext several times, it always gets the same Context object. 19 /// A context object is removed from the cache when it is disposed. 20 /// 21 /// Every context has a DB-Transaction over its livetime. After using the context object 22 /// it has to be disposed in order to finish the corresponding transaction. 23 /// </summary> 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>(); 13 27 14 [ThreadStatic] 15 private static SqlTransaction _currentTransaction = null; 16 17 public static HiveDataContext Context { 28 private static IContextFactory instance = null; 29 public static IContextFactory Instance { 18 30 get { 19 if (_hiveDataContext == null) { 20 Logger.Debug("Requested new Data Context"); 21 _hiveDataContext = new HiveDataContext("Data Source=127.0.0.1;Initial Catalog=HeuristicLab.Hive;Integrated Security=SSPI"); 22 _hiveDataContext.CommandTimeout = 240; 23 } 24 return _hiveDataContext; 25 } 26 set { 27 _hiveDataContext = value; 31 if (instance == null) 32 instance = new ContextFactory(); 33 return instance; 28 34 } 29 35 } 30 36 31 public static SqlTransaction CurrentTransaction { 32 get { 33 return _currentTransaction; 34 } set { 35 _currentTransaction = value; 36 } 37 #region IContextManager Members 38 39 public IDisposable GetContext() { 40 return GetContext(true); 37 41 } 38 42 39 public static bool IsContextNull() { 40 return _hiveDataContext == null; 43 public IDisposable GetContext(bool withTransaction) { 44 Logger.Debug("opening transaction"); 45 object obj = Thread.CurrentThread; 46 47 if (contexts.ContainsKey(obj)) { 48 throw new DataAccessException("Context for this Thread already defined"); 49 } 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; 41 63 } 64 65 private static DisposableHiveDataContext CreateContext() { 66 return new DisposableHiveDataContext("Data Source=127.0.0.1;Initial Catalog=HeuristicLab.Hive;Integrated Security=SSPI"); 67 } 68 69 void context_OnDisposing(object sender, EventArgs e) { 70 RemoveContext(); 71 RemoveAndCompleteTransaction(); 72 } 73 74 public void RollbackTransaction() { 75 Logger.Debug("rolling back transaction"); 76 object obj = Thread.CurrentThread; 77 78 TransactionScope transaction = transactions[obj]; 79 transaction.Dispose(); 80 } 81 82 public void RemoveContext() { 83 Logger.Debug("removing context"); 84 object obj = Thread.CurrentThread; 85 86 contexts.Remove(obj); 87 // context gets disposed implicitly, when it is used as IDisposable 88 } 89 90 public void RemoveAndCompleteTransaction() { 91 Logger.Debug("completing transaction"); 92 object obj = Thread.CurrentThread; 93 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); 99 } 100 } 101 102 public DataContext CurrentContext { 103 get { 104 object obj = Thread.CurrentThread; 105 if (contexts.ContainsKey(obj)) { 106 return contexts[obj]; 107 } else { 108 return null; 109 } 110 } 111 } 112 113 #endregion 114 } 115 116 /// <summary> 117 /// Offers an event when it gets disposed 118 /// </summary> 119 class DisposableHiveDataContext : HiveDataContext { 120 121 public DisposableHiveDataContext(string connection) : base(connection) { } 122 123 protected override void Dispose(bool disposing) { 124 base.Dispose(disposing); 125 if (OnDisposing != null) 126 OnDisposing(this, new EventArgs()); 127 } 128 129 public event EventHandler OnDisposing; 42 130 } 43 131 }
Note: See TracChangeset
for help on using the changeset viewer.