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.

[1332 byte] By [bhaarat_javaa] at [2007-11-27 3:37:16]
# 1
Can you please post the lookup code as well as the complete stack trace?
Samarth.Bhargavaa at 2007-7-12 8:40:33 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 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

bhaarat_javaa at 2007-7-12 8:40:33 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 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.

Samarth.Bhargavaa at 2007-7-12 8:40:33 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
you are right. that maybe whats happening. but its not possible for me to redisgn the application at this point. I'd rather just deal with the warning message that says 'default values are loaded'. i will still search for a solution though.
bhaarat_javaa at 2007-7-12 8:40:33 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 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.

Samarth.Bhargavaa at 2007-7-12 8:40:33 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
thanks..what exactly do u mean by 'global' lookup. meaning just have jdbc/Admin...?so in cases where the background processor is accessing the DB i will see the warning message and in other cases i wont.
bhaarat_javaa at 2007-7-12 8:40:33 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
That is correct.
Samarth.Bhargavaa at 2007-7-12 8:40:33 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
thanks!also, referring to your first post. i just want to say taht the background process is also running IN websphere under the same applications. ...just to be clear
bhaarat_javaa at 2007-7-12 8:40:33 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...