« Tips for efficient frequent JMX calls to WebSphere. | Main | WebSphere 6.0 and NFS V4/SAN FS, a match in heaven »

January 05, 2005

JMS Session sharing in WebSphere 5.1

Get asked a lot how we do JMS connection/session sharing in WebSphere. The following applies equally to applications using the embedded JMS provider or generic JMS providers.

  1. Begin
  2. App looks up JMS connection and gets a session.
  3. Later, another app bean again looks up a connection and gets a session.
  4. Commit.

This results in a second independant session that is registered with the transaction. WebSphere returns the same session only when the applications asks for it using the same connection each time.

  1. Begin
  2. Look up JMS Connection
  3. Get session
  4. ...
  5. Get another session from the same connection as before.
  6. Commit

Here, the same session is returned for the second session. This is because the connection is reused. If the application creates a second connection then any sessions creates on it are independant of sessions made on other connections.

This means the application should pass the JMS Connection as an argument to any methods on EJBs or POJOs that may need to make a session later on in the current transaction.

This also means MDBs that republish a message to a different queue/topic must use 2PC even when using the same JMS provider as the MDB. This is because there is no way to get the same JMS connection used by the MDB listener and hence you must make a new one and get a new session. This means two sessions and this means 2PC must be used.

If you do the same thing using async beans then this doesn't apply as you make the JMS connection and can share it. The idea here is that you make a startup bean that starts a daemon thread. The daemon thread makes a JMS connection and session, starts a transaction and then asks the session for a message. If no message is received for say 5 seconds then the tx is rolled back and we restart. If we got a message then we process it and make sure to provide the connection and session to any downstream objects. This lets us share the connection and then get 1PC behavior.

So, sessions are only shared when obtained from the same JMS connection.

January 5, 2005 in WebSphere | Permalink Sphere

Comments

Is there a way to programmatically vary the queue that my MDB listens on?

Thanks!

Posted by: Bob Cuesta | Jun 2, 2005 7:37:08 AM

Hi billy
I am confused with some concepts when reading above chapter. Could you help me with the following questions?
Will the XAResource.isSameRM returns true when the Sessions obtained from different connections connecting to the same JMS provider?
WebSphere doesn't exploit isSameRM

If it is true and the TM start the XAResource with the flag TMJOIN, will the TM commit the XAResource using 1PC option?
Although there may be 2 disk forces (one for TM and one for RM) for logging, is it still a 1PC?
Mike Spille wrote an excellent guide to XA, google him

Posted by: Jimmy | Jul 22, 2005 1:18:22 AM

Post a comment