EJB: Object not found exception

I have a servlet which is communicating with a couple of EJB's which include both session and entity beans. When I run the program it seems to run for six records and then throws up an "javax.ejb.ObjectNotFoundException: Bean not found in ................"

My program is;

public class NewsletterTestServlet extends HttpServlet
{
    private static Logger log = Logger.getLogger(NewsletterTestServlet.class);

    /**
     * Loop through all newsletters. Get office for each newsletter.  Get all users of that office
     * who are subscribed and send them the email
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
      {
        EmailGenerator emailGenerator = new EmailGenerator();
        InputStream newsletterStream  = request.getSession().getServletContext().getResourceAsStream(
                                        "/WEB-INF/newsletter.xslt");  
            int newsbastsuccess = 0;

        try
            {
            EcpPortalSessionLocal epsl = (EcpPortalSessionLocal) request.getSession().getAttribute(
                                          AcuvueConstants.EcpPortalSessionKey);

            // Get all custom newsletters and iterate
            String unsubscribeUrlBase = "http://" + request.getServerName() + ":" + request.getServerPort()
                    + request.getContextPath() + "/servlet/unsubscribe.do?";
            //Collection newsletters = CustomNewsletterTypeUtil.getLocalHome().findAll();
            //SELECT OFFICEPK FROM CUSTOMNEWSLETTER
            Connection con = AcuvueFunctions.getConnection();
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(new StringBuffer("SELECT cn.CUSTOMNEWSLETTERPK, nm.MESSAGE, cn.CREATIONDATE, ot.OFFICEPK ,ot.OFFICENAME, nt.SUBSCRIPTIONPK, nt.NEWSLETTERNAME, ")
                  .append("       NVL(cat.ADDRESS1, ' ') ADDRESS1, NVL(cat.ADDRESS2, ' ') ADDRESS2, NVL(cat.SUITE, ' ') SUITE, ")
                  .append("       NVL(cat.CITY, ' ') CITY, NVL(cat.STATE, ' ') STATE, NVL(cat.ZIPCODE, ' ') ZIPCODE, ")
                  .append("       DECODE(NVL(cet.ISPUBLIC, 0), 1, EMAILADDRESS, 'PRIVATE')  EMAILADDRESS, ")
                  .append("       DECODE(NVL(cpt.ISPUBLIC, 0), 1, PHONENUMBER, 'PRIVATE') PHONENUMBER ")
                  .append("  FROM CUSTOMNEWSLETTER cn, ")
                  .append("       NEWSLETTERMESSAGE nm, ")
                  .append("       NEWSLETTERTYPE nt, ")
                  .append("       OFFICETYPE ot, ")
                  .append("       CONTACTADDRESSTYPE cat, ")
                  .append("       CONTACTEMAILTYPE cet, ")
                  .append("       CONTACTPHONETYPE cpt ")
                  .append(" WHERE cn.CUSTOMMESSAGE = nm.NEWSLETTERMESSAGEPK AND ")
                  .append("       nm.NEWSLETTERPK = nt.NEWSLETTERPK AND ")
                  .append("       cn.OFFICEPK = ot.OFFICEPK AND ")
                  .append("       ot.emailpk = cet.emailpk (+) AND ")
                  .append("       ot.phonepk = cpt.phonepk (+) AND ")
                  .append("       ot.ADDRESSPK = cat.ADDRESSPK (+)").toString());

            try
                  {
                        while (rs.next())
                        { // Start of loop
                              String office = rs.getString("OFFICEPK");
      System.out.println("Office pk recvd :***"+ office);

                              // Get all users for this office and
                              // see if user is subscribed to the newsletter

      System.out.println("Inside while here 1");
                              Collection users = UserTypeUtil.getLocalHome().findByOffice(office);
      System.out.println("Inside while here 2");

                              log.info("Found " + users.size() + " users");
      System.out.println("The no: of users for this office :****"+users.size());
                              Iterator usersIter = users.iterator();
                              while (usersIter.hasNext())
                              { // Inner loop
                                    UserTypeLocal userLocal = (UserTypeLocal) usersIter.next();
                                    UserTypeLocalValue user = userLocal.getUserTypeLocalValue();
                                    Collection subscriptions = UserSubscriptionTypeUtil.getLocalHome().findUserSubscriptionTypeLocalsByUser(user.getPrimaryKey());
                                    log.info(user.getUserName() + " has " + subscriptions.size() + " subscriptions");
      System.out.println("The no. of Subscriptions are : ***"+ subscriptions.size() );
                                    Iterator it = subscriptions.iterator();
                                    // See if user is subscribed to the newsletter
                                    while(it.hasNext())
                                    { // Second inner loop
                                          
                                          UserSubscriptionTypeLocal ustl = (UserSubscriptionTypeLocal)it.next();
                                          UserSubscriptionTypeLocalValue userSub = ustl.getUserSubscriptionTypeLocalValue();
      System.out.println("Inside second while here 3");

                                          if (AcuvueEJBConstants.UserSubscriptionStatusSubscribed.equals(userSub.getStatus())
                                                      && rs.getString("SUBSCRIPTIONPK").equals(userSub.getSubscriptionTypeLocalValue().getPrimaryKey()))
                                          {
      System.out.println("The Subscribed user is:****"+ user);

                                                String unsubscribeUrl = unsubscribeUrlBase + "userpk=" + user.getPrimaryKey()
                                                                                    + "&newsletterpk=" + rs.getString("CUSTOMNEWSLETTERPK");
                                                //Create email and send
                                                String commid = emailGenerator.generateNewsletterEmail(
                                                                        true,
                                                                        rs.getString("NEWSLETTERNAME"),
                                                                        rs.getString("MESSAGE"),
                                                                        user,
                                                                        newsletterStream,
                                                                        rs.getString("OFFICENAME"),
                                                                        rs.getString("ADDRESS1"),
                                                                        rs.getString("ADDRESS2"),
                                                                        rs.getString("SUITE"),
                                                                        rs.getString("CITY"),
                                                                        rs.getString("STATE"),
                                                                        rs.getString("ZIPCODE"),
                                                                        rs.getString("EMAILADDRESS"),
                                                                        rs.getString("PHONENUMBER"),
                                                                        unsubscribeUrl );

                                                 log.info("Sending-" + commid);
                                                 epsl.sendEmail(commid);
                                                 break;
                                          }
                                    }
                              }
                        } // End of While(rs.next())
                  }// End of inner try
                  catch(Exception e)
                  {
                        System.out.println("Exception in office record in whiles: ***"+ e);
                  }
                  newsbastsuccess = 1;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
System.out.println(e);
e.printStackTrace();
        }

        String url = "/secure/admin/newsletter_error.jsp";
        if ( newsbastsuccess == 1 ) {
            url = "/secure/admin/newsletter_sent.jsp";
        }

        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher ( url ) ;
        dispatcher.forward(request, response);
    }
}

The Exception thrown is;
javax.ejb.ObjectNotFoundException: Bean not found in 'findByOfficeType....................'

Appreciate your help on this.

Best Regards
M_SYEDAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leo EikelmanDirector, IT and Business DevelopmentCommented:
Is your Bean associated with a package name?

And where is your bean located?

It should be in WEB-INF/classes/"BeanClassName".java

and when you package it , it should be in

WEB-INF/lib/"BeanClassName".jar


Hope this helps,

Leo

0
mrigankCommented:
The office type that you are passing, does this have valid entries in the database. If not then the exception will be thrown. No exception if the office type returns more than one row.
0
mrigankCommented:
Also

The javadocs say

"The ObjectNotFoundException exception is thrown by a finder method to indicate that the specified EJB object does not exist.

Only the finder methods that are declared to return a single EJB object use this exception. This exception should not be thrown by finder methods that return a collection of EJB objects (they should return an empty collection instead).
"

So you should be getting an empty collection for the same.



One more thing, you said it runs for 6 records, then the exception comes.
Does this mean in one go, the iterations go for 6 loops and then the error.
or
Six times the method is called successfully nd then the error .
?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

M_SYEDAuthor Commented:
Hi,

mrigank: The method calls are in a while loop which runs for each record retrieved from the database.
The error comes for a specific record after the same piece of code has been executed six times, this code includes method calls too.

I hope that answers your question.

I am thinking that it might be a data quality issue, because the program works for most of the records apart from a few which are causing the error.

Thanks a lot for your posts experts. Appreciate them.
0
mrigankCommented:
Try deleting those records from the db and running the program and see if the error recurs.
0
Leo EikelmanDirector, IT and Business DevelopmentCommented:
Is your findByOffice method using getBean(int i) method anywhere in your code?

If you are, try using getBean() instead of getBean(int i)

I don't really see any problem with the code itself.

Cheers,

Leo

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
M_SYEDAuthor Commented:
I think so too. Let me verify the quality of the data in the record thats throwing the error.
0
M_SYEDAuthor Commented:
I am using getBean() in the code and not getBean(int i)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.