简体   繁体   English

使用Spring Hibernate的Google Appengine Cloud SQL问题

[英]Google Appengine Cloud SQL issue with Spring Hibernate

I have a working Spring Hibernate Application running on Amazon EC2 with MySQL. 我在Amazon EC2上使用MySQL运行Spring Hibernate Application。 I am thinking of porting my application to Google App Engine as Google now support MySQL with Google Could SQL. 我正在考虑将我的应用程序移植到Google App Engine,因为谷歌现在通过Google Can SQL支持MySQL。

So configure my existing application to Google App Engine Web Application, then compile the code without any error. 因此,将我现有的应用程序配置为Google App Engine Web应用程序,然后编译代码而不会出现任何错误。 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. 但是,当运行通过hibernate访问数据库的应用程序时,我收到以下错误。

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 下面是我的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. 要在Eclipse localhost和appengine实例上运行Google Cloud SQL,您必须检查以下几点。

  1. Login to your eclipse with the same google account on which your Cloud SQL instance is created. 使用创建Cloud SQL实例的相同Google帐户登录您的eclipse。
  2. Right Click on project properties go to Google>Appengine check the Enable Google Cloud SQL instance 右键单击项目属性转到Google> Appengine,然后选中启用Google Cloud SQL实例
  3. Choose MySQL instance for localhost and configure its values. 为localhost选择MySQL实例并配置其值。

     Hostname : localhost Database Name : yourdatabasename Port No : 3306 Username : yourMySQLUserName Password : yourPassword Path to MySQL JDBC Jar : Path where your mysql-connector-java-xxxx.jar // I usually put this jar on the WEB-INF/lib 
  4. Configure the Google Cloud SQL Instance as well: 同时配置Google Cloud SQL实例:

     Instance Name : something:something // This you will get under Google Cloud SQL tab under Google API Console Database Name : yourdatabasename Username : yourMySQLUserName Password : yourPassword 
  5. Just copy your mysql-connector-java-xxxx.jar file and paste it under this location of your Appengine SDK in Eclipse 只需复制你的mysql-connector-java-xxxx.jar文件并将其粘贴到Eclipse中的Appengine SDK的这个位置下

     // 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\\ 
  6. The Driver ClassName and Database Access URL should be changed to following values. 应将Driver ClassName和Database Access URL更改为以下值。

     AppengineDriver Class Name : com.google.appengine.api.rdbms.AppEngineDriver Database Access URL : jdbc:google:rdbms://instance_name/database_name eg 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 
  7. By following all the things you can easily configure Google Cloud SQL in Eclipse. 通过遵循所有内容,您可以轻松地在Eclipse中配置Google Cloud SQL。

我认为这是一个连接问题。您编写数据库配置错误或数据库和应用程序之间的连接有数据丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM