Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sonal Sharma
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're already using DBCP!!!
You should configure it through Tomcat and define it there!!!

regards, Dave!!!
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sonal Sharma
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic