我正在尝试使用Java和HikariConfig API(如Google教程所示)连接到托管在Google Cloud Platform上的MySQL数据库。我正在Eclipse中构建代码,但是尽管做了我能想到的一切,但我的连接无法正常工作。我能够使用Mysql工作台(使用公共api)和本地主机(代替Google App Engine)连接到同一数据库。我想我的项目中缺少一个jar文件。我正在添加jar库的代码和屏幕截图。
我得到的错误如下:
java.lang.AbstractMethodError: Method com/google/cloud/sql/mysql/SocketFactory.connect(Ljava/lang/String;ILcom/mysql/cj/conf/PropertySet;I)Ljava/io/Closeable; is abstract
at com.google.cloud.sql.mysql.SocketFactory.connect (SocketFactory.java)
at com.mysql.cj.protocol.a.NativeSocketConnection.connect (NativeSocketConnection.java:65)
at com.mysql.cj.NativeSession.connect (NativeSession.java:152)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly (ConnectionImpl.java:955)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO (ConnectionImpl.java:825)
at com.mysql.cj.jdbc.ConnectionImpl.<init> (ConnectionImpl.java:455)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance (ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect (NonRegisteringDriver.java:199)
at com.zaxxer.hikari.util.DriverDataSource.getConnection (DriverDataSource.java:117)
at com.zaxxer.hikari.util.DriverDataSource.getConnection (DriverDataSource.java:123)
at com.zaxxer.hikari.pool.PoolBase.newConnection (PoolBase.java:365)
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry (PoolBase.java:194)
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry (HikariPool.java:460)
at com.zaxxer.hikari.pool.HikariPool.checkFailFast (HikariPool.java:534)
at com.zaxxer.hikari.pool.HikariPool.<init> (HikariPool.java:115)
at com.zaxxer.hikari.HikariDataSource.<init> (HikariDataSource.java:81)
at com.x.d3sols.web.ConnectionPoolContextListener.createConnectionPool (ConnectionPoolContextListener.java:51)
at com.x.d3sols.web.ConnectionPoolContextListener.contextInitialized (ConnectionPoolContextListener.java:71)
at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized (ContextHandler.java:843)
at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized (ServletContextHandler.java:533)
at org.eclipse.jetty.server.handler.ContextHandler.startContext (ContextHandler.java:816)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext (ServletContextHandler.java:345)
at org.eclipse.jetty.webapp.WebAppContext.startWebapp (WebAppContext.java:1406)
at com.google.apphosting.runtime.jetty9.AppEngineWebAppContext.startWebapp (AppEngineWebAppContext.java:175)
at org.eclipse.jetty.webapp.WebAppContext.startContext (WebAppContext.java:1368)
at org.eclipse.jetty.server.handler.ContextHandler.doStart (ContextHandler.java:778)
at org.eclipse.jetty.servlet.ServletContextHandler.doStart (ServletContextHandler.java:262)
at org.eclipse.jetty.webapp.WebAppContext.doStart (WebAppContext.java:522)
at com.google.apphosting.runtime.jetty9.AppEngineWebAppContext.doStart (AppEngineWebAppContext.java:120)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:68)
at com.google.apphosting.runtime.jetty9.AppVersionHandlerMap.createHandler (AppVersionHandlerMap.java:240)
at com.google.apphosting.runtime.jetty9.AppVersionHandlerMap.getHandler (AppVersionHandlerMap.java:178)
at com.google.apphosting.runtime.jetty9.JettyServletEngineAdapter.serviceRequest (JettyServletEngineAdapter.java:120)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.dispatchServletRequest (JavaRuntime.java:722)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.dispatchRequest (JavaRuntime.java:685)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run (JavaRuntime.java:655)
at com.google.apphosting.runtime.JavaRuntime$NullSandboxRequestRunnable.run (JavaRuntime.java:847)
at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run (ThreadGroupPool.java:270)
at java.lang.Thread.run (Thread.java:748)
这对我来说很关键,任何帮助将不胜感激。谢谢:)
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.sql.DataSource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
/**
* Servlet implementation class ConnectionPoolContextListener
*/
@WebListener("Creates a connection pool that is stored in the Servlet's context for later use.")
public class ConnectionPoolContextListener implements ServletContextListener {
// Saving credentials in environment variables is convenient, but not secure - consider a more
// secure solution such as https://cloud.google.com/kms/ to help keep secrets safe.
private static final String DB_USER = "<user>";
private static final String DB_PASS = "<password>";
private static final String CLOUD_SQL_CONNECTION_NAME = "<Connection_Name>";
private static final String DB_NAME = "<DB_Name>";
private DataSource createConnectionPool() {
// [START cloud_sql_mysql_servlet_create]
// The configuration object specifies behaviors for the connection pool.
HikariConfig config = new HikariConfig();
//For Localhost - IT WORKS
/*String url = "jdbc:mysql://<IP_Address>/<DB_NAME>";
config.setJdbcUrl(url);
config.setUsername(DB_USER);
config.setPassword(DB_PASS);
config.setDriverClassName("com.mysql.jdbc.Driver");
config.addDataSourceProperty("useSSL", "false");*/
//For APP ENGINE - IT DOESNT WORKS
String url = "jdbc:mysql://google/<DB_NAME>";
config.setJdbcUrl(url);
config.setUsername(DB_USER);
config.setPassword(DB_PASS);
config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory");
config.addDataSourceProperty("cloudSqlInstance", CLOUD_SQL_CONNECTION_NAME);
config.addDataSourceProperty("useSSL", "false");
config.setDriverClassName("com.mysql.jdbc.Driver");
config.setMaximumPoolSize(5);
config.setMinimumIdle(5);
config.setConnectionTimeout(60000);
config.setIdleTimeout(600000);
config.setMaxLifetime(1800000); // 30 minutes
DataSource pool = new HikariDataSource(config);
return pool;
}
@Override
public void contextDestroyed(ServletContextEvent event) {
// This function is called when the Servlet is destroyed.
HikariDataSource pool = (HikariDataSource) event.getServletContext().getAttribute("my-pool");
if (pool != null) {
pool.close();
}
}
@Override
public void contextInitialized(ServletContextEvent event) {
// This function is called when the application starts and will safely create a connection pool
// that can be used to connect to.
DataSource pool = (DataSource) event.getServletContext().getAttribute("my-pool");
if (pool == null) {
pool = createConnectionPool();
event.getServletContext().setAttribute("my-pool", pool);
}
}
}
Here is the screenshot of my folder where I've added missing libraries that I could think of
答案 0 :(得分:2)
我终于在评论的帮助下解决了这个问题。显然,我添加的appengine-web.xml文件中缺少字符串,并且该字符串已修复。我还替换了我的类路径中的一些jar文件。
要在appengine-web.xml中添加的字符串为:
/sitename/apartments
您的库如下所示:
请注意您使用的所有库的版本,我正在使用v8。为此,需要除Gson之外的所有库。
答案 1 :(得分:1)
我曾经遇到过同样的问题(我知道这是多么痛苦),并且我将不需要太多的Java经验,但这就是我可以告诉我的有关如何将Cloud-SQL与App Engine连接的信息
请注意以下两点:
如果您想将 cloud-sql 与 app-engine 连接,则必须通过socket
进行连接,否则将无法连接,并且您想从 localhost 连接到 cloud-sql ,应该通过tcp
。在最后一种情况下,您需要将公共IP添加到connections
部分的授权网络中。
据我所知,您可以做什么,但您可以尝试按照文档中的建议设置网址的格式:
HikariConfig config = new HikariConfig();
config.setJdbcUrl(String.format("jdbc:mysql:///%s", DB_NAME));
最后一件事,您需要确保CLOUD_SQL_CONNECTION_NAME
的格式如下:
CLOUD_SQL_CONNECTION_NAME = 'project-id:region:instance-id'
// You can get this information from the overview page in google cloud console - sql.
有关更多信息,您可以查看这两个链接Google docs和Java example
希望有帮助。