问题
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
回答1:
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 &
.
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.
来源:https://stackoverflow.com/questions/37644148/java-lang-illegalstateexception-could-not-load-jdbc-driver-class-com-mysql-jdb