【问题标题】:Hibernate working on local machine but failing on HerokuHibernate 在本地机器上工作,但在 Heroku 上失败
【发布时间】:2019-05-12 19:53:04
【问题描述】:

所以我最近在 Heroku 上部署了我的应用程序,但是当我尝试将休眠连接添加到数据库时它失败了。我将 JawsDB 作为附加组件添加到我的 heroku 应用程序并使用此 hibernate.cfg.xml 连接到它:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:mysql://ehc1u4pmphj917qf.cbetxkdyhwsb.us-east-1.rds.amazonaws.com:3306/ycm07x7z21h40k84</property>
        <property name="hibernate.connection.username">username</property>
        <property name="hibernate.default_schema">ycm07x7z21h40k84</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>
        <property name="hibernate.connection.CharSet">utf8</property>
        <property name="hibernate.connection.characterEncoding">utf8</property>
        <property name="hibernate.connection.useUnicode">true</property>
    </session-factory>
</hibernate-configuration>

我的 Hibernate 配置类如下:

public class HibernateUtils {
  private static SessionFactory sessionFactory;

  static {
    try {
      Configuration configuration = new Configuration().configure();

      configuration.addAnnotatedClass(City.class);
      ...
      configuration.addAnnotatedClass(Review.class);

      StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
              .applySettings(
                      configuration.getProperties())
              .build();

      sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    } catch (Exception ex) {
      System.out.println("----------------------");
      System.out.println(ex);
    }
  }

  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }
}

然后,我将 SessionFactory 作为 bean 连接,它在我需要的地方作为依赖项提供。当我编译它并在本地运行它时一切正常,但是当我将它部署到 Heroku 时,会发生以下错误:

2018-12-11T18:52:35.687631+00:00 app[web.1]: ----------------------
2018-12-11T18:52:35.687723+00:00 app[web.1]: org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
2018-12-11T18:52:35.692621+00:00 app[web.1]: 2018-12-11 18:52:35.692  WARN 4 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'usersService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dbProfileService' defined in URL [jar:file:/app/build/libs/build_3eb7c290daa3670885240cb93ab898a5-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/squadknowhow/services/DbProfileService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'provideUsersGenericRepository' defined in class path resource [squadknowhow/configurations/AppConfig.class]: Unsatisfied dependency expressed through method 'provideUsersGenericRepository' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2018-12-11T18:52:35.696062+00:00 app[web.1]: 2018-12-11 18:52:35.695  INFO 4 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-12-11T18:52:36.049747+00:00 app[web.1]: 2018-12-11 18:52:36.049  WARN 4 --- [ost-startStop-1] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
2018-12-11T18:52:36.539830+00:00 heroku[web.1]: Process exited with status 1
2018-12-11T18:52:36.557418+00:00 heroku[web.1]: State changed from starting to crashed

我绝对确定用户名和密码是有效的。我已经使用该信息成功地连接到带有 MySQL Workbench 的数据库。我怀疑我必须另外配置 Hibernate 才能在 Heroku 上使用它,但不幸的是我找不到适合我的案例的教程。如果您想了解更多信息,我非常乐意提供。如果您能指出我正确的方向,我们将不胜感激。

【问题讨论】:

  • 您使用过时的配置而不是 Spring Boot 和 Hibernate JPA 特性是否有特定原因?
  • 我之所以使用它,是因为在有关 Spring MVC 和 Hibernate 的课程中​​,他们向我展示了如何配置它,并且它一直有效。
  • 我应该使用 Spring Boot 和 Hibernate JPA 吗?
  • 这是我的建议。看来您正在使用针对 Hibernate 3 的资源;当前版本是 5.3。使用 Spring Boot,您唯一需要做的就是提供 JDBC 连接 URL(包括或单独的用户名/密码),如果绑定到应用程序,Boot 可以从 Heroku 和 Cloud Foundry 自动检测它。此设置的整个其余部分将自动发生。 (如果可以选择,最好使用 JPA API、Spring @Transactional 和 Spring Data JPA。)
  • 能否请你给我一个关于如何迁移或设置它的好教程。

标签: java mysql hibernate spring-mvc heroku


【解决方案1】:

基本上问题是 Heroku 找不到 hibernate.cfg.xml 文件,但同时配置 java 文件需要它。解决方案是在没有 hibernate.cfg.xml 文件的情况下配置休眠,我从这个网站找到了如何做:https://www.concretepage.com/hibernate/configure_hibernate_without_hibernate_cfg_xml

【讨论】:

  • 谢谢!遇到同样的问题,您的解决方案仍然有效。
【解决方案2】:

你应该把你的cfg文件放在src/main/resources或者你可以告诉配置cfg文件的绝对路径是这样的:

configuration.configure("/*{PATH-TO-YOUR-FILE}*/hibernate.cfg.xml"); (第一个更好。)

【讨论】:

  • 我不确定我是否理解您将我的 cfg 文件放在资源文件夹中的意思。如果我将它移动到资源中,我将如何引用该文件以获取 SessionFactory。
  • 如果您无法更改文件位置,您可以在拨打Configure 时设置cfg 文件的路径(正如我在回答中的第二种方式)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 2017-06-05
  • 2015-03-12
  • 1970-01-01
  • 1970-01-01
  • 2012-10-11
  • 2011-07-31
相关资源
最近更新 更多