0

OK So I am trying to make a Java server that posts to a cloudSQL instance using cloudSQL Proxy and I am stuck.

I started both the proxy and the server successfully on my local machine:

./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306 \
                  -credential_file=<PATH_TO_KEY_FILE> &

This is the server:

public class PostMainServer {
    public static void main(String[] args) throws Exception {

            Server server = new Server(7070);
            ServletContextHandler handler = new ServletContextHandler(server, "/post");
            handler.addServlet(Servlet.class, "/");
            server.start();
        }
}

This is a Jetty servlet that the server uses:

@SuppressWarnings({ "deprecation", "resource" })
    protected void doPost(HttpServletRequest request,     HttpServletResponse response) throws ServletException, IOException {

    // ...... logic goes here...... //

    // Now it's time to connect to the CloudSQL instance.
        String jdbcUrl = String.format(
                "jdbc:mysql://google/%s?cloudSqlInstance=%s&"
                   +     "socketFactory=com.google.cloud.sql.mysql.SocketFactory",
                databaseName,
                instanceConnectionName);

                 Connection conn = DriverManager.
getConnection(jdbcUrl, username,     password); //<----- The Exception is thrown here....

    // the rest below.....
}

This is the error I get from the console when I use Postman to make a POST request:

Apr 09, 2017 6:30:21 PM com.google.cloud.sql.mysql.SocketFactory connect
INFO: Connecting to Cloud SQL instance [INSTANCE-NAME].
Apr 09, 2017 6:30:21 PM com.google.cloud.sql.mysql.SslSocketFactory getInstance
INFO: First Cloud SQL connection, generating RSA key pair.
java.sql.SQLNonTransientConnectionException: Could not create connection to database server.

When walking through the debugger the SQLException is thrown here in the getConnection() method of the DriverManager class:

        SQLException reason = null;

        for(DriverInfo aDriver : registeredDrivers) {
            // If the caller does not have permission to load the driver then
            // skip it.
            if(isDriverAllowed(aDriver.driver, callerCL)) {
                try {
                    println("    trying " + aDriver.driver.getClass().getName());
                    Connection con = aDriver.driver.connect(url, info);
                    if (con != null) {
                        // Success!
                        println("getConnection returning " + aDriver.driver.getClass().getName());
                        return (con);
                    }
                } catch (SQLException ex) {
                    if (reason == null) {
                        reason = ex; // <--------- THIS LINE IS REACHED
                    }
                }

            } // The rest of the method below.....

Lastly the proxy does not register any connections it is stuck here:

2017/04/09 18:29:21 Listening on [INSTANCE-CONNECTION-NAME]
2017/04/09 18:29:21 Ready for new connections...

Assuming that the username and password credentials are right, what could possibly be the cause of this refusal to connect to the proxy? I am stuck for 2 days now.

  • The Java socket library does not need/use the client proxy so the proxy logs are irrelevant. Please include the full exception from "java.sql.SQLNonTransientConnectionException: Could not create connection to database server." You omitted the root cause exception from your question so it's hard to say what the problem is. – Vadim Apr 10 '17 at 05:00

1 Answers1

1

Ok so it turns out I had the cloud SDK installed however I didn't not authorize it on my local server by running 'gcloud auth login' on the command line first.