Dear Stackoverflow community,
what I'd like to do: working with Eclipse, use GoogleAppEngine in combination with Google CloudSQL to connect to a database and print out results
which tutorial am I following: It's the "using Cloud SQL for MySQL" tutorial right here: https://cloud.google.com/appengine/docs/standard/java/cloud-sql/using-cloud-sql-mysql
what have I achieved so far: I can make it work and print out values from my database when I hardcode user, pass and database into appengine-web.xml:
<system-properties>
<property name="cloudsql" value="jdbc:google:mysql://XXXYYYZZZ:europe-west3:XXXYYYZZZ/Database?user=root&password=12345" />
</system-properties>
and then, in HelloAppEngine.java, get this url with System.getProperty("cloudsql"):
final String selectSql = "SELECT favID from favs";
PrintWriter out = resp.getWriter();
resp.setContentType("text/plain");
String url = System.getProperty("cloudsql");
try (
Connection conn = DriverManager.getConnection(url);
ResultSet rs = conn.prepareStatement(selectSql).executeQuery()) {
out.print("Last 10 visitsss:\n");
while (rs.next()) {
out.println(rs.getString("favID"));
}
} catch (SQLException e) {
out.println("fehlerddddd: " + e);
out.println(System.getProperty("cloudsql"));
}
What I dont understand though: According to the tutorial, I dont have to hardcode user, pass and database values in the appengine-web.xml, but rather have them in a pom.xml file and then "fetch" those values from there by using ${INSTANCE_CONNECTION_NAME}, /${database}, user=${user} and password=${password}.
My pom.xml file contains these information:
<properties>
<appengine.maven.plugin.version>1.3.2</appengine.maven.plugin.version>
<appengine.api.sdk.version>1.9.62</appengine.api.sdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
<INSTANCE_CONNECTION_NAME>XXXYYYZZZ:europe-west3:faverdb</INSTANCE_CONNECTION_NAME>
<user>root</user>
<password>12345</password>
<database>XXYYZZ</database>
and also:
<dependency> <!-- Only used locally -->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version> <!-- v5.x.x is for production, v6.x.x EAP X DevAPI -->
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<!-- If using MySQL 6.x driver, use mysql-socket-factory-connector-j-6 instead -->
<artifactId>mysql-socket-factory</artifactId>
<version>1.0.5</version>
</dependency>
So, the final question: What am I missing that using ${placeholder} in my appengine-web.xml does not work while hardcoding the necessary information in there right away does work?
I'd appreciate any help :)
Ok, I found the solution: When you add a new Google App Engine standard app, the pom file contains the following part:
Eclipse shows a warning: "This app engine mavin plugin has been deprecated" which made me suspicious.
Looking at the complete pom.xml file linked by google: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/appengine-java8/cloudsql/pom.xml
I saw that their plugin section looks different:
I did not look at this part since its not explicitely mentioned on the tutorials main page: https://cloud.google.com/appengine/docs/standard/java/cloud-sql/using-cloud-sql-mysql
Replacing my plugin section with this plugin section did the trick. I guess the part where
is placed is responsible. So, problem solved :)