java - Google SQL 无法在 Google App Engine 上运行

标签 java mysql google-app-engine jdbc google-cloud-sql

我希望将应用程序部署到 Google App Engine 并允许用户使用 POST 从网络访问它。据我所知,com.mysql.jdbc.GoogleDriver 仅在应用程序部署在 GAE 期间有效。所以,每次部署到 GAE 后,我都会尝试它。不幸的是,我总是收到回复的错误部分。我认为这是我与 Google Cloud SQL 连接的问题。

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">

<!--
Allows App Engine to send multiple requests to one instance in parallel:
-->
<threadsafe>true</threadsafe>
<use-google-connector-j>true</use-google-connector-j>
<system-properties>
<property name="ae-cloudsql.cloudsql-database-url" value="jdbc:google:mysql://${INSTANCE_CONNECTION_NAME}/${database}?user=${user}&amp;password=${password}" />
</system-properties>

<!--
HTTP Sessions are disabled by default. To enable HTTP sessions specify:

  <sessions-enabled>true</sessions-enabled>

It's possible to reduce request latency by configuring your application to
asynchronously write HTTP session data to the datastore:

  <async-session-persistence enabled="true" />

With this feature enabled, there is a very small chance your app will see
stale session data. For details, see
https://cloud.google.com/appengine/docs/java/config/appconfig#Java_appengine_web_xml_Enabling_sessions
-->

</appengine-web-app>

Web.XML

@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@POST
@Path("/v3")
public Response getSAppData(AppDataRequest adr) {
    Response data = ads.getSAppData(adr.getId(), adr.getEmail(), adr.getPassword());
    return data;
}

邮政编码

public class AppDataService {
Map<AppDataRequest, Data> DataHM = new HashMap<>();

static final String JDBC_DRIVER = "com.mysql.jdbc.GoogleDriver";
static final String DB_URL = "jdbc:google:mysql://****:****/****?user=root";

static final String USER = "****";
static final String PASS = "****";

public AppDataService(){
    Connection conn = null;
    Statement stat = null;
    try{
        Class.forName("com.mysql.jdbc.GoogleDriver");
        conn = DriverManager.getConnection(DB_URL);
        stat = conn.createStatement();
        String sql = "SELECT * FROM testdata";
        ResultSet resu = stat.executeQuery(sql);
        while(resu.next()){
            int id = resu.getInt("app_id");
            String email = resu.getString("email");
            String password = resu.getString("password");
            String token = resu.getString("token");
            DataHM.put(new AppDataRequest(id, email, password), new Data(token));
        }
        resu.close();
        stat.close();
        conn.close();
    }
    catch(SQLException se){
        se.printStackTrace();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    finally{
        try{
            if(stat!=null){
                stat.close();
            }
        }
        catch (SQLException se2){
            se2.printStackTrace();
        }
        try{
            if(conn!=null){
                conn.close();
            }
        }
        catch(SQLException se3){
            se3.printStackTrace();
        }
    }               
}

public Response getSAppData(int id, String email, String password){
    Map<String, AppData> AppDataHM = new HashMap<>(); 
    Map<String, Data> DataHM1 = new HashMap<>();
    Map<String, List<String>> DataHM2 = new HashMap<>();
    HashMap<Object, Object> ADHMDHM = new HashMap<>();

    List<String> message = new ArrayList<>();
    Data data = DataHM.get(new AppDataRequest (id, email, password));
    List<String> data2 = new ArrayList<>();

    if(data != null){
        message.add("");
        AppDataHM.put("AppData", new AppData("success", message));
        DataHM1.put("Data", data);
        ADHMDHM.putAll(AppDataHM);
        ADHMDHM.putAll(DataHM1);
        String ADHMDHM1 = new Gson().toJson(ADHMDHM);
        return Response.status(200).entity(ADHMDHM1).build();
    }
    else{
        message.add("Your login information is invalid. Please try with the correct information");
        AppDataHM.put("AppData", new AppData("error", message));
        DataHM2.put("Data", data2);
        ADHMDHM.putAll(AppDataHM);
        ADHMDHM.putAll(DataHM2);
        String ADHMDHM2 = new Gson().toJson(ADHMDHM);
        return Response.status(200).entity(ADHMDHM2).build();
        }   
    }
}

访问Google SQL的代码

Jar Files

Jar 文件 Return from the GAE

使用 POSTman 返回 GAE

预先感谢所有帮助回答此问题的用户。

最佳答案

鲁本。我觉得您的代码不起作用,因为您使用的是实例 ID 而不是实例连接名称作为您的 Google Cloud SQL URL。您的 Web.XML 表明您应该使用 ${INSTANCE_CONNECTION_NAME}来自 <property name="ae-cloudsql.cloudsql-database-url" value="jdbc:google:mysql://${INSTANCE_CONNECTION_NAME}/${database}?user=${user}&amp;password=${password}" />

你可以查看这个Setting up the Cloud SQL instance .

关于java - Google SQL 无法在 Google App Engine 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43627319/

相关文章:

mysql - 未在 CREATE TABLE 语句中创建的索引和约束

php - $_GET 条件变量

PHP、MYSQL、Echo 在单列中循环行

google-app-engine - 多开发环境如何使用GAE的dispatch.yaml?

python - 索引错误: list index out of range (in query results)

java - 如何使用zookeeper作为基于spring的Web应用程序的数据源来加载属性?

java - 如何创建新的 Java 异常?

java - ReflectionTestUtils.setField (Mockito),无法识别字段。

java - 如何逐步从大型系统中的遗留内容中恢复?

google-app-engine - GAE : Exceeded maximum allocated IDs