Java connection pool without JNDI


I've a connection pool to access a MySQL DB from a servlet. I get the data source using a JNDI, which is defined on my META-INF/context.xml file.

Everything is working fine, but I have to place the MySQL driver JAR within Tomcat's /common/lib folder, instead of webapp's WEB-INF/lib; otherwise the JNDI will not work (ClassNotFoundException: com.mysql.jdbc.Driver).

Is there any other way to get the datasource, which allows me to place the JAR inside WEB-INF/lib? All the examples I've found over the Internet are using JNDI…

(It's a quite simple application, I'd really prefer not to have to import 53 JARs of some framework in order to solve my problem 🙂


Best Solution

While most of these replies are about pools, I don't think that's your question?

I think the most direct answer to your question is, simply import and use the DataSource implementation provided by your driver. You're using MySQL Connector/J? then it's MysqlDataSource.

There are methods on it to set username, password, etc.

If you don't need it in JNDI, then you don't have to configure it in JNDI via Tomcat. You can keep the driver jar in WEB-INF/lib, sure. If you want pooling around that DataSource, indeed, just use Commons DBCP and Pool.

Here's an example of making your own pooling DataSource out of a given DataSource.