Search code examples
tomcatdatasourcejndi

What JNDI InitialContext implementation should I be using for a web application datasource?


I am just getting started with JNDI for the first time. I am developing a web application using Spring Mvc deployed on tomcat, connecting to a Mysql database.

From my understanding, the reasoning to use a JNDI datasource is wanting to connect to a different database when running the application locally and on the live server, but not wanting to have to change any configuration in order to do this; I retrieve the datasource from JNDI, which gives me a different datasource on different machines.

My question is I am unsure as to what JNDI context I should be using for my java objects? The extent of examples I find online tend to say "retrieve from JNDI" without saying what the JNDI context is.

To elaborate, should I be using LDAP for example? Or should I be using tomcat? If I am using tomcat, what is the point in a local JDNI object as opposed to a global one? Ie, surely the local one means I have to change configuration setting when I want the JNDI objects to be different hence why did I bother configuring my datasource in JNDI in the first place? Intuitively, I would imagine a global tomcat JNDI context would be the best option, but I am just a little sketchy.


Solution

  • As per my experience, you use the same application and not hard-coding any thing in to the code regarding database information and still want to connect to different database when you deploy your application without making any changes.

    For example you move to the DEV -> QA->... -> Prod all might be having same JNDI name however they will be configured differently on the different environment. Use the same war/ear through out without any changes.

    JNDI name configured on tc (changeable per ENV) -- CODE moving over different environments

    JNDI gives you loose-coupling in terms of configuration.