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 does not work. Is it possible, and if yes: how?ic.lookup("java:comp/env/service/test")
If you created your custom-resources in your domain.xml, maybe you forgot to add them as a resource-ref under the servers/server tag in your domain.xml:
<resources>
<!-- JDBC RESOURCE -->
...
<custom-resource res-type="java.lang.String" jndi-name="MyCustomStringResource"
factory-class="org.glassfish.resources.custom.factory.PrimitivesAndStringFactory">
<property name="value" value="hello, this is a string resource"></property>
</custom-resource>
...
</resources>
<servers>
<server name="server" config-ref="server-config">
<application-ref ref="__admingui" virtual-servers="__asadmin"></application-ref>
...
<resource-ref ref="MyCustomStringResource"></resource-ref>
...
</server>
</servers>
The prefix indicates the usage of a resource reference which has been defined for the Java EE 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With