数据库连接池[Hikari] 初始化错误
Database connection pool[Hikari] initialize error
我正在尝试为我的网络应用程序配置 HikariCP。我没有使用任何框架,它是一个简单的 java 网络应用程序。当我尝试通过正常 JDBC 建立连接时,它成功了,我能够执行所有数据库操作。
但是,当我尝试与连接池建立连接时,我在初始化后遇到了一个我不知道的异常。
下面是我正在使用的配置,我正在上下文侦听器中对其进行初始化。我在 Java 1.7 上 运行,有一个 Tomcat 容器使用 HikariCP Java 6 依赖 2.3.2
import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.mkgcorp.travelweb.util.TLog;
import com.sun.jersey.core.util.Base64;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
public class CPInit implements ServletContextListener{
private final static String TAG = CPInit.class.getSimpleName();
private static HikariConfig mConfig;
private static HikariDataSource mDataSource;
public static String POOL_NAME = "TripPool";
public static int POOL_SIZE = 10;
public static int TIME_OUT = 30*1000;
public static int MIN_IDLE_SIZE = 2;
public static boolean isDebuggable;
private String PASSWORD = "YWRtaW4=";
public CPInit(){
String string = new String(Base64.decode(PASSWORD));
TLog.info(TAG, "Password = " + string);
mConfig = new HikariConfig();
mConfig.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
mConfig.addDataSourceProperty("url", "jdbc:mysql://127.0.0.1:3306/travlemate");
mConfig.addDataSourceProperty("cachePrepStmts", "true");
mConfig.addDataSourceProperty("prepStmtCacheSize", "250");
mConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
mConfig.addDataSourceProperty("useServerPrepStmts", "true");
mConfig.addDataSourceProperty("user", "root");
mConfig.addDataSourceProperty("password", string);
mConfig.setMaximumPoolSize(POOL_SIZE);
mConfig.setMinimumIdle(MIN_IDLE_SIZE);
mConfig.setPoolName(POOL_NAME);
mConfig.setConnectionTimeout(TIME_OUT);
mConfig.setAutoCommit(true);
mConfig.setInitializationFailFast(false);
mDataSource = new HikariDataSource(mConfig);
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
TLog.info(TAG, "CP has being destroyed");
mDataSource.close();
}
public static Connection getConnection() throws SQLException{
TLog.info(TAG, "get data connection");
return mDataSource.getConnection();
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
TLog.info(TAG, "CP has been initilizing");
String parm = arg0.getServletContext().getInitParameter("debugging");
isDebuggable = Boolean.valueOf(parm);
}
}
这是我得到的异常:
java.sql.SQLTimeoutException: Timeout after 30005ms of waiting for a connection.
at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:228)
at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:183)
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:91)
at com.mkgcorp.travelweb.controller.CPInit.getConnection(CPInit.java:68)
at com.mkgcorp.travelweb.data.Database.getConnection(Database.java:47)
at com.mkgcorp.travelweb.data.DataManager.getConnection(DataManager.java:100)
at com.mkgcorp.travelweb.data.DataManager.authenticateUser(DataManager.java:135)
at com.mkgcorp.travelweb.webservices.TripResource.isUserValid(TripResource.java:169)
at com.mkgcorp.travelweb.webservices.SyncResource.getContactStatus(SyncResource.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: JDBC4 Connection.isValid() method not supported, connection test query must be configured
at com.zaxxer.hikari.pool.BaseHikariPool.addConnection(BaseHikariPool.java:399)
at com.zaxxer.hikari.pool.HikariPoolAddConnection.run(HikariPool.java:78)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
... 1 more
实际上我得到了答案,Hikari 需要设置一个连接测试查询,如果那样做 JDBC4 isValid 将不会被调用。
所以只需添加 属性,我就能让它工作。
mConfig.setConnectionTestQuery("show tables");
我遇到了同样的问题,我刚刚弄明白了。
我在 Mac 上,不知何故我在我的 /Library/Java/Extensions
目录中安装了 postgresql-9.0-802.jdbc3.jar
。 DriverManager
会选择它作为连接到 jdbc:posgresql://
URL 的首选方式。它是 JDBC3 驱动程序,因此没有 isValid
方法。
即使我将 JDBC4 驱动程序放在我的类路径中,无论是 org.prosgreql.Driver
还是 com.impossibl.postgres.jdbc.PGDriver
,JDBC3 驱动程序都会取代它。
所以我删除了/Library/Java/Extensions/postgresql*
,一切都很好。
我正在尝试为我的网络应用程序配置 HikariCP。我没有使用任何框架,它是一个简单的 java 网络应用程序。当我尝试通过正常 JDBC 建立连接时,它成功了,我能够执行所有数据库操作。
但是,当我尝试与连接池建立连接时,我在初始化后遇到了一个我不知道的异常。
下面是我正在使用的配置,我正在上下文侦听器中对其进行初始化。我在 Java 1.7 上 运行,有一个 Tomcat 容器使用 HikariCP Java 6 依赖 2.3.2
import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.mkgcorp.travelweb.util.TLog;
import com.sun.jersey.core.util.Base64;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
public class CPInit implements ServletContextListener{
private final static String TAG = CPInit.class.getSimpleName();
private static HikariConfig mConfig;
private static HikariDataSource mDataSource;
public static String POOL_NAME = "TripPool";
public static int POOL_SIZE = 10;
public static int TIME_OUT = 30*1000;
public static int MIN_IDLE_SIZE = 2;
public static boolean isDebuggable;
private String PASSWORD = "YWRtaW4=";
public CPInit(){
String string = new String(Base64.decode(PASSWORD));
TLog.info(TAG, "Password = " + string);
mConfig = new HikariConfig();
mConfig.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
mConfig.addDataSourceProperty("url", "jdbc:mysql://127.0.0.1:3306/travlemate");
mConfig.addDataSourceProperty("cachePrepStmts", "true");
mConfig.addDataSourceProperty("prepStmtCacheSize", "250");
mConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
mConfig.addDataSourceProperty("useServerPrepStmts", "true");
mConfig.addDataSourceProperty("user", "root");
mConfig.addDataSourceProperty("password", string);
mConfig.setMaximumPoolSize(POOL_SIZE);
mConfig.setMinimumIdle(MIN_IDLE_SIZE);
mConfig.setPoolName(POOL_NAME);
mConfig.setConnectionTimeout(TIME_OUT);
mConfig.setAutoCommit(true);
mConfig.setInitializationFailFast(false);
mDataSource = new HikariDataSource(mConfig);
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
TLog.info(TAG, "CP has being destroyed");
mDataSource.close();
}
public static Connection getConnection() throws SQLException{
TLog.info(TAG, "get data connection");
return mDataSource.getConnection();
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
TLog.info(TAG, "CP has been initilizing");
String parm = arg0.getServletContext().getInitParameter("debugging");
isDebuggable = Boolean.valueOf(parm);
}
}
这是我得到的异常:
java.sql.SQLTimeoutException: Timeout after 30005ms of waiting for a connection.
at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:228)
at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:183)
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:91)
at com.mkgcorp.travelweb.controller.CPInit.getConnection(CPInit.java:68)
at com.mkgcorp.travelweb.data.Database.getConnection(Database.java:47)
at com.mkgcorp.travelweb.data.DataManager.getConnection(DataManager.java:100)
at com.mkgcorp.travelweb.data.DataManager.authenticateUser(DataManager.java:135)
at com.mkgcorp.travelweb.webservices.TripResource.isUserValid(TripResource.java:169)
at com.mkgcorp.travelweb.webservices.SyncResource.getContactStatus(SyncResource.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: JDBC4 Connection.isValid() method not supported, connection test query must be configured
at com.zaxxer.hikari.pool.BaseHikariPool.addConnection(BaseHikariPool.java:399)
at com.zaxxer.hikari.pool.HikariPoolAddConnection.run(HikariPool.java:78)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
... 1 more
实际上我得到了答案,Hikari 需要设置一个连接测试查询,如果那样做 JDBC4 isValid 将不会被调用。
所以只需添加 属性,我就能让它工作。
mConfig.setConnectionTestQuery("show tables");
我遇到了同样的问题,我刚刚弄明白了。
我在 Mac 上,不知何故我在我的 /Library/Java/Extensions
目录中安装了 postgresql-9.0-802.jdbc3.jar
。 DriverManager
会选择它作为连接到 jdbc:posgresql://
URL 的首选方式。它是 JDBC3 驱动程序,因此没有 isValid
方法。
即使我将 JDBC4 驱动程序放在我的类路径中,无论是 org.prosgreql.Driver
还是 com.impossibl.postgres.jdbc.PGDriver
,JDBC3 驱动程序都会取代它。
所以我删除了/Library/Java/Extensions/postgresql*
,一切都很好。