Tomcat Connection Pooling

With tomcat you can have Jdbc connection pooling.
You can use dbcp however there is a tomcat connection pool implementation.

Before getting started you have to put the database driver jar to the
tomcat/libs directory

For example if you use mysql you need to place the mysql-connector-java.jar inside the tomcat/libs directory

The configuration is pretty much the same however in order to use the tomcat connection pool you need to specify org.apache.tomcat.jdbc.pool.DataSourceFactory at the factory attribute.

On your META-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
        <Resource type="javax.sql.DataSource"
            name="jdbc/baseDB"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/gkatziouras"
            username="asuser"
            password="apss"/>
</Context>

And of course you add your choice of connection pool size, max open connections, idle connections etc.

Then you can get the DataSource object and of course a connection. which you have to close after you have your work done in order to release it back to the pool.

Context context = new InitialContext();
DataSource ds = (DataSource) context.lookup( "java:/comp/env/jdbc/baseDB" );	
Connection connection = ds.getConnection();

More on Tomcat JDBC Connection Pool

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.