Reputation: 497
I am using Tomcat 7.0.53
I have an application needing to load mysql-connector-java-5.0.8.jar for it's JDBC connection. If I put the connector jar in {$catalina_home}/(app instance)/lib/ directory, everything loads fine. If I import the jar to my war using maven or put the jar out at {$catalina_home/lib/ then it will not load.
I have a fairly typical war setup, and the imported jar(s) are placed in /WEB-INF/lib/ directory. There is however one difference from projects I usually work on. The context file for the different datasources is located as an explicit file in webapp/{app}/META-INF/context.xml.
Ideally I would want to put the connector jar in my war using maven, so if someone could tell me: 1. Why this is happening? 2. A possible fix so the connector jar could be imported via maven?
it would be greatly appreciated.
Upvotes: 0
Views: 1030
Reputation: 497
There are two parts to the solution.
Parts of my context files are filled in with properties located on catalina.properties. Since catalina.properties gets loaded with the instance classloader, the connector jar would have to be loaded on the same classpath to be seen. That's why I couldn't put the connector jar in TOMCAT_HOME/lib/
In my context I am calling out the tomcat factory, factory="org.apache.tomcat.jdbc.pool.DataSourceFactory", I was not loading the required dependency for that factory in my application with the connector jar dependency. Once I added the below dependencies, the issue was resolved.
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
</dependency>
Upvotes: 0
Reputation: 6497
When you have Tomcat managing your DataSource and making available via JNDI, it uses its own class loader to do the initialization, rather than the class loader that is uses for the webapp itself. This means that for the purposes of initializing the DataSource, all the classes that are required must be in Tomcat's classpath (e.g., in its lib directory).
Tomcat does this to insulate the container from potentially buggy or malicious web apps and facilitates the ability to load and unload web apps.
Upvotes: 0