jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes How to control no. of JDBC Connections in Tomcat!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "How to control no. of JDBC Connections in Tomcat!!!" Watch "How to control no. of JDBC Connections in Tomcat!!!" New topic

How to control no. of JDBC Connections in Tomcat!!!

Sonal Sharma
Ranch Hand

Joined: May 13, 2006
Posts: 62
Dear friends,
I want to know a way to configure/restrict no of JDBC connections that are available in a connectionpool which has been created programmatically thru my web application.Can we control this functionality through Tomcat in which my application is hosted.

My program for Connection Pooling is:

package com.agcl.bluedart.dao;
import java.io.*;
import java.sql.*;
import java.util.*;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import com.agcl.bluedart.util.*;

public class SQLDBConPool {

public static Connection con;
public static ConnectionFactory connectionFactory;
public static PoolingDataSource dataSource;

public static String JdbcDriver=null;
public static String JdbcUrl=null;
public static String JdbcUserId=null;
public static String JdbcPassword=null;

AgentLogger lh = new AgentLogger();
DateTimeCapture gd = new DateTimeCapture();
PropertyReader pr = new PropertyReader();
StringBuffer sbLog = null;

public SQLDBConPool(){
sbLog = null;
sbLog = new StringBuffer();


JdbcDriver = pr.getSQLServerJdbcDriver();
JdbcUrl = "jdbc:microsoft:sqlserver://"+pr.getSQLServerIpAddress()+":"+pr.getSQLServerPort()+";DatabaseName="+pr.getSQLServerDBName()+"";
JdbcUserId = pr.getSQLServerDBUserName();
JdbcPassword = pr.getSQLServerDBPassword();

}catch(Exception e){
sbLog.append("["+gd.getCurrentDate()+"] || [com.agcl.cti.DAO.DBConnPool ] || [DBConnPool] || [ERROR] || DB Connection"+e.getMessage()+".\n");

GenericObjectPool connectionPool = new GenericObjectPool ( null );
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory ( JdbcUrl,JdbcUserId,JdbcPassword );
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory ( connectionFactory,connectionPool,null,null,false,true );
dataSource = new PoolingDataSource ( connectionPool );
public static Connection getConnection() throws SQLException
SQLDBConPool db = new SQLDBConPool ();
con = dataSource.getConnection ();
return con;
/*public static void main(String[] args)

try {
Connection con=SQLDBConPool.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block

David O'Meara

Joined: Mar 06, 2001
Posts: 13459

You're already using DBCP!!!
You should configure it through Tomcat and define it there!!!

regards, Dave!!!
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Look at the javadoc for Apache's DBCP there is a method to set the maximum number of connections in the pool , that is the pool size.

As suggested by David O'Meara , Tomcat already has this feature and in case you configure the connection pool at Tomcal level then pool will be taken care by Tomcat and using JNDI you can make use of the connection pool from within your application.


Rahul Bhattacharjee
LinkedIn - Blog
Sonal Sharma
Ranch Hand

Joined: May 13, 2006
Posts: 62
Dear friends,
Cann't we set the Connection pool size manually in the code snippet that I have already provided so that I don't have to make any changes anywhere.

Thanks and regards.
Don't get me started about those stupid light bulbs.
subject: How to control no. of JDBC Connections in Tomcat!!!
It's not a secret anymore!