我面临一个非常奇怪的问题,我的App Engine服务器无法加载其Cloud SQL的GoogleDriver,这是错误(它发生在我运行“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
在我的Spring上下文文件中,我有:
<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}" />
...
有什么想法吗?
==
为了确认,我已经配置了我的appengine-web.xml,它根本没有帮助:
<?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>
-
由于hibernate jar版本之间存在一些不兼容性,我无法前进,因此我一直在尝试通过实际的发布管道(Jenkins + Maven + build + test + deploy),我不得不将Compute Engine VM的IP地址添加到Cloud SQL Instance的授权IP列表,以便运行我的单元测试并将其部署到应用程序(但是,如果我采用这种方法,我只能使用MySQL驱动程序和URL而不是GoogleDriver和URL。所以这是变得棘手......)。
我在我的Spring MVC配置文件加载的Production.properties文件中有这些属性:
################### 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
是否有一种简单的方法可以在不使用此方法的情况下动态切换外部和GAE Cloud SQL连接详细信息?
if (SystemProperty.environment.value() ==
SystemProperty.Environment.Value.Production) {
// Connecting from App Engine.
有什么想法吗?
答案 0 :(得分:2)
<use-google-connector-j>true</use-google-connector-j>
来启用它。这记录在:https://cloud.google.com/appengine/docs/java/cloud-sql/#enable_connector_j 编辑添加(来自以下评论):