Hibernate在本地计算机上工作,但在Heroku上失败

时间:2018-12-11 18:57:11

标签: java mysql hibernate spring-mvc heroku

所以我最近在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上使用它,但是不幸的是我找不到适合我的案例的教程。如果您需要更多信息,我非常乐意提供。如果您能指出正确的方向,也将不胜感激。

2 个答案:

答案 0 :(得分:1)

基本上,问题是Heroku找不到hibernate.cfg.xml文件,但同时配置Java文件需要它。解决方案是在没有hibernate.cfg.xml文件的情况下配置hibernate,我从该网站上发现了该怎么做:https://www.concretepage.com/hibernate/configure_hibernate_without_hibernate_cfg_xml

答案 1 :(得分:0)

您应该将cfg文件放置在src/main/resources中,或者可以这样配置cfg文件的绝对路径:

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