commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nathan Ewing" <>
Subject DBCP & mysql hanging after a period of time
Date Tue, 30 Mar 2004 22:46:47 GMT
I've been developing a website for about 6 months now, and throughout
the development DBCP has been giving me trouble.  If I leave the site
for a while, randomly if I come back to it, when it tries to access the
database it just hangs there forever (there is no discernable pattern to
this).  I'm certain I am closing all my connections, statements, and
whatnot during each operation.  After it starts hanging, all future
accesses to the database hang forever as well, until I reset my
webserver.  I've tried everything I can think of, and checked every
resource on the web I could find, for the last six months looking.  This
is my last resort before I am forced to write my own connection pooling
software (which I really don't want to do).  I'm using version 3.0.11 of
the mysql driver and version 1.1 of dbcp.

I tried to look into the memory during the hanging operation, and it
appears that maxActive=8 and numActive=8, so I'm guessing that the pool
is not correctly freeing up connections.  When I check the mysql admin
the number of processes seems to be 0.  It seems like DBCP is collecting
phantom connections or something.  I'm really not sure.

Please help!

Below is the offending code, the program is hanging when I do

import java.sql.DriverManager;
import java.sql.Connection;
import javax.sql.DataSource;


import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;

public class ConnectionManager {
	private static final ConnectionManager INSTANCE = new
	//Name of SQL Driver to load
	private static final String DRIVERNAME =
	private static final String CONNECTSTRING =
	//The actual pool of Database Connections
	private static DataSource dataSource;
	private static ObjectPool connectionPool;
	// Private constructor supresses 
	// default public constructor
	private ConnectionManager( ) {
		// Load the Database SQL Driver
		} catch (java.lang.ClassNotFoundException e)
{System.out.println ("Database SQL Driver not found!");}
		try {
			// First, we'll need a ObjectPool that serves as
			// actual pool of connections.
			// We'll use a GenericObjectPool instance,
			// any ObjectPool implementation will suffice.
			connectionPool = new GenericObjectPool(null);
			// Next, we'll create a ConnectionFactory that
			// pool will use to create Connections.
			// We'll use the DriverManagerConnectionFactory,
			// using the connect string passed in the
command line
			// arguments.
			ConnectionFactory connectionFactory = new
			// Now we'll create the
PoolableConnectionFactory, which wraps
			// the "real" Connections created by the
ConnectionFactory with
			// the classes that implement the pooling
poolableConnectionFactory = new
			// Finally, we create the PoolingDriver itself,
			// passing in the object pool we created.
			// We then save the datasource object to the
class for later use in getting connections
			dataSource = new
			//System.out.println("Idle: " +
connectionPool.getNumIdle() + " - Active: " +
			} catch (Exception e){}
	//Returns and Instance of the ConnectionManager object (not
really necessary)
	public static ConnectionManager getInstance( ) {
		return INSTANCE;
	//Returns a database connection object
	public static Connection getConnection(){
		Connection conn = null;
			conn = dataSource.getConnection();	
			} catch(java.sql.SQLException e){
			    System.err.print (e.getStackTrace());
		//System.out.println("Idle: " +
connectionPool.getNumIdle() + " - Active: " +
		return conn;

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message