Skip to content

Commit

Permalink
Use the Appengine MySQL driver (#751)
Browse files Browse the repository at this point in the history
* Use the Appengine MySQL driver

* Fix style
  • Loading branch information
lesv authored Jul 11, 2017
1 parent 3d8cd56 commit 0756f07
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
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] -->

0 comments on commit 0756f07

Please sign in to comment.