resource-ref binding
Hello
I have the following in my web.xml file:
<resource-ref id="ResourceRef_1178568653534">
<res-ref-name>jdbc/Admin</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
my jndi name in code is
java:comp/env/jdbc/Admin
my web application is working fine while accessing the DB. but i have a back ground process running that runs every 30 seconds or so...and connects to the DB. whenever this Background process is connecting to the DB i am getting this exception
javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context"java:".
com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInternal(NameSpace.java:1663)
com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1009)
com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:932)
can anyone tell me what i am missing here? Again, its only this Background process that is giving this exception. if i go from one page to another and in middle the bean is connecting to the DB then it is working fine.
i have no clue what i could be missing.
# 2
the lookup code is same as the other code (that works through webpages). but here it it
Context initCtx;
DataSource dataSource = (DataSource) initCtx.lookup("java:comp/env/jdbc/Admin");
Connection conn = dataSource.getConnection();
conn.setAutoCommit(true);
if i change java:comp/env/jdbc/Admin to jdbc/Admin then it works but i get this warning:
J2CA0122I: Resource reference jdbc/Admin could not be located, so default values of the following are used: [Resource-ref settings]
res-auth:1 (APPLICATION)
res-isolation-level:0 (TRANSACTION_NONE)
res-sharing-scope:true (SHAREABLE)
res-resolution-control:999 (undefined)
[Other attributes]
isCMP1_x:false (not CMP1.x)
isJMS:false (not JMS)
i was getting this error to begin with and upon googling i found a solution of making resource-ref binding in my web.xml file. Which i did. After that i no longer get this warning when making connection to the DB. BUT when connection to the DB is made via this background processor..this is when i get the exception. the Background code is running via a thread every 30 secs. so it is not 'directly' instantiated by a webpage. Hope this information helps u in helping me.
thanks
# 3
This is what might be happening.
When your web application accesses the DB, the thread is the one managed by websphere. So, websphere knows that when you refer to the "local" name of " jdbc/Admin" vi java:comp/env, it maps to the global name of "jdbc/Admin". However, When you start your own background thread, websphere has no idea that the local JNDI name "java:comp/env/jdbc/Admin" maps to which global name. Hence it gives the error.
I do not have a correct solution for you now, but you may want to re-design this so that your background thread does not have to do the DB access.
A possible design apporach may be to write a standalone batch prgram to do so.
Note that this is just a speculation, and there might be a better answer to your problem.
# 5
In that case, you can change your lookup code to something like this:-
boolean localLookupPass=false;
try {
// do local lookup
localLookupPass=true;
} catch(Exception e) {
logger.warn("Local lookup failed",e);
}
if(!localLookupPass) {
// do global lookup
}
This will ensure that you are doing global lookup only in your background thread and not everywhere else.
Also, incase this is a struts application doing action chaining (one action forwarding to another) , you may want to change the sharing-scope to unshareable.