Пример #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);
            }
            EmsResourceHolder resourceHolder = (EmsResourceHolder)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)
        {
            EmsResourceHolder conHolder = (EmsResourceHolder)suspendedResources;

            TransactionSynchronizationManager.BindResource(ConnectionFactory, conHolder);
        }
Пример #3
0
 public EmsResourceSynchronization(EmsResourceHolder resourceHolder, object resourceKey, bool transacted)
 {
     this.resourceKey    = resourceKey;
     this.resourceHolder = resourceHolder;
     this.transacted     = transacted;
 }
Пример #4
0
 public virtual IConnection GetConnection(EmsResourceHolder holder)
 {
     return(existingCon != null ? existingCon : holder.GetConnection());
 }
Пример #5
0
 public virtual ISession GetSession(EmsResourceHolder holder)
 {
     return(holder.GetSession(typeof(ISession), existingCon));
 }
Пример #6
0
        /// <summary>
        /// Obtain a EMS 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
        /// EMS 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>EMSException in case of EMS 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");

            EmsResourceHolder resourceHolder =
                (EmsResourceHolder)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);
            }
            EmsResourceHolder resourceHolderToUse = resourceHolder;

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

            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 (EMSException)
            {
                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 EmsResourceSynchronization(resourceHolderToUse,
                                                   resourceKey, resourceFactory.SynchedLocalTransactionAllowed));
                resourceHolderToUse.SynchronizedWithTransaction = true;
                TransactionSynchronizationManager.BindResource(resourceKey, resourceHolderToUse);
            }
            return(session);
        }
Пример #7
0
 /// <summary> Fetch an appropriate Session from the given EmsResourceHolder.
 /// </summary>
 /// <param name="holder">the EmsResourceHolder
 /// </param>
 /// <returns> an appropriate Session fetched from the holder,
 /// or <code>null</code> if none found
 /// </returns>
 protected virtual ISession GetSession(EmsResourceHolder holder)
 {
     return holder.GetSession();
 }
Пример #8
0
 public virtual ISession GetSession(EmsResourceHolder holder)
 {
     return EnclosingInstance.GetSession(holder);
 }
Пример #9
0
 /// <summary> Fetch an appropriate Connection from the given EmsResourceHolder.
 /// </summary>
 /// <param name="holder">the EmsResourceHolder
 /// </param>
 /// <returns> an appropriate Connection fetched from the holder,
 /// or <code>null</code> if none found
 /// </returns>
 protected virtual IConnection GetConnection(EmsResourceHolder holder)
 {
     return holder.GetConnection();
 }
Пример #10
0
 public virtual IConnection GetConnection(EmsResourceHolder holder)
 {
     return EnclosingInstance.GetConnection(holder);
 }
Пример #11
0
 public EmsResourceSynchronization(EmsResourceHolder resourceHolder, object resourceKey, bool transacted)
 {
     this.resourceKey = resourceKey;
     this.resourceHolder = resourceHolder;
     this.transacted = transacted;
 }
Пример #12
0
 public virtual IConnection GetConnection(EmsResourceHolder holder)
 {
     return (existingCon != null ? existingCon : holder.GetConnection());
 }
Пример #13
0
 public virtual ISession GetSession(EmsResourceHolder holder)
 {
     return holder.GetSession(typeof(ISession), existingCon);
 }
Пример #14
0
        /// <summary>
        /// Obtain a EMS 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
        /// EMS 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>EMSException in case of EMS 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");

            EmsResourceHolder resourceHolder =
                (EmsResourceHolder)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;
            }
            EmsResourceHolder resourceHolderToUse = resourceHolder;
            if (resourceHolderToUse == null)
            {
                resourceHolderToUse = new EmsResourceHolder();
            }

            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 (EMSException)
            {
                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 EmsResourceSynchronization(resourceHolderToUse,
                                                   resourceKey, resourceFactory.SynchedLocalTransactionAllowed));
                resourceHolderToUse.SynchronizedWithTransaction = true;
                TransactionSynchronizationManager.BindResource(resourceKey, resourceHolderToUse);
            }
            return session;
        }