3 Replies Latest reply on Feb 26, 2004 5:03 AM by pepgrifell

    When to release JMS resource in container-mamager transactio

      Hi,
      I have a stateless session bean which will use the java:/JmsXA to obtain connectionFactory, connection and session. The method transaction is container managed. When should I release these resource?
      If I release the resource before the method return, the JMS transaction (which is within the session) is managed by container and has not commited yet. Will this resource release before methor return cause the transation roll back? If I do not release resource before the method return, I will not have a reference to them and can not release them anymore.
      Advice and suggestion are really appreciated!

      jason

        • 1. Re: When to release JMS resource in container-mamager transa
          schrouf

          Just close() your JMS connection at the end of your method call. The container will internally commit your transaction ( therefore it's container managed transaction :-)

          BTW: put all your connection/session.close() code into a finally block for avoiding memory leaks. Always catch exceptions and call ctx.setRollbackOnly() in such a case.

          Connection rConnection = null;

          try
          {
          // get resources
          rConnection = ....

          // do the work
          .....
          }
          catch( Exception eEx )
          {
          // rollback transaction (will be handled by container)
          ctx.setRollbackOnly();
          }
          finally
          {
          // close resources (will internally commit transaction
          // if setRollbackOnly() has NOT been called )
          if( rConnection != null ) try{ rConnection.close(); } catch( Exception eIgnore) {};
          }

          • 2. Re: When to release JMS resource in container-mamager transa

            You also need to close the session and sender.

            Regards,
            Adrian

            • 3. Re: When to release JMS resource in container-mamager transa
              pepgrifell

              So, what happens if I don´t close the sender and the session but I close the connection ?

              As Adrian says, after sending a message the right thing should be :

              finally
              {
               try{
               if( sender != null ) sender.close();
               if( session != null ) session.close();
               if( connection != null ) connection.close();
               } catch( Exception e) {};
              }