aspose file tools*
The moose likes JDBC 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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to control no. of JDBC Connections in Tomcat!!!" Watch "How to control no. of JDBC Connections in Tomcat!!!" New topic
Author

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();

try{

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


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

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
e.printStackTrace();
}

}*/
}
David O'Meara
Rancher

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.

http://tomcat.apache.org/tomcat-5.0-doc/jndi-datasource-examples-howto.html


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!!!