Search code examples
javatomcatjndi

Who performs the connection on a JNDI DataSource configured in the server?


I'm deploying a WAR into a Tomcat that has a datasource (Oracle) defined in its context.xml.

My app accesses it by the jndi name. Do I need to embed the jar driver into the war? Or do I just need to has it in Tomcat's classpath?

--edit--

I explained a little bit wrong. I'm providing it in the war and it's working.
But my final doubt I think is if the connection was made by the server itself or the jndi simply gave the data to the application to perform it.
The driver provided in the war is confusing me in this behaviour

Thanks


Solution

  • You shouldn't add the driver to your WAR file in this case, rather deploy it to tomcat's lib directory. Reason: The server opens and maintains the connection, so the server itself needs access to the driver, not only the application.

    If you also add the driver to the application, you risk both versions diverting and might end up with duplicate classes on the classpath, which is never fun debugging. Include it once, in the position where it's definitely needed (the server) and remove it from your WAR file.