0 Replies Latest reply on Oct 17, 2002 8:29 PM by Pragnesh Ray

    Cannot rollback on error

    Pragnesh Ray Newbie

      I went thru all the post but it does not help me to solve this problem.
      I am trying to rollback the transaction when my DB is down and re Que the message.

      Here is the code snippet...

      public class LoggingEventMDB implements MessageDrivenBean, MessageListener
      {

      private MessageDrivenContext ctx = null;
      private QueueConnection conn;
      private QueueSession session;
      private DBAppender appender;
      private DBAppenderHome home;
      ....
      public void setMessageDrivenContext(MessageDrivenContext ctx)
      {
      this.ctx = ctx;
      System.out.println("LoggingEventMDB.setMessageDrivenContext, this="+hashCode());
      }

      public void ejbCreate()
      {
      try
      {
      setupPTP();// create connections to the Queue
      setupDB(); // create DBAppender object
      }
      catch(Exception e)
      {
      throw new EJBException("Failed to init LoggingEventMDB", e);
      }
      }


      private void setupPTP()
      throws JMSException, NamingException
      {
      InitialContext iniCtx = new InitialContext();
      Object tmp = iniCtx.lookup("ConnectionFactory");
      QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;
      conn = qcf.createQueueConnection();
      session = conn.createQueueSession(true,0);
      conn.start();
      }


      private void setupDB() throws SQLException
      {
      //create an session EJB
      }


      public void onMessage(Message message)
      {

      LoggingEvent event;
      try
      {
      if(message instanceof ObjectMessage) {

      ObjectMessage objectMessage = (ObjectMessage) message;

      event = (LoggingEvent) objectMessage.getObject();
      //write to DB
      appender = home.create();
      appender.append(event); // this method throws an SQL Ex on DB down
      System.out.println("LoggingEventMDB.onMessage, this="+hashCode());
      appender = null;

      }
      }
      catch(SQLException sqle) {
      System.out.println("SQL Exception received");
      appender = null;
      ctx.setRollbackOnly();
      }

      }


      Here is the ejb-jar.xml

      <enterprise-beans>
      <message-driven>
      <ejb-name>LoggingEventMDB</ejb-name>
      <ejb-class>net.earthlink.is.registration.mqappender.LoggingEventMDB</ejb-class>
      <transaction-type>Container</transaction-type>
      <acknowledge-mode>AUTO_ACKNOWLEDGE</acknowledge-mode>
      <message-driven-destination>
      <destination-type>javax.jms.Queue</destination-type>
      </message-driven-destination>
      <resource-ref>
      <res-ref-name>jms/QCF</res-ref-name>
      <res-type>javax.jms.QueueConnectionFactory</res-type>
      <res-auth>Container</res-auth>
      </resource-ref>
      </message-driven>
      <assembly-descriptor>
      <container-transaction>

      <ejb-name>LoggingEventMDB</ejb-name>
      <method-name>onMessage</method-name>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      </assembly-descriptor>

      Here is the error
      19:27:15,494 ERROR [LogInterceptor] EJBException, causedBy:
      java.lang.IllegalStateException: setRollbackOnly must only be called in the context of a transaction (EJB 2.0 - 15.5.1)
      at org.jboss.ejb.MessageDrivenEnterpriseContext$MessageDrivenContextImpl.setRollbackOnly(MessageDrivenEnterpriseContext.java:214)
      at net.earthlink.is.registration.mqappender.LoggingEventMDB.onMessage(LoggingEventMDB.java:170)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at org.jboss.ejb.MessageDrivenContainer$ContainerInterceptor.invoke(MessageDrivenContainer.java:391)
      at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:186)
      at org.jboss.ejb.plugins.MessageDrivenInstanceInterceptor.invoke(MessageDrivenInstanceInterceptor.java:88)
      at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:107)
      at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:178)
      at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:60)
      at org.jboss.ejb.plugins.RunAsSecurityInterceptor.invoke(RunAsSecurityInterceptor.java:100)
      at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:203)
      at org.jboss.ejb.MessageDrivenContainer.invoke(MessageDrivenContainer.java:302)
      at org.jboss.ejb.plugins.jms.JMSContainerInvoker.invoke(JMSContainerInvoker.java:962)
      at org.jboss.ejb.plugins.jms.JMSContainerInvoker$MessageListenerImpl.onMessage(JMSContainerInvoker.java:1038)
      at org.jboss.jms.asf.StdServerSession.onMessage(StdServerSession.java:238)
      at org.jboss.mq.SpyMessageConsumer.sessionConsumerProcessMessage(SpyMessageConsumer.java:603)
      at org.jboss.mq.SpyMessageConsumer.addMessage(SpyMessageConsumer.java:417)
      at org.jboss.mq.SpySession.run(SpySession.java:259)
      at org.jboss.jms.asf.StdServerSession.run(StdServerSession.java:177)
      at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:642)
      at java.lang.Thread.run(Thread.java:536)
      19:2