public void NoMatchingProviderDefinedInThreadLocalStorage()
        {
            IDbProvider provider = DbProviderFactory.GetDbProvider("System.Data.SqlClient");

            provider.ConnectionString = "connString1";
            MultiDelegatingDbProvider multiDbProvider = new MultiDelegatingDbProvider();
            IDictionary targetDbProviders             = new Hashtable();

            targetDbProviders.Add("db1", provider);
            multiDbProvider.TargetDbProviders = targetDbProviders;
            multiDbProvider.AfterPropertiesSet();
            try
            {
                MultiDelegatingDbProvider.CurrentDbProviderName = "db2";
                Assert.AreEqual("connString1", multiDbProvider.ConnectionString);
                Assert.Fail("InvalidDataAccessApiUsageException should have been thrown");
            }
            catch (InvalidDataAccessApiUsageException exception)
            {
                Assert.AreEqual("'db2' was not under the thread local key 'dbProviderName' and no default IDbProvider was set.", exception.Message);
            }
            finally
            {
                LogicalThreadContext.FreeNamedDataSlot(MultiDelegatingDbProvider.CURRENT_DBPROVIDER_SLOTNAME);
            }
        }
        public void NoDefaultProvided()
        {
            IDbProvider provider = DbProviderFactory.GetDbProvider("System.Data.SqlClient");

            provider.ConnectionString = "connString1";
            MultiDelegatingDbProvider multiDbProvider = new MultiDelegatingDbProvider();
            IDictionary targetDbProviders             = new Hashtable();

            targetDbProviders.Add("db1", provider);
            multiDbProvider.TargetDbProviders = targetDbProviders;
            multiDbProvider.AfterPropertiesSet();
            try
            {
                Assert.AreEqual("connString1", multiDbProvider.ConnectionString);
                Assert.Fail("InvalidDataAccessApiUsageException should have been thrown");
            }
            catch (InvalidDataAccessApiUsageException exception)
            {
                Assert.AreEqual("No provider name found in thread local storage.  Consider setting the property DefaultDbProvider to fallback to a default value.", exception.Message);
            }
            finally
            {
                LogicalThreadContext.FreeNamedDataSlot(MultiDelegatingDbProvider.CURRENT_DBPROVIDER_SLOTNAME);
            }
        }
        /// <summary>
        /// Unbind a resource for the given key from the current thread
        /// </summary>
        /// <param name="key">key to check</param>
        /// <returns>the previously bound value</returns>
        /// <exception cref="InvalidOperationException">if there is no value bound to the thread</exception>
        public static object UnbindResource(Object key)
        {
            AssertUtils.ArgumentNotNull(key, "Key must not be null");

            IDictionary resources = LogicalThreadContext.GetData(resourcesDataSlotName) as IDictionary;

            if (resources == null || !resources.Contains(key))
            {
                throw new InvalidOperationException("No value for key [" + key + "] bound to thread [" +
                                                    SystemUtils.ThreadId + "]");
            }
            Object val = resources[key];

            resources.Remove(key);
            if (resources.Count == 0)
            {
                LogicalThreadContext.FreeNamedDataSlot(resourcesDataSlotName);
            }
            if (LOG.IsDebugEnabled)
            {
                LOG.Debug("Removed value [" + Describe(val) + "] for key [" + Describe(key) + "] from thread [" +
                          SystemUtils.ThreadId + "]");
            }
            return(val);
        }
        public void FallbackToDefault()
        {
            IDbProvider provider1 = DbProviderFactory.GetDbProvider("System.Data.SqlClient");

            provider1.ConnectionString = "connString1";
            IDbProvider provider2 = DbProviderFactory.GetDbProvider("System.Data.SqlClient");

            provider2.ConnectionString = "connString2";
            MultiDelegatingDbProvider multiDbProvider = new MultiDelegatingDbProvider();
            IDictionary targetDbProviders             = new Hashtable();

            targetDbProviders.Add("db1", provider1);
            targetDbProviders.Add("db2", provider2);
            multiDbProvider.DefaultDbProvider = provider1;
            multiDbProvider.TargetDbProviders = targetDbProviders;
            multiDbProvider.AfterPropertiesSet();

            MultiDelegatingDbProvider.CurrentDbProviderName = "db314";
            try
            {
                Assert.AreEqual("connString1", multiDbProvider.ConnectionString);
            }
            finally
            {
                LogicalThreadContext.FreeNamedDataSlot(MultiDelegatingDbProvider.CURRENT_DBPROVIDER_SLOTNAME);
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Sets a flag, whether this scope is in "open" state on the current logical thread.
 /// </summary>
 private void SetOpen(bool isOpen)
 {
     if (isOpen)
     {
         LogicalThreadContext.SetData(ISOPEN_KEY, ISOPEN_KEY);
     }
     else
     {
         LogicalThreadContext.FreeNamedDataSlot(ISOPEN_KEY);
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// Gets/Sets a flag, whether this scope manages it's own session for the current logical thread or not.
 /// </summary>
 /// <value><c>false</c> if session is managed by this module. <c>false</c> otherwise</value>
 private void SetParticipating(bool participating)
 {
     if (participating)
     {
         LogicalThreadContext.SetData(PARTICIPATE_KEY, PARTICIPATE_KEY);
     }
     else
     {
         LogicalThreadContext.FreeNamedDataSlot(PARTICIPATE_KEY);
     }
 }
 /// <summary>
 /// Deactivate transaction synchronization for the current thread.
 /// </summary>
 /// <remarks>
 /// Called by transaction manager on transaction cleanup.
 /// </remarks>
 /// <exception cref="System.InvalidOperationException">
 /// If synchronization is not active.
 /// </exception>
 public static void ClearSynchronization()
 {
     if (!SynchronizationActive)
     {
         throw new InvalidOperationException("Cannot deactivate transaction synchronization - not active");
     }
     if (LOG.IsDebugEnabled)
     {
         LOG.Debug("Clearing transaction synchronization");
     }
     LogicalThreadContext.FreeNamedDataSlot(syncsDataSlotName);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Process the xml file in the given location, and schedule all of the
 /// jobs defined within it.
 /// </summary>
 /// <param name="fileName">Name of the file.</param>
 /// <param name="systemId">The system id.</param>
 /// <param name="sched">The sched.</param>
 public virtual async Task ProcessFileAndScheduleJobs(string fileName, string systemId, IScheduler sched)
 {
     LogicalThreadContext.SetData(ThreadLocalKeyScheduler, sched);
     try
     {
         await ProcessFile(fileName, systemId).ConfigureAwait(false);
         await ExecutePreProcessCommands(sched).ConfigureAwait(false);
         await ScheduleJobs(sched).ConfigureAwait(false);
     }
     finally
     {
         LogicalThreadContext.FreeNamedDataSlot(ThreadLocalKeyScheduler);
     }
 }
Exemplo n.º 9
0
 /// <summary>
 /// Process the xml file in the given location, and schedule all of the
 /// jobs defined within it.
 /// </summary>
 /// <param name="fileName">Name of the file.</param>
 /// <param name="systemId">The system id.</param>
 /// <param name="sched">The sched.</param>
 /// <param name="overwriteExistingJobs">if set to <c>true</c> [over write existing jobs].</param>
 public virtual void ProcessFileAndScheduleJobs(string fileName, string systemId, IScheduler sched,
                                                bool overwriteExistingJobs)
 {
     LogicalThreadContext.SetData(ThreadLocalKeyScheduler, sched);
     try
     {
         ProcessFile(fileName, systemId);
         ScheduleJobs(ScheduledJobs, sched, overwriteExistingJobs);
     }
     finally
     {
         LogicalThreadContext.FreeNamedDataSlot(ThreadLocalKeyScheduler);
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// 从当前线程的UnitOfWork栈中移除顶部的对象
        /// 如果当前线程中不存在事务栈,则返回<see langword="null"></see>
        /// </summary>
        /// <returns>The UnitOfWork which at the top of Stack in the current thread.</returns>
        public static IUnitOfWork Pop()
        {
            if (ThreadBoundUnitOfWorkStack == null)
            {
                return(null);
            }
            IUnitOfWork unitOfWork = ThreadBoundUnitOfWorkStack.Pop();

            if (ThreadBoundUnitOfWorkStack.Count == 0)
            {
                LogicalThreadContext.FreeNamedDataSlot(UnitOfWorkStack);
            }
            return(unitOfWork);
        }
 /// <summary>
 /// Process the xml file in the given location, and schedule all of the
 /// jobs defined within it.
 /// </summary>
 /// <param name="fileName">Name of the file.</param>
 /// <param name="systemId">The system id.</param>
 /// <param name="sched">The sched.</param>
 public virtual void ProcessFileAndScheduleJobs(string fileName, string systemId, IScheduler sched)
 {
     LogicalThreadContext.SetData(ThreadLocalKeyScheduler, sched);
     try
     {
         ProcessFile(fileName, systemId);
         ExecutePreProcessCommands(sched);
         ScheduleJobs(sched);
     }
     finally
     {
         LogicalThreadContext.FreeNamedDataSlot(ThreadLocalKeyScheduler);
     }
 }
 /// <summary>
 /// Process the xml file in the given location, and schedule all of the
 /// jobs defined within it.
 /// </summary>
 /// <param name="stream">stream to read XML data from.</param>
 /// <param name="sched">The sched.</param>
 public virtual void ProcessStreamAndScheduleJobs(Stream stream, IScheduler sched)
 {
     LogicalThreadContext.SetData(ThreadLocalKeyScheduler, sched);
     try
     {
         using (var sr = new StreamReader(stream))
         {
             ProcessInternal(sr.ReadToEnd());
         }
         ExecutePreProcessCommands(sched);
         ScheduleJobs(sched);
     }
     finally
     {
         LogicalThreadContext.FreeNamedDataSlot(ThreadLocalKeyScheduler);
     }
 }
Exemplo n.º 13
0
 /// <summary>
 /// Process the xml file in the given location, and schedule all of the
 /// jobs defined within it.
 /// </summary>
 /// <param name="stream">stream to read XML data from.</param>
 /// <param name="sched">The sched.</param>
 public virtual async Task ProcessStreamAndScheduleJobs(Stream stream, IScheduler sched)
 {
     LogicalThreadContext.SetData(ThreadLocalKeyScheduler, sched);
     try
     {
         using (var sr = new StreamReader(stream))
         {
             ProcessInternal(await sr.ReadToEndAsync().ConfigureAwait(false));
         }
         await ExecutePreProcessCommands(sched).ConfigureAwait(false);
         await ScheduleJobs(sched).ConfigureAwait(false);
     }
     finally
     {
         LogicalThreadContext.FreeNamedDataSlot(ThreadLocalKeyScheduler);
     }
 }
        /// <summary>
        /// Unbind a resource for the given key from the current thread
        /// </summary>
        /// <param name="key">key to check</param>
        /// <returns>the previously bound value</returns>
        /// <exception cref="InvalidOperationException">if there is no value bound to the thread</exception>
        public static object UnbindResource(Object key)
        {
            var resources = LogicalThreadContext.GetData(RESOURCES_DATA_SLOT_NAME) as IDictionary;

            if (resources == null || !resources.Contains(key))
            {
                throw new InvalidOperationException("No value for key [" + key + "] bound to thread [" +
                                                    Thread.CurrentThread.ManagedThreadId + "]");
            }
            Object val = resources[key];

            resources.Remove(key);
            if (resources.Count == 0)
            {
                LogicalThreadContext.FreeNamedDataSlot(RESOURCES_DATA_SLOT_NAME);
            }
            if (log.IsDebugEnabled)
            {
                log.Debug("Removed value [" + Describe(val) + "] for key [" + Describe(key) + "] from thread [" +
                          Thread.CurrentThread.ManagedThreadId + "]");
            }
            return(val);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Process Sessions that have been registered for deferred close
        /// for the given SessionFactory.
        /// </summary>
        /// <param name="sessionFactory">The session factory.</param>
        /// <exception cref="InvalidOperationException">If there is no session factory associated with the thread.</exception>
        public static void ProcessDeferredClose(ISessionFactory sessionFactory)
        {
            AssertUtils.ArgumentNotNull(sessionFactory, "No SessionFactory specified");

            IDictionary holderDictionary = LogicalThreadContext.GetData(DeferredCloseHolderDataSlotName) as IDictionary;

            if (holderDictionary == null || !holderDictionary.Contains(sessionFactory))
            {
                throw new InvalidOperationException("Deferred close not active for SessionFactory [" + sessionFactory + "]");
            }
            log.Debug("Processing deferred close of Hibernate Sessions");
            Set sessions = (Set)holderDictionary[sessionFactory];

            holderDictionary.Remove(sessionFactory);
            foreach (ISession session in sessions)
            {
                CloseSession(session);
            }

            if (holderDictionary.Count == 0)
            {
                LogicalThreadContext.FreeNamedDataSlot(DeferredCloseHolderDataSlotName);
            }
        }
Exemplo n.º 16
0
 /// <summary>
 /// Removes the user credentials from current thread.  Use statically specified
 /// credentials afterwards.
 /// </summary>
 public void RemoveCredentialsFromCurrentThread()
 {
     LogicalThreadContext.FreeNamedDataSlot(USERNAME);
     LogicalThreadContext.FreeNamedDataSlot(PASSWORD);
 }
Exemplo n.º 17
0
 /// <summary>
 /// 从当前线程中移除WarehouseId设置.
 /// </summary>
 public static void ClearCurrentThreadWarehouseId()
 {
     LogicalThreadContext.FreeNamedDataSlot(CURRENT_WAREHOUSE_ID);
 }