java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.GoogleDriver]
Asked Answered
D

1

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

Does answered 5/6, 2016 at 16:23 Comment(2)
But you added both tags to your questionPiotrowski
@Piotrowski 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.Does
P
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.

Prisilla answered 5/6, 2016 at 20:35 Comment(9)
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?Does
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 guessDoes
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 ^^)Does
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.DriverPrisilla
I've replaced the & with &amp; and added a ; but now I am getting Could not create socket factory 'com.google.cloud.sql.mysql.SocketFactory;' ..Does
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 :)Does
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: cloud.google.com/appengine/docs/flexible/java/using-cloud-sql. Is this lib experimental, or will it be supported by Google?Kaliski
It's officially supported. Documentation update is in the works.Prisilla
"&amp;" 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 &amp;.Keeley

© 2022 - 2024 — McMap. All rights reserved.