• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 525
  • Last Modified:

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
0
M_SYED
Asked:
M_SYED
  • 3
  • 3
  • 2
1 Solution
 
Leo EikelmanCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 EikelmanCommented:
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
 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now