从本地java Webapp连接到Cloud SQL PostgreSQL

时间:2018-05-02 04:34:16

标签: postgresql google-app-engine google-cloud-sql

我正在尝试从我的本地Web应用程序连接到google cloud sql postgreSQL,但我在执行后遇到异常:

DriverManager.getConnection(jdbcUrl, userName, password);

例外:

2018-05-02 09:51:13.698:WARN:oejs.ServletHandler:qtp1720435669-17: Error for /LoginListener
java.lang.UnsatisfiedLinkError: The operation completed successfully.<|
    at jnr.ffi.provider.jffi.AsmRuntime.newUnsatisifiedLinkError(AsmRuntime.java:40)
    at jnr.unixsocket.Native$LibC$jnr$ffi$0.socket(Unknown Source)
    at jnr.unixsocket.Native.socket(Native.java:92)
    at jnr.unixsocket.UnixSocketChannel.<init>(UnixSocketChannel.java:101)
    at jnr.unixsocket.UnixSocketChannel.open(UnixSocketChannel.java:65)
    at com.google.cloud.sql.postgres.SocketFactory.createSocket(SocketFactory.java:66)
    at org.postgresql.core.PGStream.<init>(PGStream.java:61)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:156)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
    at org.postgresql.Driver.makeConnection(Driver.java:452)
    at org.postgresql.Driver.connect(Driver.java:254)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at db.Connector.getConnection(Connector.java:31)
    at dbMethods.SubscriberDBMethods.getSubscriber(SubscriberDBMethods.java:16)
    at com.Admin101.LoginListener.doGet(LoginListener.java:49)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:848)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1772)
    at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:134)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:48)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at com.google.appengine.tools.development.jetty9.StaticFileFilter.doFilter(StaticFileFilter.java:123)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at com.google.appengine.tools.development.DevAppServerRequestLogFilter.doFilter(DevAppServerRequestLogFilter.java:44)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1751)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:582)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:524)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1180)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:512)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112)
    at com.google.appengine.tools.development.jetty9.DevAppEngineWebAppContext.doScope(DevAppEngineWebAppContext.java:94)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)
    at com.google.appengine.tools.development.jetty9.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:597)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)
    at org.eclipse.jetty.server.Server.handle(Server.java:534)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:320)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:283)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:108)
    at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589)
    at java.lang.Thread.run(Thread.java:748)

我在postgresql的pom中添加了依赖项。

  <dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>42.2.2</version>
</dependency>

<dependency>
  <groupId>com.google.cloud.sql</groupId>
  <artifactId>postgres-socket-factory</artifactId>
  <version>1.0.8</version>
</dependency>

JDBC连接:

public static Connection getConnection()
{   
    Connection conn = null;
    try
    {     ApiProxy.Environment env = ApiProxy.getCurrentEnvironment();
          Map<String,Object> attr = env.getAttributes();

          String jdbcUrl = String.format(
                  "jdbc:postgresql://google/%s?socketFactory=com.google.cloud.sql.postgres.SocketFactory"
                            + "&socketFactoryArg=%s",
                    DBNAME,
                    INSTANCE_NAME);

                conn = DriverManager.getConnection(jdbcUrl, userName, password);

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return conn;
}

如果部署到谷歌应用引擎,相同的连接工作正常,但在appengine本地运行时会抛出不满意的链接错误。

1 个答案:

答案 0 :(得分:0)

在JDBC连接代码中,只为已部署的应用程序定义了一个连接。确保您已启用PostreSQL Connector / J并为本地PostgreSQL连接添加一些代码。有两个相似的相关SO案例:this onethis one

检查`appengine-web.xml是否为:

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  ...
  <use-google-connector-j>true</use-google-connector-j>
</appengine-web-app>

并以此MySQL代码段为例(将其改编为PostgreSQL和您的URL):

 if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
        // Load the class that provides the new "jdbc:google:mysql://" prefix.
     Class.forName("com.mysql.jdbc.GoogleDriver");
     url = "jdbc:google:mysql://your-project-id:your-instance-name/your-database?user=root";
 } else {
     // Local MySQL instance to use during development.
     Class.forName("com.mysql.jdbc.Driver");
     url = "jdbc:mysql://127.0.0.1:3306/your-database?user=root";

     // Alternatively, connect to a Google Cloud SQL instance using:
     // jdbc:mysql://ip-address-of-google-cloud-sql-instance:3306/your-database?user=root
 }

查找相关的官方文档here