Пример #1
0
        /// <summary>
        /// Determines whether the given JMS Session is transactional, that is,
        /// bound to the current thread by Spring's transaction facilities.
        /// </summary>
        /// <param name="session">The session to check.</param>
        /// <param name="cf">The ConnectionFactory that the Session originated from</param>
        /// <returns>
        ///     <c>true</c> if is session transactional, bound to current thread; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsSessionTransactional(ISession session, IConnectionFactory cf)
        {
            if (session == null || cf == null)
            {
                return(false);
            }
            NmsResourceHolder resourceHolder = (NmsResourceHolder)TransactionSynchronizationManager.GetResource(cf);

            return(resourceHolder != null && resourceHolder.ContainsSession(session));
        }
Пример #2
0
        /// <summary>
        /// Resume the resources of the current transaction.
        /// </summary>
        /// <param name="transaction">Transaction object returned by
        /// <see cref="Spring.Transaction.Support.AbstractPlatformTransactionManager.DoGetTransaction"/>.</param>
        /// <param name="suspendedResources">The object that holds suspended resources as returned by
        /// <see cref="Spring.Transaction.Support.AbstractPlatformTransactionManager.DoSuspend"/>.</param>
        /// <remarks>Transaction synchronization will be resumed afterwards.
        /// </remarks>
        /// <exception cref="Spring.Transaction.TransactionException">
        /// In the case of system errors.
        /// </exception>
        protected override void DoResume(object transaction, object suspendedResources)
        {
            NmsResourceHolder conHolder = (NmsResourceHolder)suspendedResources;

            TransactionSynchronizationManager.BindResource(ConnectionFactory, conHolder);
        }
 public NmsResourceSynchronization(NmsResourceHolder resourceHolder, object resourceKey, bool transacted)
 {
     this.resourceKey = resourceKey;
     this.resourceHolder = resourceHolder;
     this.transacted = transacted;
 }
 public virtual IConnection GetConnection(NmsResourceHolder holder)
 {
     return (existingCon != null ? existingCon : holder.GetConnection());
 }
 public virtual ISession GetSession(NmsResourceHolder holder)
 {
     return holder.GetSession(typeof(ISession), existingCon);
 }
        /// <summary>
        /// Obtain a NMS Session that is synchronized with the current transaction, if any.
        /// </summary>
        /// <param name="resourceKey">the TransactionSynchronizationManager key to bind to
        /// (usually the ConnectionFactory)</param>
        /// <param name="resourceFactory">the ResourceFactory to use for extracting or creating
        /// NMS resources</param>
        /// <param name="startConnection">whether the underlying Connection approach should be
	    /// started in order to allow for receiving messages. Note that a reused Connection
	    /// may already have been started before, even if this flag is <code>false</code>.</param>
        /// <returns>
        /// the transactional Session, or <code>null</code> if none found
        /// </returns>
        /// <throws>NMSException in case of NMS failure </throws>
        public static ISession DoGetTransactionalSession(Object resourceKey, ResourceFactory resourceFactory, bool startConnection)
        {
            AssertUtils.ArgumentNotNull(resourceKey, "Resource key must not be null");
            AssertUtils.ArgumentNotNull(resourceKey, "ResourceFactory must not be null");

            NmsResourceHolder resourceHolder =
                (NmsResourceHolder)TransactionSynchronizationManager.GetResource(resourceKey);
            if (resourceHolder != null)
            {
                ISession rhSession = resourceFactory.GetSession(resourceHolder);
                if (rhSession != null)
                {
                    if (startConnection)
                    {
                        IConnection conn = resourceFactory.GetConnection(resourceHolder);
                        if (conn != null)
                        {
                            conn.Start();
                        }
                    }
                    return rhSession;
                }
            }
            if (!TransactionSynchronizationManager.SynchronizationActive)
            {
                return null;
            }
            NmsResourceHolder resourceHolderToUse = resourceHolder;
            if (resourceHolderToUse == null)
            {
                resourceHolderToUse = new NmsResourceHolder();
            }

            IConnection con = resourceFactory.GetConnection(resourceHolderToUse);
            ISession session = null;
            try
            {
                bool isExistingCon = (con != null);
                if (!isExistingCon)
                {
                    con = resourceFactory.CreateConnection();
                    resourceHolderToUse.AddConnection(con);
                }
                session = resourceFactory.CreateSession(con);
                resourceHolderToUse.AddSession(session, con);
                if (startConnection)
                {
                    con.Start();
                }
            }
            catch (NMSException)
            {
                if (session != null)
                {
                    try
                    {
                        session.Close();
                    }
                    catch (Exception)
                    {
                        // ignore
                    }
                }
                if (con != null)
                {
                    try
                    {
                        con.Close();
                    }
                    catch (Exception)
                    {
                        // ignore
                    }
                }
                throw;
            }
            if (resourceHolderToUse != resourceHolder)
            {
                TransactionSynchronizationManager.RegisterSynchronization(
                    new NmsResourceSynchronization(resourceHolderToUse, resourceKey,
                                                   resourceFactory.SynchedLocalTransactionAllowed));
                resourceHolderToUse.SynchronizedWithTransaction = true;
                TransactionSynchronizationManager.BindResource(resourceKey, resourceHolderToUse);
            }
            return session;
        }
