[英]Connection made to Google Cloud SQL drops intermittently
I am using a Google Cloud SQL using Java-SQL connector. 我正在使用使用Java-SQL连接器的Google Cloud SQL。 The issue I am facing is that the connection to database drops unexpectedly. 我面临的问题是与数据库的连接意外断开。 While Googling I came across this question and tried the solution suggested in the same question. 在谷歌搜索时,我遇到了这个问题,并尝试了同一问题中建议的解决方案。
In your console click the project, on the left side click Storage > CloudSQL then click on your database name . 在控制台中,单击项目,在左侧单击存储> CloudSQL,然后单击 数据库名称 。 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 . 单击该按钮,然后向下滚动到“ 激活策略” ,将其更改为“ 始终在线” ,然后单击“ 保存” 。
But I'm still facing the same issue. 但是我仍然面临着同样的问题。 Fortunately I have been keeping the logs on Google App Engine and I have attached the snapshot of the exception that occurred while connecting to database. 幸运的是,我一直将日志保存在Google App Engine上,并附上了连接数据库时发生的异常快照。
Gist of the code that I've posted below is used to establish connection to the database. 我在下面发布的代码要点用于建立与数据库的连接。
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);
Is anyone facing the same problem, Please help. 有谁遇到同样的问题,请帮忙。
Got a temporary fix, used following parameters while making connection to Google Cloud SQL 连接到Google Cloud SQL时获得了临时修复,使用了以下参数
url = "jdbc:google:mysql://my-app:mysql2/project-name?user=root&password=password&autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
Reference URL: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html 参考网址: https : //dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.