繁体   English   中英

建议使用本地和Heroku postgres数据库的Hibernate配置

[英]Recommended Hibernate configuration for using a local and a Heroku postgres database

我正在开发一个连接到PostgreSQL数据库的Jersey应用程序。 我正在寻找一个解决方案,该方法如何配置Hibernate ,使其始终正确连接到本地数据库或基于Heroku的数据库(取决于是否在本地部署应用程序或将其推送到Heroku )。

使用Heroku指南,我尝试了以下操作:

HibernateUtil.java:

private static SessionFactory buildSessionFactory() {
    try {
        // Create the SessionFactory from hibernate.cfg.xml
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");

        configuration.setProperty("hibernate.connection.url",
                System.getenv("DATABASE_URL"));

        URI dbUri = new URI(System.getenv("DATABASE_URL"));

        String username = dbUri.getUserInfo().split(":")[0];
        String password = dbUri.getUserInfo().split(":")[1];
        String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':'
                + dbUri.getPort() + dbUri.getPath();
        configuration
                .setProperty("hibernate.connection.username", username);
        configuration
                .setProperty("hibernate.connection.password", password);
        configuration.setProperty("hibernate.connection.url", dbUrl);
        System.out.println("Hibernate Annotation Configuration loaded");

        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();
        System.out.println("Hibernate Annotation serviceRegistry created");

        SessionFactory sessionFactory = configuration
                .buildSessionFactory(serviceRegistry);

        return sessionFactory;
    } catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

我的hibernate.cfg.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 4.0//EN"
        "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection properties - Driver, URL, user, password -->
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost/MYAPP</property>
        <property name="hibernate.connection.username">user</property>
        <property name="hibernate.connection.password">password</property>

        <!-- org.hibernate.HibernateException: No CurrentSessionContext configured! -->
        <property name="hibernate.current_session_context_class">thread</property>

        <!-- Mapping with model class containing annotations -->
        <mapping class="com.example.object" />
    </session-factory>
</hibernate-configuration>

这个想法是HibernateUtil覆盖hibernate.cfg.xml属性(URL,用户,密码)。 本地部署有效,但部署到Heroku失败:

远程:[错误]无法在项目myapp上执行目标de.juplo:hibernate4-maven-plugin:.1.0:export(默认):目标de.juplo:hib rnate4-maven-plugin:1.1.0:export的执行默认值失败:调用Driver#connect:连接到localhost:5432时出错。 检查主机名和端口是否正确,并且邮局主管处的t正在接受TCP / IP连接。 连接被拒绝-> [帮助]

看起来在Heroku上,您的应用正在使用hibernate.cfg.xml的配置,而不是buildSessionFactory()方法。 您可以确认正在调用buildSessionFactory()吗?

我想你应该删除hibernate.connection.*从你的属性hibernate.cfg.xml和使用DATABASE_URL本地。 您可以在env中进行设置,也可以将其放入.env文件中,然后使用foreman start在本地运行您的应用程序,这将拾取该文件。 这是确保开发环境和生产环境之间配对的最佳方法,它将测试您的buildSessionFactory()代码。

如果愿意,您甚至可以按照本指南在本地使用Heroku数据库。 捕获Heroku DATABASE_URL并将其放入您的.env文件。 然后将ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory args添加到JDBC URL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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