|
16 | 16 |
|
17 | 17 | package com.example.appengine.cloudsql;
|
18 | 18 |
|
| 19 | +import com.google.apphosting.api.ApiProxy; |
19 | 20 | import com.google.common.base.Stopwatch;
|
20 | 21 |
|
| 22 | + |
21 | 23 | import java.io.IOException;
|
22 | 24 | import java.io.PrintWriter;
|
23 | 25 | import java.net.Inet4Address;
|
|
30 | 32 | import java.sql.SQLException;
|
31 | 33 | import java.sql.Timestamp;
|
32 | 34 | import java.util.Date;
|
| 35 | +import java.util.Map; |
33 | 36 | import java.util.concurrent.TimeUnit;
|
34 | 37 |
|
35 | 38 | import javax.servlet.ServletException;
|
@@ -100,15 +103,17 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
|
100 | 103 | @Override
|
101 | 104 | public void init() throws ServletException {
|
102 | 105 | try {
|
103 |
| - String url = System.getProperty("cloudsql"); |
| 106 | + ApiProxy.Environment env = ApiProxy.getCurrentEnvironment(); |
| 107 | + Map<String,Object> attr = env.getAttributes(); |
| 108 | + String hostname = (String) attr.get("com.google.appengine.runtime.default_version_hostname"); |
| 109 | + |
| 110 | + String url = hostname.contains("localhost:") |
| 111 | + ? System.getProperty("cloudsql-local") : System.getProperty("cloudsql"); |
104 | 112 | log("connecting to: " + url);
|
105 | 113 | try {
|
106 |
| - Class.forName("com.mysql.jdbc.Driver"); |
107 | 114 | conn = DriverManager.getConnection(url);
|
108 |
| - } catch (ClassNotFoundException e) { |
109 |
| - throw new ServletException("Error loading JDBC Driver", e); |
110 | 115 | } catch (SQLException e) {
|
111 |
| - throw new ServletException("Unable to connect to PostGre", e); |
| 116 | + throw new ServletException("Unable to connect to Cloud SQL", e); |
112 | 117 | }
|
113 | 118 |
|
114 | 119 | } finally {
|
|
0 commit comments