MaxStocker.com   MaxStocker.com    
   
Home About Blog Stuff Contact
 
   
 

May 2009

The other day...
Posted : Thu May 28th

Added Java to Category List
Posted : Tue May 26th

Proper exception handling can't wait
Posted : Mon May 25th

Connection woes
Posted : Sat May 23rd

Why I hate Apple
Posted : Wed May 20th

Extracting DOCX content with Java
Posted : Tue May 19th

I notice
Posted : Fri May 15th

JDBC Best Practices
Posted : Fri May 15th

Simple PHP RSS loader
Posted : Sun May 10th

MySQL and DBCP for Tomcat 5
Posted : Sat May 9th

Tracing 316.70.50.1
Posted : Mon May 4th

The flip side
Posted : Tue April 28th

Starting to irritate me
Posted : Fri April 24th

As seen on the internet
Posted : Wed April 22nd

Recent Comments

Max in Whose blog is it anyway?
on Mon May 10th

Rob in Whose blog is it anyway?
on Fri May 7th

Anonymous in SEO and the magic beans
on Thu April 8th

Max in SEO and the magic beans
on Thu April 8th

n.o. in SEO and the magic beans
on Thu April 8th

silky in Right way, wrong way
on Fri February 19th

Categories

Technical
69 Entries

Java
23 Entries

Security
18 Entries

Privacy
6 Entries

Database
11 Entries

Internet
58 Entries

Business
31 Entries

Site Updates
19 Entries

Personal
86 Entries

RSS Feed RSS Feed

Tag Cloud

MySQL and DBCP for Tomcat 5
Posted : Saturday May 9th, 2009

I find that between each version of Tomcat working totally differently and the fact that the documentation is wildly inconsistent and often misleading setting up a connection pool on Tomcat can be a big headache. On top of everything else I found when Googling that you get all these hits that aren't always totally useful or point you at the documentation that's not quite all there in the first place. Now that I got a DBCP pool up and working on Tomcat 5.5 with MySQL I thought I'd describe it here.

So if you're looking for help with a Tomcat 5.5 DBCP and MySQL connection pool you are in the right place. If you have another Tomcat version sorry but you're likely out of luck. Again different versions have different ways of doing all this.

Step One - Get all the Jars in the right place

You need two jars for a DBCP connection pool with MySQL. One is the DBCP jar called naming-factory-dbcp.jar. Tomcat seems to ship with this now so you should have it. It should be in CATALINA_HOME/common/lib

The second jar is a MySQL connector jar with the MySQL JDBC driver. When you download this jar make sure you download the correct (aka non debugging) driver. This jar also goes into CATALINA_HOME/common/lib

Step Two - configure your context

There are a couple of ways to configure your context (see the Tomcat docs for alternatives) but the way I will describe here is to create your own context.xml. This goes into your META-INF directory in your project, WAR and when deployed. For this example imagine the context is named DBTest and in a similarly named code base. Here are the contents for your context




auth="Container"
type="javax.sql.DataSource"
username="USERNAME"
password="PASSWORD"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://HOSTNAME:3306/DATABASENAME"
validationQuery="select 1"
maxActive="5"
maxIdle="2"/>

Step Three - configure your web.xml

You need to add the following to the web.xml for your application. This snippet goes inside the web-app element of your web.xml.


jdbc/mypool
javax.sql.DataSource
Container

Step Four - write some testing code

Here is a small snippet of code showing how to get a java.sql.Connection using what has been defined above. Note how the name above "jdbc/mypool" is translated into one we can lookup from the InitialContext.

Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/mypool");
Connection conn = ds.getConnection();

Step Five - Deploy

You can now build and deploy your web application to Tomcat 5.5 and you should now have a functional DBCP connection pool running to MySQL.

I hope this helps someone, I think it would have helped me.

Tags

DBCP  MySQL  pool  Tomcat5 

Categories

Technical  Java  Internet 

Comments

 
   
  Follow me on Twitter   My Facebook Profile   My LinkedIn Profile   RSS feed of my blog Home   |   About   |   Blog   |   Stuff   |   Contact   |   Privacy Policy  
   
  © 2008 Max Stocker