This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes servlet onstart up can't find db resource reference name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet onstart up can Watch "servlet onstart up can New topic
Author

servlet onstart up can't find db resource reference name

Denise Smith
Ranch Hand

Joined: Jul 26, 2005
Posts: 36
This servlet works if I start it manually after the server has stated but not with a scheduler in the servlet. The schduler starts the job. but the job fails with this error.

[6/20/08 15:17:06:016 EDT] 0000002a SystemErr R com.ibm.db.beans.DBException: Unexpected error; javax.naming.NameNotFoundException: Name "comp/env/espConn" not found in context "java:".
at com.ibm.db.beans.DBException.getSQLException(DBException.java)
at com.ibm.db.beans.DBSelect.execute(DBSelect.java)
at net.ongov.esp.resources.ExamPostedTodayByJobtitle.execute(ExamPostedTodayByJobtitle.java:45)
at net.ongov.esp.resources.ScheduledExamNotification.scheduleJob(ScheduledExamNotification.java:104)
at net.ongov.esp.resources.ScheduledExamNotification$1.run(ScheduledExamNotification.java:308)
at java.util.TimerThread.mainLoop(Timer.java:447)
at java.util.TimerThread.run(Timer.java:397)

Why can't the job find the db resource reference name when it starts on it's own?


Surround yourself with learners...They will educate you!
Murthy Tanniru
Greenhorn

Joined: Jun 03, 2008
Posts: 14
may be the scheduler in a different context
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Originally posted by Denise Smith:
This servlet works if I start it manually after the server has stated but not with a scheduler in the servlet. The schduler starts the job. but the job fails with this error.

[6/20/08 15:17:06:016 EDT] 0000002a SystemErr R com.ibm.db.beans.DBException: Unexpected error; javax.naming.NameNotFoundException: Name "comp/env/espConn" not found in context "java:".
at com.ibm.db.beans.DBException.getSQLException(DBException.java)
at com.ibm.db.beans.DBSelect.execute(DBSelect.java)
at net.ongov.esp.resources.ExamPostedTodayByJobtitle.execute(ExamPostedTodayByJobtitle.java:45)
at net.ongov.esp.resources.ScheduledExamNotification.scheduleJob(ScheduledExamNotification.java:104)
at net.ongov.esp.resources.ScheduledExamNotification$1.run(ScheduledExamNotification.java:308)
at java.util.TimerThread.mainLoop(Timer.java:447)
at java.util.TimerThread.run(Timer.java:397)

Why can't the job find the db resource reference name when it starts on it's own?


Perhaps the name is not yet bound by the time the scheduler starts ? Exactly when does the name get bound in the JNDI ? What server is this and what scheduler is it ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Denise Smith
Ranch Hand

Joined: Jul 26, 2005
Posts: 36
Originally posted by Murthy Tanniru:
may be the scheduler in a different context


I think Murthy may be correct I spawned a new thread in the init() method of a load on start up servlet. That thread is outside the current context.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Originally posted by Denise Smith:


I think Murthy may be correct I spawned a new thread in the init() method of a load on start up servlet. That thread is outside the current context.


I am not sure I follow. You have 2 JNDI contexts ? If the scheduler has access to the same JNDI that the name is bound to, there should be no problem accessing the bound object
Denise Smith
Ranch Hand

Joined: Jul 26, 2005
Posts: 36
Originally posted by John Meyers:


I am not sure I follow. You have 2 JNDI contexts ? If the scheduler has access to the same JNDI that the name is bound to, there should be no problem accessing the bound object


actually...the thread code doesn't know the context information and I don't know how to tell it.
when I setDataSourcename this is the error I get

select.setDataSourceName((String)new InitialContext().lookup("java:comp/env/espConn"));

[6/23/08 12:10:40:562 EDT] 0000003c SystemErr R javax.naming.NameNotFoundException: Name "comp/env/espConn" not found in context "java:".
at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1094)
at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:990)
at com.ibm.ws.naming.urlbase.UrlContextImpl.lookup(UrlContextImpl.java:1263)
at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:201)
at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:142)
at javax.naming.InitialContext.lookup(InitialContext.java:361)
at net.ongov.esp.resources.LocalExamSeriesForExamPostedToday.initializer(LocalExamSeriesForExamPostedToday.java:44)
at net.ongov.esp.resources.LocalExamSeriesForExamPostedToday.<init>(LocalExamSeriesForExamPostedToday.java:27)
at net.ongov.esp.resources.ScheduledExamNotification.scheduleJob(ScheduledExamNotification.java:100)
at net.ongov.esp.resources.ScheduledExamNotification$1.run(ScheduledExamNotification.java:316)
at java.util.TimerThread.mainLoop(Timer.java:447)
at java.util.TimerThread.run(Timer.java:397)


Any suggestions? how can I get the context that the servlet is using
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

If I am not mistaken, you are trying to access a bound datasource in the JNDI on application startup ? Why not do that in a servlet context listener ? That may be a better place to put the code and schedule whatever you want to. If this is supposed to run like a batch, you should ask yourself if it belongs in a web application.

If you have a JNDI explorer you can also see if the name is actually bound when you are looking for it.
Denise Smith
Ranch Hand

Joined: Jul 26, 2005
Posts: 36
Here is my init method of the servlet: this executes the class ReadMail and the method scheduleJob();

*/
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub

//Use the schedule() method of the java.util.Timer class:
ServletContext sc = arg0.getServletContext();


System.out.println("Initializing Scheduler for Notifications");
long now = System.currentTimeMillis();
long millisecondsInFuture = 30000 ;
Date whenToRun = new Date(now + millisecondsInFuture);
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
// job code here
String[] args = {};
try {
ReadMail.main(args);
scheduleJob();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
};
timer.schedule(task, whenToRun);
}


ReadMail workds great(there are no data access beans in this class)
the scheduleJob(); starts to run but as soon as it hits tries to get the datasource it errors with the error provided previously.

here is the the Try block that cathes the Naming exception:

try {

select.setDataSourceName((String)new InitialContext().lookup("java:comp/env/espConn"));

select.setCommand("some select statement");
DBParameterMetaData parmMetaData = ParameterMetaData();
parmMetaData.setParameter(1, "somevariable",
java.sql.DatabaseMetaData.procedureColumnIn,
java.sql.Types.CHAR, String.class);
}


can anyone help me???

If I run my code without spawning a new thread it works
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet onstart up can't find db resource reference name