与 Google 云 SQL 的连接间歇性断开

Connection made to Google Cloud SQL drops intermittently

我正在使用 Google Cloud SQL 使用 Java-SQL 连接器。我面临的问题是与数据库的连接意外断开。在谷歌搜索时,我遇到了这个 question 并尝试了同一问题中建议的解决方案。

In your console click the project, on the left side click Storage > CloudSQL then click on your database name. You will see an 'Edit' button on top. Click that and scroll down to Activation Policy, change it to Always On and then click save.

但我仍然面临同样的问题。幸运的是,我一直在 Google App Engine 上保存日志,并且附上了连接数据库时发生的异常的快照。

我在下面发布的代码要点用于建立与数据库的连接。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.StringTokenizer;
import java.util.logging.Logger;

import com.google.appengine.api.utils.SystemProperty;
import static com.google.appengine.api.utils.SystemProperty.environment;
import static com.google.appengine.api.utils.SystemProperty.Environment.Value.Development;
import static com.google.appengine.api.utils.SystemProperty.Environment.Value.Production;

 Connection con=null;
SystemProperty.Environment.Value env = environment.value();
    if(env == Production)
    {
        System.out.println("Inside Production Phase");
        // Load the class that provides the new "jdbc:google:mysql://" prefix.
        Class.forName("com.mysql.jdbc.GoogleDriver");
        url = "jdbc:google:mysql://<my-project-id>:<cloud-sql-instance>/<database-name>?user=<user-name>&password=<database-password>&useUnicode=true&characterEncoding=UTF-8";
    }//if
    else if(env == Development)
    {
        System.out.println("Inside Development Phase");
        // This will load the MySQL driver, each DB has its own driver
        Class.forName("com.mysql.jdbc.Driver");
        url = "jdbc:mysql://127.0.0.1:3306/<database-name>?user=root";
    }//else if
    con = DriverManager.getConnection(url);

有没有人遇到同样的问题,求助。

获得临时修复,在连接到 Google Cloud SQL

时使用了以下参数
url = "jdbc:google:mysql://my-app:mysql2/project-name?user=root&password=password&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";

参考 URL: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html