/// <summary>
        /// Creates the shared connection for this container.
        /// </summary>
        /// <remarks>
        /// The default implementation creates a standard Connection
        /// and prepares it through <see cref="PrepareSharedConnection"/>
        /// </remarks>
        /// <returns>the prepared Connection</returns>
        /// <exception cref="NMSException">if the creation failed.</exception>
        protected virtual IConnection CreateSharedConnection()
        {
            IConnection con = CreateConnection();

            try
            {
                PrepareSharedConnection(con);
                return(con);
            } catch (Exception)
            {
                NmsUtils.CloseConnection(con);
                throw;
            }
        }
        /// <summary>
        /// Invoke the specified listener as Spring SessionAwareMessageListener,
        /// exposing a new NMS Session (potentially with its own transaction)
        /// to the listener if demanded.
        /// </summary>
        /// <param name="listener">The Spring ISessionAwareMessageListener to invoke.</param>
        /// <param name="session">The session to operate on.</param>
        /// <param name="message">The received message.</param>
        /// <exception cref="NMSException">If thrown by NMS API methods.</exception>
        /// <see cref="ISessionAwareMessageListener"/>
        /// <see cref="ExposeListenerSession"/>
        protected virtual void DoInvokeListener(ISessionAwareMessageListener listener, ISession session, IMessage message)
        {
            IConnection conToClose     = null;
            ISession    sessionToClose = null;

            try
            {
                ISession sessionToUse = session;
                if (!ExposeListenerSession)
                {
                    //We need to expose a separate Session.
                    conToClose     = CreateConnection();
                    sessionToClose = CreateSession(conToClose);
                    sessionToUse   = sessionToClose;
                }
                // Actually invoke the message listener
                if (logger.IsDebugEnabled)
                {
                    logger.Debug("Invoking listener with message of type [" + message.GetType() +
                                 "] and session [" + sessionToUse + "]");
                }
                listener.OnMessage(message, sessionToUse);
                // Clean up specially exposed Session, if any
                if (sessionToUse != session)
                {
                    if (sessionToUse.Transacted && SessionTransacted)
                    {
                        // Transacted session created by this container -> commit.
                        NmsUtils.CommitIfNecessary(sessionToUse);
                    }
                }
            } finally
            {
                NmsUtils.CloseSession(sessionToClose);
                NmsUtils.CloseConnection(conToClose);
            }
        }