0

I'm using c3p0 connection pool for communication between my (unsigned) Java applet and MySql db. I have this code for initializing pool:

DataSource pool = pools.get(db_name);
if(pool == null) 
    pool = initializePool(db_name);
return pool.getConnection();

Initialize pool method looks like this:

protected DataSource initializePool(String db_name) throws SQLException {
    try {
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass("com.mysql.jdbc.Driver");
        cpds.setJdbcUrl(connectionDetails.getConnectionString(db_name));
        cpds.setUser(connectionDetails.getUsername()); 
        cpds.setPassword(connectionDetails.getPassword());
        pools.put(db_name, cpds);
        return cpds;
    } catch (PropertyVetoException e) {
        e.printStackTrace();
        throw new SQLException("JDBC driver could not be initialized propertly");   
    }
}

My c3p0-config.xml is simple:

<c3p0-config><default-config>
<property name="acquireIncrement">1</property>
<property name="initialPoolSize">5</property>
<property name="minPoolSize">1</property>
<property name="maxPoolSize">7</property>
<property name="maxConnectionAge">0</property>
<property name="maxIdleTime">600</property>
<property name="maxIdleTimeExcessConnections">120</property> 
<property name="automaticTestTable">C3p0_test_table</property>
<property name="idleConnectionTestPeriod">60</property>
<property name="testConnectionOnCheckin">true</property></default-config></c3p0-config>

The problem is that "getConnection" call takes sometimes even 10 or more seconds to respond when i call a database for the first time (second time i contact a db it's faster). I've been looking all over the net and "c3p0" manual for way to speedup connection pool without luck.

When i run my applet (from server) java console output looks like this:

Jan 16, 2013 12:57:14 PM com.mchange.v2.log.MLog <clinit>
INFO: MLog clients using java 1.4+ standard logging.
Jan 16, 2013 12:57:16 PM com.mchange.v2.c3p0.C3P0Registry <clinit>
INFO: jdk1.5 management interfaces unavailable... JMX support disabled.
java.security.AccessControlException: access denied ("javax.management.MBeanServerPermission" "createMBeanServer")
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.management.ManagementFactory.getPlatformMBeanServer(Unknown Source)
    at com.mchange.v2.c3p0.management.ActiveManagementCoordinator.<init>(ActiveManagementCoordinator.java:42)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at com.mchange.v2.c3p0.C3P0Registry.<clinit>(C3P0Registry.java:134)
    at com.mchange.v2.c3p0.impl.PoolBackedDataSourceBase.<init>(PoolBackedDataSourceBase.java:228)
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.<init>(AbstractPoolBackedDataSource.java:62)
    at com.mchange.v2.c3p0.ComboPooledDataSource.<init>(ComboPooledDataSource.java:109)
    at com.mchange.v2.c3p0.ComboPooledDataSource.<init>(ComboPooledDataSource.java:105)
    ...

Jan 16, 2013 12:57:16 PM com.mchange.v2.c3p0.C3P0Registry banner
INFO: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:05:05; debug? true; trace: 10]
Jan 16, 2013 12:57:16 PM com.mchange.v2.c3p0.PoolConfig <clinit>
WARNING: Read of system Properties blocked -- ignoring any c3p0 configuration via System properties! (But any configuration via a c3p0.properties file is still okay!)
java.security.AccessControlException: access denied ("java.util.PropertyPermission" "*" "read,write")
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPropertiesAccess(Unknown Source)
    at java.lang.System.getProperties(Unknown Source)
    at com.mchange.v2.c3p0.PoolConfig.<clinit>(PoolConfig.java:101)
    at com.mchange.v2.c3p0.impl.C3P0ImplUtils.defaultConnectionTester(C3P0ImplUtils.java:283)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.<init>(WrapperConnectionPoolDataSource.java:47)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.<init>(WrapperConnectionPoolDataSource.java:67)
    at com.mchange.v2.c3p0.ComboPooledDataSource.<init>(ComboPooledDataSource.java:114)
    at com.mchange.v2.c3p0.ComboPooledDataSource.<init>(ComboPooledDataSource.java:105)
    ...

started taking connection - Wed Jan 16 12:57:16 CET 2013
Jan 16, 2013 12:57:20 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ some connection info and params for DB1]
Connection taken - Wed Jan 16 12:57:23 CET 2013
started taking connection - Wed Jan 16 12:57:23 CET 2013
Jan 16, 2013 12:57:24 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ some connection info and params for DB2]
Connection taken - Wed Jan 16 12:57:24 CET 2013

What am i missing? Is there a way to speed up process of getting connection? I thought that it might be a server issue, but i'm having those lags even when i run my applet from eclipse (and make it contact remote database at server). Please help, or at least explain me why getting connection is so slow?

4

1 回答 1

0

很可能是 testConnectionOnCheckin=true 杀死了你。由于 c3p0 将这项工作卸载到后台线程 - 这些操作似乎已备份,并且连接不会立即可用于重用。

于 2013-06-12T00:01:42.717 回答