2013-01-16 59 views
0

我正在使用c3p0連接池進行我的(未簽名的)Java applet和MySql db之間的通信。 我有初始化池驗證碼:c3p0「getConnection」很慢

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

初始化池的方法是這樣的:

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"); 
    } 
} 

我C3P0-config.xml中很簡單:

<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> 

的問題是,當我第一次調用數據庫時,「getConnection」調用有時甚至需要10秒或更長時間才能響應(第二次我聯繫數據庫更快)。我一直在尋找網絡和「c3p0」手冊來加速連接池,但沒有運氣。

當我運行我的小應用程序(從服務器)Java控制檯輸出如下:

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 

我在想什麼?有沒有辦法加快獲取連接的過程?我認爲這可能是一個服務器問題,但即使當我從eclipse運行我的小程序(並使其與服務器上的遠程數據庫聯繫)時,我仍然存在這些滯後。請幫忙,或者至少解釋一下爲什麼連接速度很慢?

+0

使用其他工具(如mysql命令行客戶端)連接到遠程數據庫需要多長時間?您確定這不僅僅是網絡延遲,還是DNS解析時間過長?每次啓動小程序時是否會發生同樣的延遲,或者如果您退出小程序並再次啓動,速度會更快嗎? – jpkrohling

+0

好吧,當我從本地主機啓動並且連接到遠程數據庫時,速度會更快。奇怪的問題是Applet和數據庫都在服務器上,我從我的電腦上測試Applet。當我關閉小程序並重新啓動它時,運行速度會更快,但不會太多。命令行mysql很快。 – guest86

回答

0

這很可能是testConnectionOnCheckin = true,這會殺了你。由於c3p0將這項工作卸載到後臺線程 - 這些操作看起來是備份的,並且連接不能立即重新使用。