简体   繁体   中英

hibernate cant connect with Heroku postgresql addon

I deployed my code on heroku today, but since, i cant find a way of connecting my app to the postgresql addon.

I tried to put the info inside my code in the HibernateUtil class with getenv("DATABASE_URL"), i tried putting the URL, USER and PASS in my persistence.xml file but it didnt work either.

Thats what my HibernateUtil class look like.

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();
    }

Thats my persistence.xml file

<?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>


I was also using this dependecy in pom.xml because ive seen in a comment around that it helped, but didnt.

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>4.2.1.Final</version>
 </dependency>

Is there a simple way of connecting a database in my heroku app?

In your persistence.xml file you are refering to a database hosted on AWS, likely with the wrong username and password. Fix these to reffer to the database hosted on 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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM