我正在使用c3p0连接池在我的(未签名)Java applet和MySql db之间进行通信。 我有这个代码用于初始化pool:
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”手册,以便在没有运气的情况下加速连接池。
当我运行我的applet(来自服务器)时,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运行我的applet(并使其与服务器上的远程数据库联系),我也有这些滞后。请帮忙,或者至少解释一下为什么连接速度太慢?
答案 0 :(得分:0)
它很可能是testConnectionOnCheckin = true,它会杀了你。由于c3p0将此工作卸载到后台线程 - 这些操作似乎备份,并且连接不能立即重用。