java.lang.IllegalStateException:无法加载 JDBC 驱动程序类 [com.mysql.jdbc.GoogleDriver] [英] java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.GoogleDriver]
问题描述
我正在尝试将我的数据库从 Google 灵活环境连接到 Google Cloud SQL.连接字符串和驱动程序类如下所示:
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>
但是,我目前得到
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]
我尝试连接的数据库是第二代 Cloud SQL MySQL 数据库.
The database I am trying connecting to is a Second Generation Cloud SQL MySQL database.
为什么我会收到此异常?
Why am I getting this exception?
App Engine 具有此
属性.我还没有看到灵活环境的这个属性 - 至少在我目前阅读的那些页面上没有看到.是否需要在 app.yaml 文件中添加任何内容?
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?
不确定是否必须在灵活环境中执行此操作,但我目前正尝试在我的 yaml 文件中将 use-google-connector-j
属性设置为 true:
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
但目前看来这不起作用:https://code.google.com/p/googleappengine/issues/detail?id=11444
but it appears this is not working at the moment: https://code.google.com/p/googleappengine/issues/detail?id=11444
推荐答案
com.mysql.jdbc.GoogleDriver
旨在为 App Engine 标准环境应用程序工作.
com.mysql.jdbc.GoogleDriver
is designed to work for App Engine Standard Environment applications.
对于在 App Engine 柔性环境应用程序上运行的 Java 应用程序,请使用 mysql-socket-工厂图书馆.
For Java applications running on App Engine Flexible Environment applications use the mysql-socket-factory library.
对于基于 Maven 的应用程序,添加对库的依赖:
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>
切换到标准/官方com.mysql.jdbc.Driver
.连接字符串从
Switch to the standard/official com.mysql.jdbc.Driver
. The connection string changes from
jdbc:google:mysql://instance_name/db_name
到
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.
注意:如果您在 XML 文件中指定连接字符串,您可能需要将诸如 &
之类的特殊字符转义为 &代码>.
Note: If you are specifying the connection string in an XML file you might have to escape special characters like &
to &
.
注意:此方法不适用于mysql驱动(6及以上)的开发线.我不得不使用生产 5.1.39 版本.
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.
这篇关于java.lang.IllegalStateException:无法加载 JDBC 驱动程序类 [com.mysql.jdbc.GoogleDriver]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!