Skip to main content

When to create a javax.jms.Connection

2 replies [Last post]
cmathrusse
Offline
Joined: 2008-04-15

I have a Stateless Session bean that is responsible for sending messages onto a queue. It has a reference to a ConnectionFactory:

@Resource(mappedName="jms/myConnectionFactory")
private javax.jms.ConnectionFactory connectionFactory;

I know the connectionFactory is defined as a ConnectionPool in the container so I am very accustomed to simply asking the factory for a connection. But I am leery as to holding onto the connection. Currently, each time a message is requested to be sent I create a new Connection, which is used to create a Session. After the send is complete I close both the Session and the Connection. But now I am wondering if it would be cleaner if I were to simply get the Connection once and hold onto it for the life of the bean rather than allocating a new Connection for each request.

Of course, all of the work is performed within a CMT. So should I simply ask for a Connection once and use it for all requests and simply deal with creating a Session for each request?

Thanks for the help..

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
nigeldeakin
Offline
Joined: 2007-10-12

Creating a new JMS connection and session for every request is fine. The connection will be fetched from and returned to a pool.

Nigel

MQ team

cmathrusse
Offline
Joined: 2008-04-15

Great. Thanks for the response. I was simply concerned that I may be causing a situation where I was over working the JMS server or the ConnectionPool.