问题描述
我在使用MySQL的Amazon EC2上运行Spring Hibernate应用程序。我正在考虑将我的应用程序移植到Google App Engine,因为Google现在通过Google Could SQL支持MySQL。
因此,将我现有的应用程序配置到Google App Engine Web应用程序,然后编译代码没有任何错误。我没有改变任何东西到我现有的应用程序,它编译并创建了所需的表并且成功启动了服务器。然而,当运行通过hibernate访问数据库的应用程序时,我收到以下错误 org.springframework.transaction.CannotCreateTransactionException:无法打开Hibernate的Session的交易;嵌套的例外是org.hibernate.exception.JDBCConnectionException:在org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:596)
在org.springframework.transaction.support无法打开连接
。 AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
在org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
在org.springframework.transaction.interceptor.TransactionInterceptor.invoke( TransactionInterceptor.java:105)在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172
)
在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java: 202)
为$ Proxy30.findById(未知来源)
。在com.openentry.catgen.services.impl.WebsiteServiceImpl.getMasterDomain(WebsiteServiceImpl.java:99)
我为实体类使用注解。
有什么需要改变的吗?
是我的applicationContext.xml
< bean id =dataSource
class =org.springframework.jdbc。 datasource.DriverManagerDataSource
p:driverClassName =$ {jdbc.driverClassName}p:url =$ {jdbc.url}
p:username =$ {jdbc.username}p:password = $ {jdbc.password}/>
< bean id =sessionFactory
class =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
p:dataSource-ref =dataSourcep:configurationClass =org.hibernate.cfg.AnnotationConfiguration
p:packagesToScan =com.package.app.entities>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> $ {hibernate.dialect}< / prop>
< prop key =hibernate.show_sql> $ {hibernate.show_sql}< / prop>
< prop key =hibernate.format_sql> $ {hibernate.format_sql}< / prop>
< prop key =hibernate.generate_statistics> $ {hibernate.generate_statistics}< / prop>
< prop key =hibernate.connection.useUnicode> $ {hibernate.connection.useUnicode}< / prop>
< prop key =hibernate.connection.characterEncoding> $ {hibernate.connection.characterEncoding}< / prop>
< prop key =hibernate.connection.charSet> $ {hibernate.connection.charSet}< / prop>
< prop key =hibernate.hbm2ddl.auto> $ {hibernate.hbm2ddl.auto}< / prop>
< /道具>
< / property>
< / bean>
< tx:annotation-driven />
< bean id =transactionManager
class =org.springframework.orm.hibernate3.HibernateTransactionManager
p:sessionFactory-ref =sessionFactory/>
< bean id =hibernateTemplateclass =org.springframework.orm.hibernate3.HibernateTemplate>
< constructor-arg>
< ref bean =sessionFactory/>
< / constructor-arg>
< / bean>
在Eclipse本地主机上运行Google Cloud SQL并在appengine实例中,您必须清单以下几点。
- 使用与您的Cloud SQL实例相同的Google帐户登录到eclipse。 / li>
- 右键点击项目属性,转到 Google> Appengine ,选中启用Google Cloud SQL实例
-
配置Google Cloud SQL实例:
实例名称:something:something // This this you get get在Google API控制台下的Google Cloud SQL选项卡下
数据库名称:yourdatabasename
用户名:yourMySQLUserName
密码:yourPassword
-
只需将您的mysql-connector-java-xxxx.jar文件复制并粘贴到Eclipse的Appengine SDK的此位置即可。
//此路径显示为Eclipse
D:\MyEclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.7.2 .1 \appengine -java-sdk-1.7.2.1\lib\impl\
-
AppengineDriver类名称:com.google.appengine .api.rdbms.AppEngineDriver
数据库访问URL:jdbc:google:rdbms:// instance_name / database_name
eg jdbc:google:rdbms:// XXXXXX:xxxxx / XXX_databasename
user:username //您的数据库用户默认其根目录
密码:password //您的数据库密码在GAE中默认为空白Cloud SQL
通过遵循所有的事情,您可以轻松地在Eclipse中配置Google Cloud SQL。
主机名:localhost
数据库名称:yourdatabasename
端口号:3306
用户名:yourMySQLUserName
密码:yourPassword
MySQL JDBC路径Jar:你的mysql-connector-java-xxxx.jar的路径//我通常会这个jar文件在WEB-INF / lib
I have a working Spring Hibernate Application running on Amazon EC2 with MySQL. I am thinking of porting my application to Google App Engine as Google now support MySQL with Google Could SQL.
So configure my existing application to Google App Engine Web Application, then compile the code without any error. I have not changed anything to my existing application and it compiled and also created the required tables and the server started successfully.
However, when running application that access the database via hibernate, I am getting the following error.
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:596)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy30.findById(Unknown Source)
at com.openentry.catgen.services.impl.WebsiteServiceImpl.getMasterDomain(WebsiteServiceImpl.java:99)
I am using annotations for my entity classes.
Is there anything I need changing for this?
below is my applicationContext.xml
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
p:username="${jdbc.username}" p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource" p:configurationClass="org.hibernate.cfg.AnnotationConfiguration"
p:packagesToScan="com.package.app.entities">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
<prop key="hibernate.connection.useUnicode">${hibernate.connection.useUnicode}</prop>
<prop key="hibernate.connection.characterEncoding">${hibernate.connection.characterEncoding}</prop>
<prop key="hibernate.connection.charSet">${hibernate.connection.charSet}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>
To run Google Cloud SQL on Eclipse localhost and on appengine instance, you must checklist the following points.
- Login to your eclipse with the same google account on which your Cloud SQL instance is created.
- Right Click on project properties go to Google>Appengine check the Enable Google Cloud SQL instance
Choose MySQL instance for localhost and configure its values.
Hostname : localhost Database Name : yourdatabasename Port No : 3306 Username : yourMySQLUserName Password : yourPassword Path to MySQL JDBC Jar : Path where your mysql-connector-java-x.x.xx.jar // I usually put this jar on the WEB-INF/lib
Configure the Google Cloud SQL Instance as well:
Instance Name : something:something // This you will get under Google Cloud SQL tab under Google API Console Database Name : yourdatabasename Username : yourMySQLUserName Password : yourPassword
Just copy your mysql-connector-java-x.x.xx.jar file and paste it under this location of your Appengine SDK in Eclipse
// This path is shown for Eclipse D:\MyEclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.7.2.1\appengine-java-sdk-1.7.2.1\lib\impl\
The Driver ClassName and Database Access URL should be changed to following values.
AppengineDriver Class Name : com.google.appengine.api.rdbms.AppEngineDriver Database Access URL : jdbc:google:rdbms://instance_name/database_name e.g. jdbc:google:rdbms://XXXXXX:xxxxx/XXX_databasename user : username // Your Database User by default its root password : password // Your Database Password by default its blank in GAE Cloud SQL
By following all the things you can easily configure Google Cloud SQL in Eclipse.
这篇关于Google Appengine Cloud Spring与Spring Hibernate的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!