5 Replies Latest reply on Dec 2, 2003 8:54 AM by Andreas

    CMP and ObjectNotFoundException

    Andreas Newbie

      Hi all,
      I have this session bean method, which creates an order - and on success sends a msg to a JMS-queue.

      OrderLocalHome ohome = getOrderLocalHome( );
      OrderLocal order = ohome.create( userId, orderDate );
      ...
      JMSUtil.sendToJMSQueue( JNDINames.QUEUE_ORDER, order.getOrderUId, false );

      Some time later the OrderProcessorMDB gets the msg and, among other things, tries to modify the before created order.
      Here do I get the "ObjectNotFoundException" for orderHome.findByPrimaryKey().
      But there is an order with the corresponding PK in the database.
      When I place additional orders the OrderProcessorMDB.onMessage() never sees the least created order.
      That means ohome.findAll() allways returns a collection not containing the very latest placed order.
      I simply don't get!
      Any ideas?

      Thanks, Andreas

        • 1. Re: CMP and ObjectNotFoundException
          patrick Novice

          Maybe it has something to do with transactions.

          Do you do yor last update and findall in the same mdb.

          Because if that is the case the transaction isn't commited yet.

          • 2. Re: CMP and ObjectNotFoundException
            Andreas Newbie

            Thanks for replying on this issue.

            I probably do have a transaction problem here.

            Please help me if I am wrong with one of the following:
            1. Inside SLSB.createOrder()
            (a) a new order is created
            (b) a msg is sent to the JMS
            (c) createOrder() returns
            (d) the transactions get committed

            2. Inside OrderProcessorMDB - orderHome.findByPrimaryKey() should be able to "see" the newly created order, because of (d), right?

            Here is the code I am using in the MDB:

            /**
            * This method expects a javax.jms.ObjectMessage that
            * contains the orderUId as the message object
            */
            public void onMessage( Message recvMsg )
            {
            Integer orderUId = null;
            OrderLocal order = null;

            try
            {
            Debug.print( "OrderProcessorMDB.onMessage(" + recvMsg + ")" );

            /* Get the order to proceed */
            ObjectMessage msg = ( ObjectMessage ) recvMsg;
            orderUId = ( Integer ) msg.getObject( );

            OrderLocalHome ohome = OrderUtil.getLocalHome();

            // debugging start
            java.util.Collection orderStubs = ohome.findAll();
            if( orderStubs != null ) {
            Debug.print( " *** orderStubs.size()" + orderStubs.size() ); // 0 after the 1. order!!
            if( 0 < orderStubs.size() ) {
            Iterator i = orderStubs.iterator();
            while( i.hasNext() ) {
            Debug.print( " *** order: " + i.next() );
            }
            }
            }
            // debugging end

            order = ohome.findByPrimaryKey( orderUId );

            /* Proceed the order */
            proceedPayment( order );
            proceedOrder( order );
            }
            catch ( CreditCardException c )
            {
            try
            {
            cancelOrder( order );
            }
            catch ( Exception e )
            {
            Debug.print( "Unable to cancel the order[" + orderUId + "]", e );
            }
            }
            catch ( Exception e )
            {
            Debug.print( "Unable to proceed the order[" + orderUId + "]", e );
            }
            }

            /**
            * TODO This function should call the CreditCardProcessor webservice to proceed
            * the payment.
            */
            private void proceedPayment( OrderLocal order )
            throws CreditCardException {}

            private void proceedOrder( OrderLocal order )
            throws Exception
            {
            order.changeStatus( OrderStatus.TO_DELIVER );
            notifyCustomer( order );
            }

            Hopefully somebody can help me with this one.
            Andreas

            • 3. Re: CMP and ObjectNotFoundException
              Andreas Newbie

              Here also the relevant log entries:

              13:01:21,819 DEBUG [Order#findAll] Executing SQL: SELECT t0_o.orderUId FROM t_order t0_o
              13:01:21,857 INFO [STDOUT] *** orderStubs.size()0
              13:01:21,859 DEBUG [Order#findByPrimaryKey] Executing SQL: SELECT orderUId FROM t_order WHERE orderUId=?
              13:01:21,863 INFO [STDOUT] Unable to proceed the order[1]
              13:01:21,865 ERROR [STDERR] javax.ejb.ObjectNotFoundException: No such entity!
              13:01:21,869 ERROR [STDERR] at org.jboss.ejb.plugins.cmp.jdbc.JDBCFindEntityCommand.execute(JDBCFindEntityCommand.java:52)
              13:01:21,870 ERROR [STDERR] at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.findEntity(JDBCStoreManager.java:571)
              13:01:21,871 ERROR [STDERR] at org.jboss.ejb.plugins.CMPPersistenceManager.findEntity(CMPPersistenceManager.java:299)
              13:01:21,872 ERROR [STDERR] at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.findEntity(CachedConnectionInterceptor.java:300)
              13:01:21,873 ERROR [STDERR] at org.jboss.ejb.EntityContainer.findLocal(EntityContainer.java:637)
              13:01:21,874 ERROR [STDERR] at sun.reflect.GeneratedMethodAccessor55.invoke(Unknown Source)
              13:01:21,875 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              13:01:21,876 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:324)
              13:01:21,877 ERROR [STDERR] at org.jboss.ejb.EntityContainer$ContainerInterceptor.invokeHome(EntityContainer.java:1043)
              13:01:21,878 ERROR [STDERR] at org.jboss.ejb.plugins.AbstractInterceptor.invokeHome(AbstractInterceptor.java:88)
              13:01:21,879 ERROR [STDERR] at org.jboss.ejb.plugins.EntitySynchronizationInterceptor.invokeHome(EntitySynchronizationInterceptor.java:197)
              13:01:21,880 ERROR [STDERR] at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invokeHome(CachedConnectionInterceptor.java:214)
              13:01:21,881 ERROR [STDERR] at org.jboss.ejb.plugins.AbstractInterceptor.invokeHome(AbstractInterceptor.java:88)
              13:01:21,882 ERROR [STDERR] at org.jboss.ejb.plugins.EntityInstanceInterceptor.invokeHome(EntityInstanceInterceptor.java:89)
              13:01:21,910 ERROR [STDERR] at org.jboss.ejb.plugins.EntityLockInterceptor.invokeHome(EntityLockInterceptor.java:61)
              13:01:21,911 ERROR [STDERR] at org.jboss.ejb.plugins.EntityCreationInterceptor.invokeHome(EntityCreationInterceptor.java:28)
              13:01:21,912 ERROR [STDERR] at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:88)
              13:01:21,913 ERROR [STDERR] at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:267)
              13:01:21,914 ERROR [STDERR] at org.jboss.ejb.plugins.TxInterceptorCMT.invokeHome(TxInterceptorCMT.java:98)
              13:01:21,915 ERROR [STDERR] at org.jboss.ejb.plugins.SecurityInterceptor.invokeHome(SecurityInterceptor.java:92)
              13:01:21,916 ERROR [STDERR] at org.jboss.ejb.plugins.LogInterceptor.invokeHome(LogInterceptor.java:120)
              13:01:21,918 ERROR [STDERR] at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invokeHome(ProxyFactoryFinderInterceptor.java:93)
              13:01:21,918 ERROR [STDERR] at org.jboss.ejb.EntityContainer.internalInvokeHome(EntityContainer.java:483)
              13:01:21,920 ERROR [STDERR] at org.jboss.ejb.Container.invoke(Container.java:720)
              13:01:21,921 ERROR [STDERR] at org.jboss.ejb.plugins.local.BaseLocalProxyFactory.invokeHome(BaseLocalProxyFactory.java:293)
              13:01:21,922 ERROR [STDERR] at org.jboss.ejb.plugins.local.LocalHomeProxy.invoke(LocalHomeProxy.java:110)
              13:01:21,923 ERROR [STDERR] at $Proxy47.findByPrimaryKey(Unknown Source)
              13:01:21,923 ERROR [STDERR] at xpetstore.services.order.ejb.OrderProcessorMDB.onMessage(Unknown Source)
              13:01:21,924 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              13:01:21,925 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              13:01:21,926 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              13:01:21,927 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:324)
              13:01:21,928 ERROR [STDERR] at org.jboss.ejb.MessageDrivenContainer$ContainerInterceptor.invoke(MessageDrivenContainer.java:460)
              13:01:21,929 ERROR [STDERR] at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:185)
              13:01:21,930 ERROR [STDERR] at org.jboss.ejb.plugins.MessageDrivenInstanceInterceptor.invoke(MessageDrivenInstanceInterceptor.java:62)
              13:01:21,931 ERROR [STDERR] at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:84)
              13:01:21,932 ERROR [STDERR] at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:267)
              13:01:21,933 ERROR [STDERR] at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:128)
              13:01:21,934 ERROR [STDERR] at org.jboss.ejb.plugins.RunAsSecurityInterceptor.invoke(RunAsSecurityInterceptor.java:90)
              13:01:21,935 ERROR [STDERR] at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191)
              13:01:21,936 ERROR [STDERR] at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)
              13:01:21,937 ERROR [STDERR] at org.jboss.ejb.MessageDrivenContainer.internalInvoke(MessageDrivenContainer.java:374)
              13:01:21,938 ERROR [STDERR] at org.jboss.ejb.Container.invoke(Container.java:700)
              13:01:21,939 ERROR [STDERR] at org.jboss.ejb.plugins.jms.JMSContainerInvoker.invoke(JMSContainerInvoker.java:824)
              13:01:21,939 ERROR [STDERR] at org.jboss.ejb.plugins.jms.JMSContainerInvoker$MessageListenerImpl.onMessage(JMSContainerInvoker.java:1114)
              13:01:21,940 ERROR [STDERR] at org.jboss.jms.asf.StdServerSession.onMessage(StdServerSession.java:256)
              13:01:21,941 ERROR [STDERR] at org.jboss.mq.SpyMessageConsumer.sessionConsumerProcessMessage(SpyMessageConsumer.java:633)
              13:01:21,942 ERROR [STDERR] at org.jboss.mq.SpyMessageConsumer.addMessage(SpyMessageConsumer.java:433)
              13:01:21,943 ERROR [STDERR] at org.jboss.mq.SpySession.run(SpySession.java:298)
              13:01:21,944 ERROR [STDERR] at org.jboss.jms.asf.StdServerSession.run(StdServerSession.java:180)
              13:01:21,945 ERROR [STDERR] at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:727)
              13:01:21,946 ERROR [STDERR] at java.lang.Thread.run(Thread.java:552)
              13:01:21,970 DEBUG [Counter] Executing SQL: UPDATE t_counter SET cnt_value=? WHERE cnt_name=?
              13:01:21,974 DEBUG [Counter] Rows affected = 1
              13:01:21,976 DEBUG [OrderItem] Executing SQL: UPDATE t_order_item SET itemId_fk=?, order_fk=? WHERE orderItemUId=?
              13:01:22,004 DEBUG [OrderItem] Rows affected = 1
              13:01:22,036 WARN [TxConnectionManager] Prepare called on a local tx. Use of local transactions on a jta transaction with more than one branch may result in inconsistent data in some cases of failure.

              • 4. Re: CMP and ObjectNotFoundException
                Adrian Brock Master

                You cannot guarantee this behaviour.

                You are committing to two different resources. These
                are done sequentially.
                It is possible (and happens in this case) that the jms commit
                occurs before the jdbc commit.

                Thread1: jms commit()
                Thread2: jms receive()
                Thread2: read database
                Thread1: jdbc commit()

                Two phase commit only guarantees that the jdbc commit
                will occur at some point in the future not that all commits
                to different resources occur together.

                In your case you don't even have that jdbc commit
                guarantee since you are using a local db connection.

                Regards,
                Adrian

                • 5. Re: CMP and ObjectNotFoundException
                  Andreas Newbie

                  Thanks Adrian,

                  I think I understand what you mean.
                  At the time MDB gets the message, I cannot be sure the before created order is persisted to the db, because the transaction has not yet committed, right?

                  But how should I change this to something that works 100%?

                  Regards,
                  Andreas