无法加载JDBC驱动程序类'com.mysql.jdbc.GoogleDriver' [英] Cannot load JDBC driver class 'com.mysql.jdbc.GoogleDriver'
问题描述
我面临一个非常奇怪的问题,那就是我的App Engine服务器无法加载其Cloud SQL的GoogleDriver,这是错误(运行mvn appengine:update后就会发生)。
org.springframework.transaction.CannotCreateTransactionException:无法为事务打开Hibernate Session;嵌套异常是org.hibernate.exception.GenericJDBCException:无法打开连接
在org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:596)
...
引起:org.apache.commons.dbcp.SQLNestedException:无法在org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1429)$ b加载JDBC驱动程序类'com.mysql.jdbc.GoogleDriver'
$ b在org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
在org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
在org .springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 48 more
由:java.lang.ClassNotFoundException引起:com.mysql.jdbc.GoogleDriver
appengine.properties
################### MySQL配置 - Google Cloud App Engine ################## ########
jdbc.driverClassName = com.mysql.jdbc.GoogleDriver
jdbc.url = jdbc:google:mysql:// mytestapp:testdb?user = someuser
jdbc.username = someuser
jdbc.password = ******
jdbc.dialect = org.hibernate.dialect.MySQLDialect
在我的Spring上下文中,我有:
< context:property -placeholder location =classpath:appengine.properties/>
< bean
id =dataSource
class =org.apache.commons.dbcp.BasicDataSource
destroy-method =close>
name =driverClassName
value =$ {jdbc.driverClassName}/>
name =url
value =$ {jdbc.url}/>
...
有什么想法?
==
为了确认,我已经配置了我的appengine-web.xml,它根本没有帮助:
<?xml version =1.0encoding =utf-8?>
< appengine-web-app xmlns =http://appengine.google.com/ns/1.0>
< application> mytestapp< / application>
< version> 1< / version>
< threadsafe> true< / threadsafe>
<会话启用>真实< /会话启用>
< system-properties>
< property name =java.util.logging.config.filevalue =WEB-INF / logging.properties/>
< / system-properties>
< use-google-connector-j> true< / use-google-connector-j>
< / appengine-web-app>
-
我无法由于hibernate jar版本之间存在一些不兼容问题,所以我继续尝试通过实际版本管道(Jenkins + Maven + build + test + deploy),我不得不将Compute Engine虚拟机的IP地址添加到云授权IP列表中SQL实例为了运行我的单元测试并将其部署到应用程序(但是,如果我保持这种方法,我只能使用MySQL驱动程序和URL而不是GoogleDriver和URL,所以这变得棘手......)。
我在由我的Spring MVC配置文件加载的Production.properties文件中拥有这些属性:
################### MySQL配置 - Google Cloud App Engine ############### ###########
jdbc.driverClassName = com.mysql.jdbc.GoogleDriver
jdbc.url = jdbc:google:mysql:// ******* testapp :testsqldb?user = root
jdbc.username = root
jdbc.password = *****
jdbc.dialect = org.hibernate.dialect.MySQL方言
是否有一种简单的方法可以在不使用此方法的情况下动态切换外部和GAE Cloud SQL连接详细信息?
if(SystemProperty.environment.value()==
SystemProperty.Environment.Value.Production){
//从App Engine连接。
有什么想法?
- 该类不会在App Engine运行时自动提供。您必须通过向appengine-web.xml中添加
< use-google-connector-j> true< / use-google-connector-j>
来启用它。详情请参阅: https://cloud.google.com/appengine / docs / java / cloud-sql /#enable_connector_j
编辑添加(来自以下评论):
- 另外,从GAE连接到Cloud SQL时,应该将密码字段留空。
- 请确保您的代码在GAE之外运行(例如,在工作站上,在GCE上,在Jenkins版本上),它使用库存MySQL连接器,因为Google连接器仅在GAE上可用。
- 您可能也想要查看使用股票MySQL驱动程序,它可以从GAE和其他连接中使用。在 https:// github上有一个演示。 com / GoogleCloudPlatform / appengine-cloudsql-native-mysql-hibernate-jpa-demo-java
I am facing a very bizarre problem where my App Engine server can't load its Cloud SQL's GoogleDriver, here's the error (It happens right after I run "mvn appengine:update").
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:596)
...
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.GoogleDriver'
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1429)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 48 more
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.GoogleDriver
appengine.properties
################### MySQL Configuration - Google Cloud App Engine ##########################
jdbc.driverClassName=com.mysql.jdbc.GoogleDriver
jdbc.url=jdbc:google:mysql://mytestapp:testdb?user=someuser
jdbc.username=someuser
jdbc.password=******
jdbc.dialect=org.hibernate.dialect.MySQLDialect
In my Spring context file I have:
<context:property-placeholder location="classpath:appengine.properties" />
<bean
id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" >
<property
name="driverClassName"
value="${jdbc.driverClassName}" />
<property
name="url"
value="${jdbc.url}" />
...
Any ideas?
==
Just to confirm, I had already configured my appengine-web.xml and it doesn't help at all:
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>mytestapp</application>
<version>1</version>
<threadsafe>true</threadsafe>
<sessions-enabled>true</sessions-enabled>
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
<use-google-connector-j>true</use-google-connector-j>
</appengine-web-app>
--
I couldn't move forward due to some incompatibility between hibernate jar versions so I kept trying through the actual release pipeline (Jenkins + Maven + build + test + deploy), I had to add the Compute Engine VM's IP address to the list of authorized IPs of the Cloud SQL Instance in order to run my unit tests and deploy it to the application (but, if I keep this approach, I can only use the MySQL driver and URL instead of the GoogleDriver and URL. So this is getting tricky...).
I have these properties in a Production.properties file that is loaded by my Spring MVC config file:
################### MySQL Configuration - Google Cloud App Engine ##########################
jdbc.driverClassName=com.mysql.jdbc.GoogleDriver
jdbc.url=jdbc:google:mysql://*******testapp:testsqldb?user=root
jdbc.username=root
jdbc.password=*****
jdbc.dialect=org.hibernate.dialect.MySQLDialect
Is there an easy way to dynamically switch between external and GAE Cloud SQL connection details without using this approach?
if (SystemProperty.environment.value() ==
SystemProperty.Environment.Value.Production) {
// Connecting from App Engine.
Any ideas?
- The class is not automatically available in the App Engine runtime. You must enable it by adding
<use-google-connector-j>true</use-google-connector-j>
to your appengine-web.xml. This is documented at: https://cloud.google.com/appengine/docs/java/cloud-sql/#enable_connector_j
Edited to add (from comments below):
- Also, when connecting to Cloud SQL from GAE you should leave the password field empty.
- You should also make sure that if your code is running outside GAE (e.g. on your workstation, on GCE, on a Jenkins build) it uses stock MySQL connector as the Google connector is only available on GAE.
- You might also want too look into using the stock MySQL driver, which works both from GAE and other connections. There is a demo of this at https://github.com/GoogleCloudPlatform/appengine-cloudsql-native-mysql-hibernate-jpa-demo-java
这篇关于无法加载JDBC驱动程序类'com.mysql.jdbc.GoogleDriver'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!