Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the Appengine MySQL driver #751

Merged
merged 3 commits into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions appengine-java8/cloudsql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@
<password>myPassword</password>
<database>sqldemo</database>

<!-- [START_EXCLUDE] -->
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<!-- [END_EXCLUDE] -->
</properties>
<!-- [END properties] -->

<dependencies>
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
<dependency>
Expand All @@ -68,15 +67,16 @@
</dependency>

<!-- [START dependencies] -->
<dependency>
<!-- Driver injected at runtime by the use of <use-google-connector-j> in appengine-web.xml -->
<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 -->
<!--<version>6.0.6</version>-->
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId> <!-- mysql-socket-factory-connector-j-6 -->
<artifactId>mysql-socket-factory</artifactId>
<version>1.0.3</version>
</dependency>
<!-- [END dependencies] -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package com.example.appengine.cloudsql;

import com.google.apphosting.api.ApiProxy;
import com.google.common.base.Stopwatch;


import java.io.IOException;
import java.io.PrintWriter;
import java.net.Inet4Address;
Expand All @@ -30,6 +32,7 @@
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletException;
Expand Down Expand Up @@ -100,15 +103,17 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
@Override
public void init() throws ServletException {
try {
String url = System.getProperty("cloudsql");
ApiProxy.Environment env = ApiProxy.getCurrentEnvironment();
Map<String,Object> attr = env.getAttributes();
String hostname = (String) attr.get("com.google.appengine.runtime.default_version_hostname");

String url = hostname.contains("localhost:")
? System.getProperty("cloudsql-local") : System.getProperty("cloudsql");
log("connecting to: " + url);
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
throw new ServletException("Error loading JDBC Driver", e);
} catch (SQLException e) {
throw new ServletException("Unable to connect to PostGre", e);
throw new ServletException("Unable to connect to Cloud SQL", e);
}

} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<threadsafe>true</threadsafe>
<runtime>java8</runtime>
<use-google-connector-j>true</use-google-connector-j>

<service>cloudsql</service>

<system-properties>
<property name="cloudsql" value="jdbc:mysql://google/${database}?useSSL=false&amp;cloudSqlInstance=${INSTANCE_CONNECTION_NAME}&amp;socketFactory=com.google.cloud.sql.mysql.SocketFactory&amp;user=${user}&amp;password=${password}" />
<property name="cloudsql" value="jdbc:google:mysql://${INSTANCE_CONNECTION_NAME}/${database}?user=${user}&amp;password=${password}" />
<property name="cloudsql-local" value="jdbc:mysql://google/${database}?useSSL=false&amp;cloudSqlInstance=${INSTANCE_CONNECTION_NAME}&amp;socketFactory=com.google.cloud.sql.mysql.SocketFactory&amp;user=${user}&amp;password=${password}" />
</system-properties>
</appengine-web-app>
<!-- [END config] -->