【问题标题】:Connect to Cloud SQL using Sql2o in Google Cloud Endpoint在 Google Cloud Endpoint 中使用 Sql2o 连接到 Cloud SQL
【发布时间】:2015-03-12 12:49:54
【问题描述】:

我正在尝试连接到谷歌云端点内的云 sql,并使用轻量级 jdbc 包装器 sql2o 作为数据访问方法。

@Api(name = "questionapi", version = "v1", description = "question api")
public class QuestionService {

    private static Sql2o sql2o = new Sql2o(
            "jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxxxx", "root",
            "xxxxxxx");

    @ApiMethod(name = "get", httpMethod = HttpMethod.GET)
    public List<Question> get() {

        String q = "select * from questions";

        try (Connection conn = sql2o.open()) {
            return conn.createQuery(q).executeAndFetch(Question.class);
        }
    }

应用程序运行后,我可以访问localhost:8888/_ah/api/explorer 试用该api。但是,有一个错误说:

org.sql2o.Sql2oException: Could not acquire a connection from DataSource - No suitable driver found for jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxxxx

我该如何解决这个问题?

编辑:

更改为 maven 项目后,我收到了这个新的错误消息:

503 Service Unavailable

- Show headers -

{
 "error": {
  "message": "java.lang.NoClassDefFoundError: Could not initialize class com.mysql.jdbc.ConnectionImpl",
  "code": 503,
  "errors": [
   {
    "domain": "global",
    "reason": "backendError",
    "message": "java.lang.NoClassDefFoundError: Could not initialize class com.mysql.jdbc.ConnectionImpl"
   }
  ]
 }
}

编辑

又是新的一天,我还是卡在这里。

我所做的是使用 maven 下载 endpoints-skeleton-archetype 项目,它是一个可供使用的新的空 Cloud Endpoints 后端 API 项目,包含所需的文件和目录。

我立即将其部署到应用引擎,并尝试返回一个有意义的值。成功了,将返回一个简单的“帮助世界”字符串。

接下来,我尝试使用 jdbc 连接到云 sql。为了做到这一点,我按照教程here&lt;use-google-connector-j&gt;true&lt;/use-google-connector-j&gt; 添加到 appengine-web.xml

我尝试不同的连接字符串组合

    Class.forName("com.mysql.jdbc.GoogleDriver");
        String url = "jdbc:google:mysql://xxxxxxxxxxxx:xxxxx?user=root";

        conn = DriverManager.getConnection(url);
        ResultSet rs = conn.createStatement().executeQuery("SELECT 1 + 1");

在所有这些之后,我仍然收到此错误消息。

    503 Service Unavailable

- Show headers -

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "backendError",
    "message": "java.lang.NullPointerException"
   }
  ],
  "code": 503,
  "message": "java.lang.NullPointerException"
 }
}

【问题讨论】:

  • 也有这个问题但无法解决...

标签: google-app-engine google-cloud-endpoints google-cloud-sql


【解决方案1】:

当在类路径中找不到 jdbc 驱动程序时会发生此错误。您如何管理依赖项?你用maven吗?如果您将 mysql jdbc 驱动程序添加到依赖项列表中,则该错误应该得到修复。

我对您的代码有另一条评论,与您的问题无关。但无论如何,它来了。 下面的代码行存在连接泄漏,因为您从不关闭连接。这最终将耗尽连接池,您的应用程序将挂起。 return sql2o.open().createQuery(q).executeAndFetch(Question.class);

这是使用sql2o时正确的做法: try (Connection con = sql2o.open()) { return con.createQuery(q).executeAndFetch(Question.class); }

【讨论】:

  • 我改成了maven项目,在maven依赖中添加了sql2o jar和connector j jar文件。现在我有一条新的错误消息。
  • 您是否在 GAE 上启用了 mysql 连接器? cloud.google.com/appengine/docs/java/cloud-sql/…
  • 是的,我确实添加了一行新的 XML。我正在尝试使用文档中的方法连接到数据库。之后我尝试使用 sql2o。任何想法?你是谷歌的吗?
  • google 说“MySQL 连接器/J 在 Google App Engine 中可用,但它不包含在应用程序中,除非应用程序明确要求它”。所以我还应该将 mysql-connector-java-5.1.6.jar 包含到 maven 依赖项中吗?
  • 我现在可以使用 jdbc 连接到云 sql。但是我仍然无法使用sql2o连接到云sql,我发现sql2o对javax.naming.*类有依赖,但是google app engine不包含也不支持这个类。谷歌列出了一个白名单,谷歌应用引擎支持哪些类。这是链接:cloud.google.com/appengine/docs/java/jrewhitelist