4

I am trying to connect to my database from the Google Flexible Environment to the Google Cloud SQL. The connection string and the driver class are shown below:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.GoogleDriver" />
    <property name="url" value="jdbc:google:mysql://mz-test:us-central1:mz-life-cloudsql-prod/mz_db" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>

However, I am currently getting

org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/classes/context/applicationContext-jooq.xml]: 
            Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; 
            nested PropertyAccessExceptions (1) are:|PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; 
            nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.GoogleDriver]

The database I am trying connecting to is a Second Generation Cloud SQL MySQL database.

Why am I getting this exception?

The App Engine had this <use-google-connector-j> property. I've not seen this property for the Flexible Environment - at least not on those pages what I've been reading so far. Is there anything I'd have to set in addtion in my app.yaml file?


Not sure if I have to do this in Flexible Environment but I am currently trying to set the use-google-connector-j property to true in my yaml file:

use-google-connector-j: true

but it appears this is not working at the moment: https://code.google.com/p/googleappengine/issues/detail?id=11444

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • But you added both tags to your question – Jens Jun 05 '16 at 16:38
  • @Jens That is because I am using **Google Flexible Environment** and that is actually a mix of **Google App Engine** and **Google Compute Engine** or something like that and all of them are (part of) **Google Cloud Platform**. – Stefan Falk Jun 05 '16 at 16:40

1 Answers1

13

com.mysql.jdbc.GoogleDriver is designed to work for App Engine Standard Environment applications.

For Java applications running on App Engine Flexible Environment applications use the mysql-socket-factory library.

For a Maven-based application, add a dependency on the library:

<dependency>
    <groupId>com.google.cloud.sql</groupId>
    <artifactId>mysql-socket-factory</artifactId>
    <version>1.0.1</version>
</dependency>

Switch to the standard/official com.mysql.jdbc.Driver. The connection string changes from

jdbc:google:mysql://instance_name/db_name

to

jdbc:mysql://google/db_name?cloudSqlInstance=<instance_connection_name>&socketFactory=com.google.cloud.sql.mysql.SocketFactory

The value for <instance_connection_name> can be found on the Cloud SQL instance overview page in Google Cloud Console.

Note: If you are specifying the connection string in an XML file you might have to escape special characters like & to &amp;.

Note: This method doesn't work with the development line of the mysql driver (6 and above). I had to use the production 5.1.39 version.

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
Vadim
  • 4,996
  • 1
  • 26
  • 30
  • Does that mean `jdbc:google:mysql://mz-test:us-central1:mz-life-cloudsql-prod/mz_db` changes now to `jdbc:mysql://google/mz_db?cloudSqlInstance=jdbc:google:mysql&socketFactory=com.google.cloud.sql.mysql.SocketFactory` and I only need to add this dependency? – Stefan Falk Jun 05 '16 at 20:50
  • Oh.. no it must be `jdbc:mysql://google/mz_db?cloudSqlInstance=mz-test:us-central1:mz-life-cloudsql-prod&socketFactory=com.google.cloud.sql.mysql.SocketFactory` I guess – Stefan Falk Jun 05 '16 at 20:54
  • Okay it's complaining about "*The reference to entity "socketFactory" must end with the ';' delimiter.*" but I was thinking about another thing: What jdbc driver am I using now? Do I still use `com.mysql.jdbc.GoogleDriver`? (just asking because deployment takes almost forever ^^) – Stefan Falk Jun 05 '16 at 21:07
  • Where are you putting this? It sounds like an error from an XML parser. For Flex VMs, you will use the standard/official MySQL driver com.mysql.jdbc.Driver – Vadim Jun 05 '16 at 21:42
  • 1
    I've replaced the `&` with `&` and added a `;` but now I am getting [Could not create socket factory 'com.google.cloud.sql.mysql.SocketFactory;'](http://stackoverflow.com/questions/37647091/could-not-create-socket-factory-com-google-cloud-sql-mysql-socketfactory) .. – Stefan Falk Jun 05 '16 at 21:47
  • Maybe you can take a look at my other question (see above) but I guess your answer is correct so I'll accept it! Thank you :) – Stefan Falk Jun 05 '16 at 21:47
  • Thanks a TON for this @Vadim, I've spent 2 days trying to get this working before you posted this. It looks like this library only supports 2nd gen DBs, however. It appears to work, yet contradicts the Cloud SQL docs, which say flex env to 2nd gen dbs is not supported: https://cloud.google.com/appengine/docs/flexible/java/using-cloud-sql. Is this lib experimental, or will it be supported by Google? – avaynshtok Jun 06 '16 at 19:27
  • It's officially supported. Documentation update is in the works. – Vadim Jun 06 '16 at 19:44
  • "&" worked for me.. Thanks. `Note: If you are specifying the connection string in an XML file you might have to escape special characters like & to &.` – Nitin Sep 08 '16 at 05:57