Declarative transaction not working for statteful ejb for 10g AS

Discussions

EJB programming & troubleshooting: Declarative transaction not working for statteful ejb for 10g AS

  1. Hi Friends,
     While migrating from sun app server to oracle 10g as, i have problem in declarative transactions.
    I created stateless and stateful beans with declarative transactions. Transactions were working fine till they were in sun app server environment, but they fail in oracle 10g as environment.

    I build the ear through sun IDE and deployed the ear on oracle 10g as. Things worked fine till transactions come onto picture.Declarative transaction get failed in case of oracle 10g as.

    Please if you have faced such problem or have any way to solve this problem, Pls revert back.

    rgds,
    m_khanduri

    Threaded Messages (4)

  2. Message Driven Bean[ Go to top ]

    hi

    I ve created a MDB with 3.0 specification.
    The MDB and its client code is given below.

    @MessageDriven(activateConfig =
    {
    @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
    @ActivationConfigProperty(propertyName="destination", propertyValue="queue/STDQ")
    })

    public class HUBMessageBrokerBean implements MessageListener
    {

    public void PostConstruct(){
    System.out.println("Post Construct method is called from MDB");
    }
    public void onMessage(Message arg0) {

    try {
    System.out.println("its coming in MDB bean");
    } catch (Exception e) {

    e.printStackTrace();
    }
    }

    }


    and the client who calls MDB is as follows:

    public class MDBServlet extends HttpServlet {

        QueueConnectionFactory queueConnectionFactory = null;
        QueueConnection queueConnection = null;
        QueueSession queueSession = null;
        QueueSender queueSender =null;
        Queue queue = null;



    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doPost(req,resp);
    }


    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    addStudent(req,resp);
    }


    private void addStudent(HttpServletRequest req, HttpServletResponse resp)throws ServletException,IOException {

    try {
    System.out.println("In the MDBServlet engine.");
    ObjectMessage obj = queueSession.createObjectMessage();
    Message msg = queueSession.createMessage();
    // Student_demo student = new Student_demo();
    // student.setFName(req.getParameter("fname"));
    // student.setLName(req.getParameter("lname"));
    //student.setRollno(Integer.parseInt(req.getParameter("rollno")));
    //student.setRollno(req.getParameter("rollno"));
    String v = "vishal";
    //obj.setObject(v);
    //msg.set
    System.out.println("before calling MDB");
    queueSender.send(msg);
    System.out.println("After calling MDB");
    } catch (JMSException e) {
    // TODO Auto-generated catch block
    System.out.println("The exception caught:"+e);
    e.printStackTrace();
    }
    }


    public void init() throws ServletException {
         try {
         Context context = new InitialContext();
                queueConnectionFactory =
                 (QueueConnectionFactory)context.lookup("QueueConnectionFactory");
         queueConnection =
         queueConnectionFactory.createQueueConnection();
         queueSession = queueConnection.createQueueSession(false,
         Session.AUTO_ACKNOWLEDGE);
         queue = (Queue)context.lookup("queue/STDQ");
         queueSender = queueSession.createSender(queue);
         } catch (Exception e) {
                System.out.println("Connection problem: " + e.toString());
                if (queueConnection != null) {
                    try {
                        queueConnection.close();
                    } catch (JMSException ee) {}
                }
         System.exit(1);
         }

    }


    @Override
    public void destroy() {
            if (queueConnection != null) {
                try {
                    queueConnection.close();
                } catch (JMSException e) {
                 e.printStackTrace();
    }
            }
    }


    }


    I have also changed or modified jbossmq-service.xml to have the queue name. the entry is as follows:

    <mbean code="org.jboss.mq.server.jmx.Queue"
    name="jboss.mq.destination:service=Queue,name=STDQ">
        <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
        <depends optional-attribute-name="SecurityManager">jboss.mq:service=SecurityManager</depends>
      </mbean>


    now the problem is that when my client sends a message to the queue, it should call onMessage() of my MDB but this method is not being called. and also no exception is caught.

    please help me how can i tell the queue or container to call onMessage() of my MDB.

    reply awaited. Thanks.

    Vishal Joshi
  3. Check J2EE specification[ Go to top ]

    If I am not wrong (and I might be), J2EE specification doesn't allow declarative transactions on Stateful Session Beans.
  4. Hi Daniel,
              But Transaction within stateless bean too is not working. Here is the little code i have used for rollback for my stateless bean. stateless bean used context rollback method, but that method doesn't mark my transaction for rollback. This method work only for declarative transactions.

    Regards
    m_khanduri

    public String insertRecord(String DAOReference, BonBaseDto insertRecord) throws java.rmi.RemoteException, BONException{
    ..
    ...
    .....
    // Some code for insertion of data.
    .......

    }catch(BONException be){
      this.context.setRollbackOnly();
      throw new EJBException("#### BONException: " +
      be.getMessage());
      //throw be;
      }
      catch(Exception e){
      this.context.setRollbackOnly();
      throw new EJBException("#### Exception: " + e.getMessage
      ());
      //throw new BONException("BON-SES-INS-ERR-1-01-0001",e);
      }
  5. I guess you should call rollback()[ Go to top ]

    Mayank,

    Since you're using bean managed transaction, you're responsible for mark the transaction bondaries.

    So, you have to clearly begin the transaction, and also rollback the transaction. For example :

    Context context = new InitialContext();
    UserTransaction transaction = (UserTransaction) contex.lookup("jta/UserTransaction");

    try {
        transaction.begin();

        // do your database stuff
        // call other beans

    } catch(Exception e) {
       transaction.rollback();
       throw e;
    }

    Calling setRollBackOnly() in a bean managed transaction, won't garantee that the transaction will rollback'ed. You have to do that by yourself.