views:

954

answers:

2

I've created some JDBC resources and Custom resources in GlassFish. I named JDBC resource jdbc/mydb and the Custom resource service/test.

The JDBC connection is lookedup with the call ic.lookup("java:comp/env/jdbc/mydb"). It can also be found with ic.lookup("jdbc/mydb"), which has some disadvantages, see answer of Robin below.

But my custom resource has to be lookedup with ic.lookup("service/test"). The line ic.lookup("java:comp/env/service/test") does not work. Is it possible, and if yes: how?

+2  A: 

The prefix indicates the usage of a resource reference which has been defined for the JEE entity from which the JNDI lookup is being made. You would have to define such a reference for you custom resource as well to look it up the same way.

The prefix is not necessary for the JDBC lookup either, as you can use the full jndi name instead. The problem with that approach, is if you move the resource such that it is no longer located in the local initial context, you will not find it without changing the lookup name in your code to include the location information. Using a reference shields your code from this, so only the mapping in the reference would change.

Some info here on usage

Robin
OK. But what do I need to define, so I can use the 'java:com:env/'-prefix for custom resources as well? I didn't do somehtings special for the JDBC, so GlassFish/NetBeans must be providing for this. I only added resources in my web.config and resource reference in sun-web.xml.
doekman
Sorry, your info points to IBM info which is not vendor independent...
doekman
No, it isn't, but the principles for how it is used are. The configuration examples are specific to ibm and the specific protocols being used find resources. Not sure if this can be vendor neutral, you would have to determine how to do it with your app server of choice.
Robin
A: 

I am not sure what Glassfish means by 'custom resource'. The JEE specification says that the 'java:comp/env' namespace is reserved for resource lookups which are bound only for that particular application. So

ic.lookup("java:comp/env/jdbc/mydb")

actually means that jdbc/mydb is a name binding for the application (EJB/servlet etc) that you are doing the lookup from. Doing a direct lookup without using the java:comp/env prefix ties down your application to the server environment that you are deploying on.

talonx