M_SYED
asked on
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.ObjectNotFoundE xception: Bean not found in ................"
My program is;
public class NewsletterTestServlet extends HttpServlet
{
private static Logger log = Logger.getLogger(Newslette rTestServl et.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().getSe rvletConte xt().getRe sourceAsSt ream(
"/WEB-INF/newsletter.xslt" );
int newsbastsuccess = 0;
try
{
EcpPortalSessionLocal epsl = (EcpPortalSessionLocal) request.getSession().getAt tribute(
AcuvueConstants.EcpPortalS essionKey) ;
// Get all custom newsletters and iterate
String unsubscribeUrlBase = "http://" + request.getServerName() + ":" + request.getServerPort()
+ request.getContextPath() + "/servlet/unsubscribe.do?" ;
//Collection newsletters = CustomNewsletterTypeUtil.g etLocalHom e().findAl l();
//SELECT OFFICEPK FROM CUSTOMNEWSLETTER
Connection con = AcuvueFunctions.getConnect ion();
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( ).findByOf fice(offic e);
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.getUserTypeLocal Value();
Collection subscriptions = UserSubscriptionTypeUtil.g etLocalHom e().findUs erSubscrip tionTypeLo calsByUser (user.getP rimaryKey( ));
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() ;
UserSubscriptionTypeLocalV alue userSub = ustl.getUserSubscriptionTy peLocalVal ue();
System.out.println("Inside second while here 3");
if (AcuvueEJBConstants.UserSu bscription StatusSubs cribed.equ als(userSu b.getStatu s())
&& rs.getString("SUBSCRIPTION PK").equal s(userSub. getSubscri ptionTypeL ocalValue( ).getPrima ryKey()))
{
System.out.println("The Subscribed user is:****"+ user);
String unsubscribeUrl = unsubscribeUrlBase + "userpk=" + user.getPrimaryKey()
+ "&newsletterpk=" + rs.getString("CUSTOMNEWSLE TTERPK");
//Create email and send
String commid = emailGenerator.generateNew sletterEma il(
true,
rs.getString("NEWSLETTERNA ME"),
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("Except ion 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().getReq uestDispat cher ( url ) ;
dispatcher.forward(request , response);
}
}
The Exception thrown is;
javax.ejb.ObjectNotFoundEx ception: Bean not found in 'findByOfficeType......... .......... .'
Appreciate your help on this.
Best Regards
My program is;
public class NewsletterTestServlet extends HttpServlet
{
private static Logger log = Logger.getLogger(Newslette
/**
* 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().getSe
"/WEB-INF/newsletter.xslt"
int newsbastsuccess = 0;
try
{
EcpPortalSessionLocal epsl = (EcpPortalSessionLocal) request.getSession().getAt
AcuvueConstants.EcpPortalS
// Get all custom newsletters and iterate
String unsubscribeUrlBase = "http://" + request.getServerName() + ":" + request.getServerPort()
+ request.getContextPath() + "/servlet/unsubscribe.do?"
//Collection newsletters = CustomNewsletterTypeUtil.g
//SELECT OFFICEPK FROM CUSTOMNEWSLETTER
Connection con = AcuvueFunctions.getConnect
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
// Get all users for this office and
// see if user is subscribed to the newsletter
System.out.println("Inside
Collection users = UserTypeUtil.getLocalHome(
System.out.println("Inside
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.getUserTypeLocal
Collection subscriptions = UserSubscriptionTypeUtil.g
log.info(user.getUserName(
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
UserSubscriptionTypeLocalV
System.out.println("Inside
if (AcuvueEJBConstants.UserSu
&& rs.getString("SUBSCRIPTION
{
System.out.println("The Subscribed user is:****"+ user);
String unsubscribeUrl = unsubscribeUrlBase + "userpk=" + user.getPrimaryKey()
+ "&newsletterpk=" + rs.getString("CUSTOMNEWSLE
//Create email and send
String commid = emailGenerator.generateNew
true,
rs.getString("NEWSLETTERNA
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("Except
}
newsbastsuccess = 1;
} catch (Exception e) {
log.error(e.getMessage(), e);
System.out.println(e);
e.printStackTrace();
}
String url = "/secure/admin/newsletter_
if ( newsbastsuccess == 1 ) {
url = "/secure/admin/newsletter_
}
RequestDispatcher dispatcher = getServletContext().getReq
dispatcher.forward(request
}
}
The Exception thrown is;
javax.ejb.ObjectNotFoundEx
Appreciate your help on this.
Best Regards
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.
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 .
?
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 .
?
ASKER
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.
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.
Try deleting those records from the db and running the program and see if the error recurs.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
I think so too. Let me verify the quality of the data in the record thats throwing the error.
ASKER
I am using getBean() in the code and not getBean(int i)
And where is your bean located?
It should be in WEB-INF/classes/"BeanClass
and when you package it , it should be in
WEB-INF/lib/"BeanClassName
Hope this helps,
Leo