Search code examples
javamysqljdbccompressionconnection-pooling

MySQL traffic compression with connection pool


I am trying to implement Connection Pooling using Commons Pool and DBCP. I am able to establish a connection to the database and everything works as expected, except traffic compression.
This is my configuration:
- Commons DBCP 1.4
- Commons Pool 1.6
- MySQL Connector Java 5.1.21
- MySQL Server 5.5 on Windows Server 2008
- Java 1.6

Please consider this code snippet to instantiate the connection pool:

private void setupDataSource(Hashtable<String, String> data) {
    try
    {
        GenericObjectPool<Connection> pool = new GenericObjectPool<Connection>();
        pool.setMinIdle(5);
        pool.setMaxActive(-1);
        pool.setTestOnBorrow(true);
        pool.setTimeBetweenEvictionRunsMillis(5000);
        pool.setMinEvictableIdleTimeMillis(300000);
        Properties props = new Properties();
        props.setProperty("user", "root");
        props.setProperty("password", "password");
        props.setProperty("useCompression", "true");

        ConnectionFactory cf = new DriverConnectionFactory(new com.mysql.jdbc.Driver(),
                    "jdbc:mysql://" + ip + ":" + port,
                    props);

        KeyedObjectPoolFactory<String, GenericObjectPool<Connection>> kopf =new GenericKeyedObjectPoolFactory<String, GenericObjectPool<Connection>>(null, 20, GenericObjectPool.WHEN_EXHAUSTED_GROW, 20, true, false);
        PoolableConnectionFactory pcf =  new PoolableConnectionFactory(cf,
                pool,
                kopf,
                "Select 1;",
                false,
                true);

        PoolingDriver pd = new PoolingDriver();
        pd.registerPool("MyPool", pool);
    }catch (Exception ex)
    {
        ex.printStackTrace();
    }
}

Then, to obtain a connection i use:

Connection conn = java.sql.DriverManager.getConnection("jdbc:apache:commons:dbcp:MyPool");

First, is there another way to check if compression works, except comparing the outgoing network traffic in MSQL Tools?

And what needs to be changed in order to enable compression?

Thanks for your help

klib


Solution

  • Now, after almost one year, I have tested the latest Connector/J (5.1.25) and the problem does not appear anymore. Since I did not change anything, it looks like connector 5.1.21 was kind of buggy. BTW, there was no real Bugfix in the release notes, they only 'improved compression behaviour'.