Example #1
0
        /// <summary>
        /// Gets an instance of a DataContext.
        /// </summary>
        /// <returns></returns>
        internal DataContext GetDataContext()
        {
            string threadDataKey = "SqlDataContext" + _providerName;

            var threadData = ThreadDataManager.GetCurrentNotNull();

            if (threadData.HasValue(threadDataKey))
            {
                DataContext result = Verify.ResultNotNull(threadData[threadDataKey] as DataContext);

                // In a result of a flush, data context type can be changed
                if (result.GetType().GUID == DataContextClass.GUID)
                {
                    return(result);
                }
            }

            DataContext dataContext = CreateDataContext();

            dataContext.ObjectTrackingEnabled = false;

            threadData.OnDispose += dataContext.Dispose;

            threadData.SetValue(threadDataKey, dataContext);

            if (_sqlLoggingContext.Enabled)
            {
                dataContext.Log = new SqlLoggerTextWriter(_sqlLoggingContext);
            }

            return(dataContext);
        }
        internal static IDisposable EnsureThreadDataServiceScope()
        {
            if (RequestScopedServiceProvider != null || ServiceLocatorNotInitialized)
            {
                return(EmptyDisposable.Instance);
            }

            var current = ThreadDataManager.GetCurrentNotNull();

            var serviceScopeFactory = (IServiceScopeFactory)_serviceProvider.GetService(typeof(IServiceScopeFactory));
            var serviceScope        = serviceScopeFactory.CreateScope();

            current.SetValue(ThreadDataKey, serviceScope);

            return(new ThreadDataServiceScopeDisposable(current));
        }
Example #3
0
        /// <exclude />
        public static Measurement EndProfiling()
        {
            var threadData = ThreadDataManager.GetCurrentNotNull();

            var stack = threadData[ProfilerKey] as Stack <Measurement>;

            Verify.That(stack.Count == 1, "Performance node stack should have exactly one (the root) node");

            threadData.SetValue(ProfilerKey, null);

            var measurement = stack.Pop();

            measurement.MemoryUsage = GC.GetTotalMemory(false) - measurement.MemoryUsage;

            return(measurement);
        }
        private static void SubscribeToTransactionRollbackEvent()
        {
            var transaction = Transaction.Current;

            if (transaction == null)
            {
                return;
            }

            var currentThreadData = ThreadDataManager.GetCurrentNotNull();

            Hashset <string> transactions;

            const string tlsKey = "XmlDataProvider enlisted transactions";

            if (!currentThreadData.HasValue(tlsKey))
            {
                transactions = new Hashset <string>();
                currentThreadData.SetValue(tlsKey, transactions);
            }
            else
            {
                transactions = (Hashset <string>)currentThreadData[tlsKey];
            }

            string transactionId = transaction.TransactionInformation.LocalIdentifier;

            if (transactions.Contains(transactionId))
            {
                return;
            }

            transactions.Add(transactionId);


            ThreadStart logging = () =>
            {
                var exception = new TransactionException("XML data provider does not support transaction's API, changes were not rolled back.");
                Log.LogWarning(LogTitle, exception);
            };

            transaction.EnlistVolatile(new TransactionRollbackHandler(logging), EnlistmentOptions.None);
        }
Example #5
0
        /// <exclude />
        public static SqlConnection GetConnection(string connectionString)
        {
            string threadDataKey = "SqlDataContext" + connectionString + "_SqlConnection";

            var threadData = ThreadDataManager.GetCurrentNotNull();

            SqlConnection connection;

            if (threadData.HasValue(threadDataKey))
            {
                connection = threadData[threadDataKey] as SqlConnection;
            }
            else
            {
                connection = new SqlConnection(connectionString);
                connection.Open();

                threadData.SetValue(threadDataKey, connection);
                threadData.OnDispose += connection.Close;
            }
            return(connection);
        }