从GAE连接到MySQL实例时发生java通信异常
我正在尝试从GAE(谷歌应用引擎)内部连接到我的MySQL DB实例,也在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: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at 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) at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:668) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1078) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2417) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2450) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2235) at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:818) at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:46) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at 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 com.mysql.jdbc.GoogleNonRegisteringDriver$JdbcWrapper.getInstance(GoogleNonRegisteringDriver.java:282) at com.mysql.jdbc.GoogleNonRegisteringDriver.connect(GoogleNonRegisteringDriver.java:252) at 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 javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:326) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:437) at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:444) at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:256) at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:308) at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:300) at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:441) at java.lang.Thread.run(Thread.java:745) Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost. at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3039) at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:592)
这里是appengine网站。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答案相关的屏幕截图:
# 1 楼答案
您正在使用第二代云sql实例。这些还无法从app engine访问。使用第一代实例,您可以将应用添加为授权应用。我也有同样的问题
# 2 楼答案
Communications link failure
通常表示您没有连接权限您是否为GAE应用程序授予了对CloudSQL实例的访问权限
https://cloud.google.com/sql/docs/access-control#appengine