Пример #7
0
 /// <summary> Fetch an appropriate Session from the given MessageResourceHolder.
 /// </summary>
 /// <param name="holder">the MessageResourceHolder
 /// </param>
 /// <returns> an appropriate ISession fetched from the holder,
 /// or <code>null</code> if none found
 /// </returns>
 protected virtual ISession GetSession(NmsResourceHolder holder)
 {
     return holder.GetSession();
 }
Пример #8
0
 /// <summary> Fetch an appropriate Connection from the given MessageResourceHolder.
 /// </summary>
 /// <param name="holder">the MessageResourceHolder
 /// </param>
 /// <returns> an appropriate IConnection fetched from the holder,
 /// or <code>null</code> if none found
 /// </returns>
 protected virtual IConnection GetConnection(NmsResourceHolder holder)
 {
     return holder.GetConnection();
 }
Пример #9
0
 public virtual ISession GetSession(NmsResourceHolder holder)
 {
     return EnclosingInstance.GetSession(holder);
 }
Пример #10
0
 public virtual IConnection GetConnection(NmsResourceHolder holder)
 {
     return EnclosingInstance.GetConnection(holder);
 }
Пример #11
0
 public NmsResourceSynchronization(NmsResourceHolder resourceHolder, object resourceKey, bool transacted)
 {
     this.resourceKey    = resourceKey;
     this.resourceHolder = resourceHolder;
     this.transacted     = transacted;
 }
Пример #12
0
 public virtual IConnection GetConnection(NmsResourceHolder holder)
 {
     return(existingCon != null ? existingCon : holder.GetConnection());
 }
Пример #13
0
 public virtual ISession GetSession(NmsResourceHolder holder)
 {
     return(holder.GetSession(typeof(ISession), existingCon));
 }
Пример #14
0
        /// <summary>
        /// Obtain a NMS Session that is synchronized with the current transaction, if any.
        /// </summary>
        /// <param name="resourceKey">the TransactionSynchronizationManager key to bind to
        /// (usually the ConnectionFactory)</param>
        /// <param name="resourceFactory">the ResourceFactory to use for extracting or creating
        /// NMS resources</param>
        /// <param name="startConnection">whether the underlying Connection approach should be
        /// started in order to allow for receiving messages. Note that a reused Connection
        /// may already have been started before, even if this flag is <code>false</code>.</param>
        /// <returns>
        /// the transactional Session, or <code>null</code> if none found
        /// </returns>
        /// <throws>NMSException in case of NMS failure </throws>
        public static ISession DoGetTransactionalSession(Object resourceKey, ResourceFactory resourceFactory, bool startConnection)
        {
            AssertUtils.ArgumentNotNull(resourceKey, "Resource key must not be null");
            AssertUtils.ArgumentNotNull(resourceKey, "ResourceFactory must not be null");

            NmsResourceHolder resourceHolder =
                (NmsResourceHolder)TransactionSynchronizationManager.GetResource(resourceKey);

            if (resourceHolder != null)
            {
                ISession rhSession = resourceFactory.GetSession(resourceHolder);
                if (rhSession != null)
                {
                    if (startConnection)
                    {
                        IConnection conn = resourceFactory.GetConnection(resourceHolder);
                        if (conn != null)
                        {
                            conn.Start();
                        }
                    }
                    return(rhSession);
                }
            }
            if (!TransactionSynchronizationManager.SynchronizationActive)
            {
                return(null);
            }
            NmsResourceHolder resourceHolderToUse = resourceHolder;

            if (resourceHolderToUse == null)
            {
                resourceHolderToUse = new NmsResourceHolder();
            }

            IConnection con     = resourceFactory.GetConnection(resourceHolderToUse);
            ISession    session = null;

            try
            {
                bool isExistingCon = (con != null);
                if (!isExistingCon)
                {
                    con = resourceFactory.CreateConnection();
                    resourceHolderToUse.AddConnection(con);
                }
                session = resourceFactory.CreateSession(con);
                resourceHolderToUse.AddSession(session, con);
                if (startConnection)
                {
                    con.Start();
                }
            }
            catch (NMSException)
            {
                if (session != null)
                {
                    try
                    {
                        session.Close();
                    }
                    catch (Exception)
                    {
                        // ignore
                    }
                }
                if (con != null)
                {
                    try
                    {
                        con.Close();
                    }
                    catch (Exception)
                    {
                        // ignore
                    }
                }
                throw;
            }
            if (resourceHolderToUse != resourceHolder)
            {
                TransactionSynchronizationManager.RegisterSynchronization(
                    new NmsResourceSynchronization(resourceHolderToUse, resourceKey,
                                                   resourceFactory.SynchedLocalTransactionAllowed));
                resourceHolderToUse.SynchronizedWithTransaction = true;
                TransactionSynchronizationManager.BindResource(resourceKey, resourceHolderToUse);
            }
            return(session);
        }