Should a JMS application create a new session for every message that it sends or is it preferable to create a session for a long message sequence?
views:
41answers:
2The class document for Session from Sun clearly talks about a single Session object handling messages plural.
I would say no in general, though it really depends on the context and on how you logically think of communications in your application.
If message sending is an occasional one-off thing, and subsequent messages are completely unrelated, then yes if you can tolerate the costs.
If you do have a lengthy and logical sequence of interaction, it makes sense to do them within a session as long as you don't mess up your threading.
A session actually provides facilities for a sequence of messages, so by definition it is meant to deal with multiple messages.
Here is what the docs say:
A session serves several purposes:
* It is a factory for its message producers and consumers. * It supplies provider-optimized message factories. * It supports a single series of transactions that combine work
spanning its producers and consumers into atomic units.
* It defines a serial order for the messages it consumes and the
messages it produces.
* It retains messages it consumes until they have been acknowledged. * It serializes execution of message listeners registered with its
message consumers.