Google Cloud SQL的连接间歇性丢失 [英] Connection made to Google Cloud SQL drops intermittently

查看:114
本文介绍了Google Cloud SQL的连接间歇性丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用使用Java-SQL连接器的Google Cloud SQL。我面临的问题是与数据库的连接意外地下降。当谷歌搜索时,我遇到了这个



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

  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;

导入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;

连接con = null;
SystemProperty.Environment.Value env = environment.value();
if(env == Production)
{
System.out.println(Inside Production Phase);
//载入提供新的jdbc:google:mysql://前缀的类。
Class.forName(com.mysql.jdbc.GoogleDriver);
url =jdbc:google:mysql://< my-project-id>:< cloud-sql-instance> /< database-name>?user =< user-name>&密码= LT;数据库密码>&安培;了useUnicode =真安培;的characterEncoding = UTF-8\" ;
} // if
else if(env == Development)
{
System.out.println(Inside Development Phase);
//这将加载MySQL驱动程序,每个DB都有自己的驱动
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; 

引用网址: https://dev.mysql.com/doc/connector-j/5.1/ en / connector-j-reference-configuration-properties.html


I am using a Google Cloud SQL using Java-SQL connector. 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. 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.

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

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

这篇关于Google Cloud SQL的连接间歇性丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