【发布时间】: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