【问题标题】:How can I configure Hibernate with environment variable如何使用环境变量配置 Hibernate
【发布时间】:2012-01-11 02:12:45
【问题描述】:

所以我想在 heroku 上部署我的 java 应用程序。部署后,它会设置一个环境变量 DATABASE_URL。我想用它作为我的休眠网址。我目前有 hibernate.cfg.xml 并在那里设置了 url jdbc:postgresql://localhost:port/db 像这样。如何将其更改为 DATABASE_URL?

【问题讨论】:

    标签: hibernate postgresql configuration heroku


    【解决方案1】:

    其中一种方法是在创建 SessionFactory 之前使用 Configuration 中的 setProperty(String propertyName, String value) 显式覆盖 hibernate.connection.url 的值。

    要获取环境变量,可以使用System.getenv(String name)

    /**Load the hibernate.cfg.xml from the classpath**/
    Configuration cfg = new Configuration();
    cfg.setProperty("hibernate.connection.url", System.getenv("DATABASE_URL"));
    SessionFactory sessionFactory = cfg.buildSessionFactory();
    

    【讨论】:

    • 非常感谢您在几分钟前解决了这个问题。 :)
    • 请告诉我是否可以。祝你好运:)
    • 您可能需要调整从 System.getenv("DATABASE_URL") 返回的字符串。
    【解决方案2】:

    希望对你有帮助,

    我正在使用 Jboss AS 5.x 的 HSQL DB 和休眠来动态创建表并使用以下 *.cfg.xml 文件。

    使用$JBOSS_HOME 作为环境变量。

    <?xml version="1.0" encoding="UTF-8"?>
    

        <session-factory>
    
                <!-- Database connection settings -->
    
                <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
                <property name="connection.url">jdbc:hsqldb:$JBOSS_HOME/server/test/data/hypersonic/localDB</property>
                <property name="connection.username">sa</property>
    
                <property name="connection.password"></property>
    
                <!-- JDBC connection pool (use the built-in) -->
                <property name="connection.pool_size">1</property>
    
                <!-- SQL dialect -->
                <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
    
                <!-- Enable Hibernate's automatic session context management -->
                <property name="current_session_context_class">thread</property>
    
                <!-- Disable the second-level cache -->
                <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
                <!-- Echo all executed SQL to stdout -->
                <property name="show_sql">true</property>
                <!-- Drop and re-create the database schema on startup -->
    
                <property name="hbm2ddl.auto">update</property>
                <!--  Mapping files  -->
                <mapping resource="friends_presence_log.hbm.xml" />
                <mapping resource="profileuuid.hbm.xml" />
        </session-factory>
    

    所以,这意味着如果你想在 Jboss 配置中使用环境变量,那么你可以正常使用,稍后由内部 hibernate.jar 实用程序获取,你可以像在 java 程序中一样正常获得连接或其他东西。

    【讨论】:

    • 使用 $VAR 不会被解析
    【解决方案3】:

    我搜索了很多其他解决方案,而没有在 java 本身中进行任何编程。 我得出以下结论。

    <?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.check_nullability">false</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.username">${hibernate_username}</property>
        <property name="hibernate.connection.password">${hibernate_password}</property>
        <property name="hibernate.connection.url">jdbc:postgresql://${hibernate_db_host}/${hibernate_db_name}</property>
        <property name="hibernate.search.autoregister_listeners">false</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">${hibernate_show_sql}</property>
    </session-factory>
    </hibernate-configuration>
    

    我使用以下 vmargs 启动我的应用程序:

    -Dhibernate_username=test -Dhibernate_password=testpassword -Dhibernate_db_host=localhost -Dhibernate_db_name=test -Dhibernate_show_sql=true
    

    我将此解决方案发布到此旧帖子,因为我在旧论坛帖子中发现了此问题(Google Search Side 3+ ^^)。而且我认为这非常有用。

    【讨论】:

    • 为什么我们需要将它们作为 VM 参数传入,而不仅仅是在环境变量中设置它们?
    • 在我们的用例中,休眠配置文件被打包在一个 jar 文件中。我们的客户编辑这些文件有点乏味。这就是我们将它们打包在 VM 参数中的原因,它们更易于配置。
    • 感谢您对旧话题的回复!有没有什么方法可以像你一样设置它,而不是 VMargs 使用环境变量?
    【解决方案4】:

    如果你使用像 gradle 这样的构建工具,你可以使用 @...@ 括起来的占位符,如下所示:

    <?xml version = "1.0" encoding = "utf-8"?>
    <!DOCTYPE hibernate-configuration SYSTEM 
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
       <session-factory>
          <property name = "hibernate.dialect">
             @db_dialect@
          </property>
    
          <property name = "hibernate.connection.driver_class">
             @db_driver@
          </property>
    
          <!-- Assume test is the database name -->
    
          <property name = "hibernate.connection.url">
             @db_url@
          </property>
    
          <property name = "hibernate.connection.username">
             @db_user@
          </property>
    
          <property name = "hibernate.connection.password">
             @db_password@
          </property>
    
          <!-- Cannot use anything other than "validate" in production. --> 
          <property name="hibernate.hbm2ddl.auto">validate</property>
    
          <!-- Connection pooling -->
          <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
          <property name="hibernate.c3p0.min_size">1</property>
          <property name="hibernate.c3p0.max_size">500</property>
          <property name="hibernate.c3p0.timeout">120</property>
          <property name="hibernate.c3p0.max_statements">1024</property>
    
          <!-- Your mappings -->
       </session-factory>
    </hibernate-configuration>
    

    然后在 build.gradle 你会有这样的东西:

    def db_driver = System.getenv('MYAPP_DB_DRIVER')
    def db_url = System.getenv('MYAPP_DB_URL')
    def db_dialect = System.getenv('MYAPP_DB_DIALECT')
    
    processResources {
        filesMatching(['**/*.xml', '**/*.properties', '**/*.json']) {
            filter ReplaceTokens, tokens: [
                'db_driver': db_driver,
                'db_url': db_url,
                'db_dialect': db_dialect,
            ]
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-11
      • 2019-04-10
      • 2011-09-11
      • 2020-09-28
      • 2019-12-02
      • 1970-01-01
      • 2019-08-03
      • 2014-08-05
      相关资源
      最近更新 更多