users@glassfish.java.net

JMS Transaction Issues

From: <glassfish_at_javadesktop.org>
Date: Tue, 26 Oct 2010 09:27:52 PDT

Hello,
I am trying to figure out the proper way to handle my problem. My problem is this I have a Session Bean which calls another session bean that submits items to a JMS Queue. The problem is because the outer session bean creates a transaction when the inner session bean is submitting to the queue it is not releasing the connection to the pool. I can get around this problem by using the following annotation on the submit method of the inner session bean.

[code]@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)[/code]

Here is a simple code to reproduce my problem.
[code]
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jms.stresstest;

import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author brsmit
 */
@Stateless(mappedName="OuterTranBean")
public class OuterTranSessionBean implements OuterTranSessionBeanRemote {
    @EJB StressTestSubmitterSessionBeanRemote submitter;
    public void runTest() {
        for(int i = 0; i < 2000; i++){
            submitter.submit(i);
        }
    }
    
    
}

package jms.stresstest;

import javax.ejb.Remote;

/**
 *
 * @author brsmit
 */
@Remote
public interface OuterTranSessionBeanRemote {

    void runTest();
    
}

package jms.stresstest;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 *
 * @author brsmit
 */
@MessageDriven(mappedName = "jms/StressTestQueue", activationConfig = {
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
    })
public class StressTestMDBBean implements MessageListener {
    
    public StressTestMDBBean() {
    }

    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println("Processing message. #" + textMessage.getText());
            try {
                Thread.currentThread().sleep(2000);
            } catch (InterruptedException ex) {
                Logger.getLogger(StressTestMDBBean.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("Finished processing message. #" + textMessage.getText());
        } catch (JMSException ex) {
            Logger.getLogger(StressTestMDBBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}

package jms.stresstest;

import javax.ejb.Stateless;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author brsmit
 */
@Stateless(mappedName="StressTestSubmitterBean")
public class StressTestSubmitterSessionBean implements StressTestSubmitterSessionBeanRemote {

    public void submit(Integer id) {
        try {
            Context ctx = new InitialContext();
            ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("jms/ConnectionFactory");
            Destination destination = (Destination) ctx.lookup("jms/StressTestQueue");
            try {
                Connection connection = connectionFactory.createConnection();
                try {
                    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                    MessageProducer producer = null;
                    try {
                        producer = session.createProducer(destination);
                        //ObjectMessage message = session.createObjectMessage(new CreditorSearchVO(reportItemId, creditorName, creditorAddress,
                        // creditorCity, creditorState, creditorZip, creditorPhone));
                        TextMessage message = session.createTextMessage(id.toString());
                        producer.send(message);

                    } finally {
                        if(producer != null)
                            producer.close();

                        if(session != null)
                            session.close();
                    }
                } finally {
                    if(connection != null)
                        connection.close();
                }
            } catch (JMSException ex) {
                // don't raise the exception because it will cause a rollback
                System.out.println("Error creating JMS session.");
            }
        } catch (NamingException ex) {
            // don't raise the exception because it will cause a rollback
            System.out.println("Error finding JMS ConnectionFactory or jms/CreditorSearchQueue ");
        }
    }
    
}

package jms.stresstest;

import javax.ejb.Remote;

/**
 *
 * @author brsmit
 */
@Remote
public interface StressTestSubmitterSessionBeanRemote {

    void submit(Integer id);
    
}

[/code]
[Message sent by forum member 'epicofchaos']

http://forums.java.net/jive/thread.jspa?messageID=486276