簡體   English   中英

hibernate 無法與 Heroku postgresql 插件連接

[英]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&amp;password=043184abbcffdf268f7bbf8890eb3f697de1d6c1107973a579559d6dd5672cfa&amp;ssl=true&amp;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&amp;password=043184abbcffdf268f7bbf8890eb3f697de1d6c1107973a579559d6dd5672cfa&amp;ssl=true&amp;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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM