从GAE连接到MySQL实例时发生CommunicationsException

时间:2016-01-09 14:41:38

标签: java mysql google-app-engine

我尝试从GAE(Google App Engine)内部连接到我的MySQL数据库实例,也在GAE上。它从外面工作得很好。

经过一周的搜索,我似乎无法找到问题。

没有MySQL连接代码,servlet在GAE上没有错误就可以正常工作。

这是简单的servlet:

public class DBServlet extends HttpServlet {
        @Override
        public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            try {
                Class.forName("com.mysql.jdbc.GoogleDriver");
                Connection conn = DriverManager.getConnection("jdbc:google:mysql://cloud-2280:mysql/account", "root", "");
                //Connection conn = DriverManager.getConnection("jdbc:google:mysql://cloud-1180:mysql/account?user=root");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(DBServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(DBServlet.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                        conn.close();
                    } catch (SQLException ex) {
                        Logger.getLogger(DBServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

当在GAE上绑定时,我在GAE日志中得到了这个:

  

com.conversedynamics.NewServlet doPost:null   com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:   通信链路故障最后一个数据包成功发送到   服务器是0毫秒前。驱动程序没有收到任何数据包   从服务器。在   sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)   在   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)   在   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)   在java.lang.reflect.Constructor.newInstance(Constructor.java:46)at   com.mysql.jdbc.Util.handleNewInstance(Util.java:411)at   com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)   在com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:668)at   com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1078)at at   com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2417)at at   com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2450)   在com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2235)   在com.mysql.jdbc.ConnectionImpl。(ConnectionImpl.java:818)at   com.mysql.jdbc.JDBC4Connection。(JDBC4Connection.java:46)at   sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)   在   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)   在   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)   在java.lang.reflect.Constructor.newInstance(Constructor.java:46)at   com.mysql.jdbc.Util.handleNewInstance(Util.java:411)at   com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)at at   com.mysql.jdbc.GoogleNonRegisteringDriver $ JdbcWrapper.getInstance(GoogleNonRegisteringDriver.java:282)   在   com.mysql.jdbc.GoogleNonRegisteringDriver.connect(GoogleNonRegisteringDriver.java:252)   在java.sql.DriverManager.getConnection(DriverManager.java:571)at   java.sql.DriverManager.getConnection(DriverManager.java:233)at   com.conversedynamics.NewServlet.doPost(NewServlet.java:90)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:637)at at   javax.servlet.http.HttpServlet.service(HttpServlet.java:717)at at   org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)   在   org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1166)   在   org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)   在   org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)   在   org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)   在   org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)   在   org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)   在   org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)   在   org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)   在   org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)   在   org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)   在   org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)   在org.mortbay.jetty.Server.handle(Server.java:326)处   org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)   在   org.mortbay.jetty.HttpConnection $ RequestHandler.headerComplete(HttpConnection.java:923)   在org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)at   com.google.tracing.TraceContext $ TraceContextRunnable.runInContext(TraceContext.java:437)   在   com.google.tracing.TraceContext $ TraceContextRunnable $ 1.run(TraceContext.java:444)   在   com.google.tracing.CurrentContext.runInContext(CurrentContext.java:256)   在   com.google.tracing.TraceContext $ AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:308)   在   com.google.tracing.TraceContext $ AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:300)   在   com.google.tracing.TraceContext $ TraceContextRunnable.run(TraceContext.java:441)   在java.lang.Thread.run(Thread.java:745)引起:   java.io.EOFException:无法读取服务器的响应。期待   读取4个字节,在连接意外丢失之前读取0个字节。在   com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3039)at at   com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:592)

这是appengine-web.xml

<application>cloud-2280</application>
    <version>1</version>
    <!--    <ssl-enabled>false</ssl-enabled>
    <sessions-enabled>true</sessions-enabled>
    -->
    <threadsafe>false</threadsafe>
    <use-google-connector-j>true</use-google-connector-j>
</appengine-web-app>

更新:与koma的答案相关的屏幕截图:

enter image description here

2 个答案:

答案 0 :(得分:2)

您正在使用第二代云sql实例。尚未从应用引擎访问这些内容。使用第一代实例,您可以将您的应用添加为授权应用。我遇到了同样的问题。

答案 1 :(得分:1)

Communications link failure通常表示您无权连接。

您是否为GAE应用程序授予了对CloudSQL实例的访问权限?

https://cloud.google.com/sql/docs/access-control#appengine