Exemplo n.º 1
0
        /**
         * Unbind a resource for the given key from the current thread.
         * @param key the key to unbind (usually the resource factory)
         * @return the previously bound value (usually the active resource object)
         * @throws IllegalStateException if there is no value bound to the thread
         * @see ResourceTransactionManager#getResourceFactory()
         */

        /// <summary>The unbind resource.</summary>
        /// <param name="key">The key.</param>
        /// <returns>The System.Object.</returns>
        /// <exception cref="InvalidOperationException"></exception>
        public static object UnbindResource(object key)
        {
            var actualKey = TransactionSynchronizationUtils.UnwrapResourceIfNecessary(key);
            var value     = DoUnbindResource(actualKey);

            if (value == null)
            {
                throw new InvalidOperationException("No value for key [" + actualKey + "] bound to thread [" + Thread.CurrentThread.Name + "]");
            }

            return(value);
        }
Exemplo n.º 2
0
        /**
         * Retrieve a resource for the given key that is bound to the current thread.
         * @param key the key to check (usually the resource factory)
         * @return a value bound to the current thread (usually the active
         * resource object), or <code>null</code> if none
         * @see ResourceTransactionManager#getResourceFactory()
         */

        /// <summary>The get resource.</summary>
        /// <param name="key">The key.</param>
        /// <returns>The System.Object.</returns>
        public static object GetResource(object key)
        {
            var actualKey = TransactionSynchronizationUtils.UnwrapResourceIfNecessary(key);
            var value     = DoGetResource(actualKey);

            if (value != null && Logger.IsTraceEnabled)
            {
                Logger.Trace(
                    "Retrieved value [" + value + "] for key [" + actualKey + "] bound to thread [" +
                    Thread.CurrentThread.Name + "]");
            }

            return(value);
        }
Exemplo n.º 3
0
        /**
         * Bind the given resource for the given key to the current thread.
         * @param key the key to bind the value to (usually the resource factory)
         * @param value the value to bind (usually the active resource object)
         * @throws IllegalStateException if there is already a value bound to the thread
         * @see ResourceTransactionManager#getResourceFactory()
         */

        /// <summary>The bind resource.</summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <exception cref="InvalidOperationException"></exception>
        public static void BindResource(object key, object value)
        {
            var actualKey = TransactionSynchronizationUtils.UnwrapResourceIfNecessary(key);

            AssertUtils.ArgumentNotNull(value, "Value must not be null");
            var map = resources.Value;

            // set ThreadLocal Map if none found
            if (map == null)
            {
                map             = new Dictionary <object, object>();
                resources.Value = map;
            }

            var oldValue = map.Get(actualKey);

            map.Add(actualKey, value);

            // Transparently suppress a ResourceHolder that was marked as void...
            if (oldValue is IResourceHolder && ((IResourceHolder)oldValue).IsVoid())
            {
                oldValue = null;
            }

            if (oldValue != null)
            {
                throw new InvalidOperationException("Already value [" + oldValue + "] for key [" + actualKey + "] bound to thread [" + Thread.CurrentThread.Name + "]");
            }

            if (Logger.IsTraceEnabled)
            {
                Logger.Trace(
                    "Bound value [" + value + "] for key [" + actualKey + "] to thread [" +
                    Thread.CurrentThread.Name + "]");
            }
        }
Exemplo n.º 4
0
        /**
         * Unbind a resource for the given key from the current thread.
         * @param key the key to unbind (usually the resource factory)
         * @return the previously bound value, or <code>null</code> if none bound
         */

        /// <summary>The unbind resource if possible.</summary>
        /// <param name="key">The key.</param>
        /// <returns>The System.Object.</returns>
        public static object UnbindResourceIfPossible(object key)
        {
            var actualKey = TransactionSynchronizationUtils.UnwrapResourceIfNecessary(key);

            return(DoUnbindResource(actualKey));
        }