Memory leak with c3p0 using ConnectionProperties

Achim Abeling <[email protected]>
Newsgroups gmane.comp.db.mysql.java
Message-ID <[email protected]>
Hi,

I think I found a memory leak in mysql-connector-3.1.11.

Perhaps the cause for the memory leak is in c3p0, but since the memory 
leak goes away when the JDBC url is changed I first post the problem here.

A class to reproduce the problem is attached.
I am using mysql-connector-3.1.11 and c3p0 version 0.9.0 with JDK 1.5.0.

c3p0 has the possibility to expire idling connections. The test case 
sets an expire time of 1 second.
The log shows that c3p0 really expires the connections and creates new 
connections.

When the JDBC url is
"jdbc:mysql://localhost/test?profileSQL=true"
the memory leak occurs, when it is
"jdbc:mysql://localhost/test"
it occurs not.

The memory leak can be observed by starting the class and then doing a
# jmap -histo <processid> |grep "com.mysql.jdbc.Connection"

On my linux workstation it produces something like this:

Attaching to process ID 14539, please wait...
Debugger attached successfully.
Client compiler detected.
JVM version is 1.5.0-b64
Iterating over heap. This may take a while...
1180872 16401 
com.mysql.jdbc.ConnectionProperties$BooleanConnectionProperty
277200  3465 
com.mysql.jdbc.ConnectionProperties$IntegerConnectionProperty
182952  2541    com.mysql.jdbc.ConnectionProperties$StringConnectionProperty
168168  231     com.mysql.jdbc.Connection
36960   462 
com.mysql.jdbc.ConnectionProperties$MemorySizeConnectionProperty
Heap traversal took 9.15 seconds.

The second number in each line is the object count. And it is increasing.

Greetings
Achim


-- 
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe:    http://lists.mysql.com/[email protected]
C3P0MemoryLeakTest.java (text/plain, 1.4 KB)
package de.freenet.xselling.db;

import java.util.Properties;

import java.sql.Connection;
import java.sql.Statement;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.DataSources;
import com.mchange.v2.c3p0.PoolConfig;

public class C3P0MemoryLeakTest
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
		throws Exception
	{

		PoolConfig pcfg = new PoolConfig();
		pcfg.setInitialPoolSize(10);
		pcfg.setMinPoolSize(10);
		pcfg.setMaxPoolSize(30);
		pcfg.setAcquireIncrement(1);
		/* idle connections expire after 1 second */
		pcfg.setMaxIdleTime(1);
		pcfg.setMaxStatements(0);
		pcfg.setIdleConnectionTestPeriod(30);

		Class.forName("com.mysql.jdbc.Driver");
		
		/**
		 * This URL produces a memory leak
		 */
		String jdbcUrl = "jdbc:mysql://localhost/test?profileSQL=true";
		/**
		 * This one not.
		 */
//		String jdbcUrl = "jdbc:mysql://localhost/test";
		Properties connectionProps = new Properties();
		connectionProps.put("user","root");
		DataSource unpooled = DataSources.unpooledDataSource(
				jdbcUrl, connectionProps);
		DataSource ds = DataSources.pooledDataSource(unpooled, pcfg);
		
		while (true) {
			Thread.sleep(5000);
			System.out.println(System.currentTimeMillis()+": doing testquery");
			Connection conn = ds.getConnection();
			Statement stmt = conn.createStatement();
			stmt.executeQuery("select * from c3p0_testtable");
			conn.close();
			System.gc();
		}
	}

}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.