I am reading Spring in Action third edition. I am on chapter 6 data access with Spring. I have a question while configuring a datasource
we can use the option JNDI Datasource. In the spring configuration file, we give something like
It says that <resource-ref> true prepends java:comp/env/. My question is what does resource-ref mean and why do we use it?
You have already answered your own question of 'what it does'.
The reason this is needed is because the JEE spec does not standardize JNDI naming conventions hence different application servers follow different rules. Some like Tomcat bind to the JNDI tree with the java:/comp/env/ prefix so in this case you would set this flag to true. It is really application server specific. If you are only going to support one application server it is just a matter of figuring out which one is right and defining it correctly.
Amazing. Thankyou very much. So it means that we set it to true or not, it's just a matter of choice as such kind of namespace
convention will be separate for every server. Am i intercepting correct?
It is recommended but not required by J2EE to use this convention. So it is possible that it will be bound differently by the application server in which case resource-ref="true" would not find your JDNI in that context. However in my experience usually setting this to true is the safest bet.
From the API docs
JNDI names may or may not include the "java:comp/env/" prefix expected by J2EE applications when accessing a locally mapped (ENC - Environmental Naming Context) resource. If it doesn't, the "java:comp/env/" prefix will be prepended if the "resourceRef" property is true (the default is false) and no other scheme (e.g. "java:") is given.