[英]hibernate cant connect with Heroku postgresql addon
我今天在 heroku 上部署了我的代码,但是因为我找不到将我的应用程序连接到 postgresql 插件的方法。
我尝试使用 getenv("DATABASE_URL") 将信息放入 HibernateUtil class 中的代码中,我尝试将 URL、USER 和 PASS 放入我的持久化文件中。
这就是我的 HibernateUtil class 的样子。
public static EntityManagerFactory factory = null;
static {
init();
}
private static void init() {
try {
if (factory == null) {
factory = Persistence.
createEntityManagerFactory("PERSISTENCE");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static EntityManagerFactory getFactory() {
return factory;
}
@Produces
@RequestScoped
public static EntityManager getEntityManager() {
return factory.createEntityManager();
}
这就是我的坚持。xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="PERSISTENCE">
<class>br.com.pitang.model.Telefone</class>
<class>br.com.pitang.model.Usuario</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://ec2-174-129-27-3.compute-1.amazonaws.com:5432/d33b5ude6vhu0b?username=eoyhrbbdifhuel&password=043184abbcffdf268f7bbf8890eb3f697de1d6c1107973a579559d6dd5672cfa&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.username" value="eoyhrbbdifhuel"/>
<property name="hibernate.connection.password" value="043184abbcffdf268f7bbf8890eb3f697de1d6c1107973a579559d6dd5672cfa"/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
<!-- c3p0 connection pool settings -->
<property name="hibernate.connection.provider_class" value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.min_size" value="1" />
<property name="hibernate.c3p0.max_size" value="5" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="3000" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.timeout" value="1800" />
</properties>
</persistence-unit>
</persistence>
我也在 pom.xml 中使用了这个依赖,因为我在评论中看到它有帮助,但没有。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>4.2.1.Final</version>
</dependency>
有没有在我的 heroku 应用程序中连接数据库的简单方法?
在您的persistence.xml
文件中,您指的是托管在 AWS 上的数据库,可能使用了错误的用户名和密码。 修复这些以引用托管在 Heroku 上的数据库:
<property name="hibernate.connection.url" value="jdbc:postgresql://ec2-174-129-27-3.compute-1.amazonaws.com:5432/d33b5ude6vhu0b?username=eoyhrbbdifhuel&password=043184abbcffdf268f7bbf8890eb3f697de1d6c1107973a579559d6dd5672cfa&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.username" value="eoyhrbbdifhuel"/>
<property name="hibernate.connection.password" value="043184abbcffdf268f7bbf8890eb3f697de1d6c1107973a579559d6dd5672cfa"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.