Class JmsManagedConnection

  • All Implemented Interfaces:
    javax.jms.ExceptionListener, javax.resource.spi.ManagedConnection

    public class JmsManagedConnection
    extends Object
    implements javax.resource.spi.ManagedConnection, javax.jms.ExceptionListener

    Managed Connection, manages one or more JMS sessions.

    Every ManagedConnection will have a physical JMSConnection under the hood. This may leave out several session, as specifyed in 5.5.4 Multiple Connection Handles. Thread safe semantics is provided

    Hm. If we are to follow the example in 6.11 this will not work. We would have to use the SAME session. This means we will have to guard against concurrent access. We use a stack, and only allowes the handle at the top of the stack to do things.

    As to transactions we some fairly hairy alternatives to handle: XA - we get an XA. We may now only do transaction through the XAResource, since a XASession MUST throw exceptions in commit etc. But since XA support implies LocatTransaction support, we will have to use the XAResource in the LocalTransaction class. LocalTx - we get a normal session. The LocalTransaction will then work against the normal session api.

    An invokation of JMS MAY BE DONE in none transacted context. What do we do then? How much should we leave to the user???

    One possible solution is to use transactions any way, but under the hood. If not LocalTransaction or XA has been aquired by the container, we have to do the commit in send and publish. (CHECK is the container required to get a XA every time it uses a managed connection? No its is not, only at creation!)

    Does this mean that a session one time may be used in a transacted env, and another time in a not transacted.

    Maybe we could have this simple rule:

    If a user is going to use non trans:

    • mark that i ra deployment descr
    • Use a JmsProviderAdapter with non XA factorys
    • Mark session as non transacted (this defeats the purpose of specifying
    • trans attrinbutes in deploy descr NOT GOOD

    From the JMS tutorial: "When you create a session in an enterprise bean, the container ignores the arguments you specify, because it manages all transactional properties for enterprise beans."

    And further: "You do not specify a message acknowledgment mode when you create a message-driven bean that uses container-managed transactions. The container handles acknowledgment automatically."

    On Session or Connection:

    From Tutorial: "A JMS API resource is a JMS API connection or a JMS API session." But in the J2EE spec only connection is considered a resource.

    Not resolved: connectionErrorOccurred: it is verry hard to know from the exceptions thrown if it is a connection error. Should we register an ExceptionListener and mark al handles as errounous? And then let them send the event and throw an exception?

    Author:
    Peter Antman., Jason Dillon, Adrian Brock
    • Constructor Detail

      • JmsManagedConnection

        public JmsManagedConnection​(JmsManagedConnectionFactory mcf,
                                    javax.resource.spi.ConnectionRequestInfo info,
                                    String user,
                                    String pwd)
                             throws javax.resource.ResourceException
        Create a JmsManagedConnection.
        Parameters:
        mcf -
        info -
        user -
        pwd -
        Throws:
        javax.resource.ResourceException
    • Method Detail

      • getConnection

        public Object getConnection​(Subject subject,
                                    javax.resource.spi.ConnectionRequestInfo info)
                             throws javax.resource.ResourceException
        Get the physical connection handler. This bummer will be called in two situations:
        1. When a new mc has bean created and a connection is needed
        2. When an mc has been fetched from the pool (returned in match*)
        It may also be called multiple time without a cleanup, to support connection sharing.
        Specified by:
        getConnection in interface javax.resource.spi.ManagedConnection
        Parameters:
        subject -
        info -
        Returns:
        A new connection object.
        Throws:
        javax.resource.ResourceException
      • destroy

        public final void destroy()
                           throws javax.resource.ResourceException
        Destroy the physical connection.
        Specified by:
        destroy in interface javax.resource.spi.ManagedConnection
        Throws:
        javax.resource.ResourceException - Could not property close the session and connection.
      • cleanup

        public void cleanup()
                     throws javax.resource.ResourceException
        Cleans up, from the spec - The cleanup of ManagedConnection instance resets its client specific state. Does that mean that authentication should be redone. FIXME
        Specified by:
        cleanup in interface javax.resource.spi.ManagedConnection
        Throws:
        javax.resource.ResourceException
      • associateConnection

        public void associateConnection​(Object obj)
                                 throws javax.resource.ResourceException
        Move a handler from one mc to this one.
        Specified by:
        associateConnection in interface javax.resource.spi.ManagedConnection
        Parameters:
        obj - An object of type JmsSession.
        Throws:
        javax.resource.ResourceException - Failed to associate connection.
        javax.resource.spi.IllegalStateException - ManagedConnection in an illegal state.
      • lock

        protected void lock()
      • tryLock

        protected void tryLock()
                        throws javax.jms.JMSException
        Throws:
        javax.jms.JMSException
      • unlock

        protected void unlock()
      • addConnectionEventListener

        public void addConnectionEventListener​(javax.resource.spi.ConnectionEventListener l)
        Add a connection event listener.
        Specified by:
        addConnectionEventListener in interface javax.resource.spi.ManagedConnection
        Parameters:
        l - The connection event listener to be added.
      • removeConnectionEventListener

        public void removeConnectionEventListener​(javax.resource.spi.ConnectionEventListener l)
        Remove a connection event listener.
        Specified by:
        removeConnectionEventListener in interface javax.resource.spi.ManagedConnection
        Parameters:
        l - The connection event listener to be removed.
      • getXAResource

        public XAResource getXAResource()
                                 throws javax.resource.ResourceException
        Get the XAResource for the connection.
        Specified by:
        getXAResource in interface javax.resource.spi.ManagedConnection
        Returns:
        The XAResource for the connection.
        Throws:
        javax.resource.ResourceException - XA transaction not supported
      • getLocalTransaction

        public javax.resource.spi.LocalTransaction getLocalTransaction()
                                                                throws javax.resource.ResourceException
        Get the location transaction for the connection.
        Specified by:
        getLocalTransaction in interface javax.resource.spi.ManagedConnection
        Returns:
        The local transaction for the connection.
        Throws:
        javax.resource.ResourceException
      • getMetaData

        public javax.resource.spi.ManagedConnectionMetaData getMetaData()
                                                                 throws javax.resource.ResourceException
        Get the meta data for the connection.
        Specified by:
        getMetaData in interface javax.resource.spi.ManagedConnection
        Returns:
        The meta data for the connection.
        Throws:
        javax.resource.ResourceException
        javax.resource.spi.IllegalStateException - ManagedConnection already destroyed.
      • setLogWriter

        public void setLogWriter​(PrintWriter out)
                          throws javax.resource.ResourceException
        Set the log writer for this connection.
        Specified by:
        setLogWriter in interface javax.resource.spi.ManagedConnection
        Parameters:
        out - The log writer for this connection.
        Throws:
        javax.resource.ResourceException
      • getLogWriter

        public PrintWriter getLogWriter()
                                 throws javax.resource.ResourceException
        Get the log writer for this connection.
        Specified by:
        getLogWriter in interface javax.resource.spi.ManagedConnection
        Returns:
        Always null
        Throws:
        javax.resource.ResourceException
      • onException

        public void onException​(javax.jms.JMSException exception)
        Specified by:
        onException in interface javax.jms.ExceptionListener
      • getSession

        protected javax.jms.Session getSession()
        Get the session for this connection.
        Returns:
        Either a topic or queue connection.
      • getJMSContext

        protected javax.jms.JMSContext getJMSContext()
        Get the JMSContext for this connection.
        Returns:
        the JMSContext for this connection.
      • sendEvent

        protected void sendEvent​(javax.resource.spi.ConnectionEvent event)
        Send an event.
        Parameters:
        event - The event to send.
      • removeHandle

        protected void removeHandle​(JmsSession handle)
        Remove a handle from the handle map.
        Parameters:
        handle - The handle to remove.
      • getInfo

        protected javax.resource.spi.ConnectionRequestInfo getInfo()
        Get the request info for this connection.
        Returns:
        The request info for this connection.
      • getManagedConnectionFactory

        protected JmsManagedConnectionFactory getManagedConnectionFactory()
        Get the connection factory for this connection.
        Returns:
        The connection factory for this connection.
      • getUserName

        protected String getUserName()
        Get the user name for this connection.
        Returns:
        The user name for this connection.
      • createConnection

        public javax.jms.Connection createConnection​(Object factory,
                                                     String username,
                                                     String password,
                                                     boolean transacted,
                                                     int ack)
                                              throws javax.jms.JMSException
        Create a connection from the given factory.An XA connection will be created if possible.
        Parameters:
        factory - An object that implements ConnectionFactory, XAQConnectionFactory
        username - The username to use or null for no user.
        password - The password for the given username or null if no username was specified.
        transacted -
        ack -
        Returns:
        A connection.
        Throws:
        javax.jms.JMSException - Failed to create connection.
        IllegalArgumentException - Factory is null or invalid.