Hibernate JTA:每个环境读取数据库连接参数 [英] Hibernate JTA : Read DB connection parameters per environment

查看:94
本文介绍了Hibernate JTA:每个环境读取数据库连接参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用hibernate编写一个javaEE应用程序。该应用程序将在多个环境(dev,qa,prod等)上运行。将具有与它们中的每一个相关联的单独的dbs。我想为每个环境分别设置hibernate属性,如jdbc-url,username,password等。



我目前的持久性。

 < persistence-unit name =PUtransaction-type = JTA> 
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< validation-mode> CALLBACK< / validation-mode>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.OracleDialect/>
< property name =hibernate.hbm2ddl.autovalue =validate/>

< property name =hibernate.temp.use_jdbc_metadata_defaultsvalue =false/>
< property name =hibernate.event.merge.entity_copy_observervalue =allow/>

< property name =hibernate.connection.driver_classvalue =oracle.jdbc.OracleDriver/>

< property name =hibernate.connection.usernamevalue =abc/>
< property name =hibernate.connection.passwordvalue =***/>

< / properties>
< / persistence-unit>

我在我的java代码中使用持久化单元如下:

  @PersistenceContext(unitName =PU)
private EntityManager em;

有没有一种方法可以将存储在单独属性文件中的hibernate属性插入到EntityManager适用于不同的环境?



请注意,我使用的是JTA,因此无法使用EntityManagerFactory。我也不是&不希望使用spring。

解决方案

真的是BAAAAAAD的想法,在应用程序WAR / EAR中包含特定于环境的信息捆绑(通过包含多个配置,或者为不同的环境创建不同的捆绑包)。例如,不同的数据库应该保存在容器中。



在你的情况中,你的 persistence.xml 应该看起来像:

 <持久性> 
< persistence-unit name =PU>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< jta-data-source> jdbc / fooAppDs< / jta-data-source>
<属性>
... ...
< / properties>
< / persistence-unit>
< /余辉>

当然,您应该有相应的资源引用 jdbc / fooAppDs



通过这样做,您可以在任何环境中部署您的应用程序。您只需在您的容器中创建正确的数据源并将其分配给 jdbc / fooAppDs






我相信另一种方法可行,但我不建议,通过在classpath中创建 hibernate.cfg.xml 。您可能希望拥有本地文件系统位置并将其添加到classpath中,而不是将该文件放入JAR / WAR / EAR中。


I am writing a javaEE application, using hibernate. The application will be running on multiple environments (dev, qa, prod etc.) & will have separate dbs's associated with each of them. I would like to set the hibernate properties like jdbc-url, username , password etc. separately for each of these environments.

My current persistence.xml looks like :

    <persistence-unit name="PU" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <validation-mode>CALLBACK</validation-mode>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />

            <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
            <property name="hibernate.event.merge.entity_copy_observer" value="allow"/>

            <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
            <property name="hibernate.connection.url" value="jdbc:oracle:thin:@host/schema"/>

            <property name="hibernate.connection.username" value="abc"/>
            <property name="hibernate.connection.password" value="***"/>    

        </properties>
    </persistence-unit>

I am using the persistence unit as follows in my java code:

@PersistenceContext(unitName = "PU")
private EntityManager em; 

Is there a way that I can inject the hibernate properties, which are stored in separate properties files, into EntityManager for different environments ?

Please note that I am using JTA and hence cannot use EntityManagerFactory. Also I am not & do not want to use spring.

解决方案

It is really a BAAAAAAD idea to include environment-specific information in your application WAR/EAR bundle (either by including multiple configs, or by creating different bundles for different environment). Different database, for example, should be maintained in the container.

In your case, your persistence.xml should look like:

<persistence>
   <persistence-unit name="PU">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>jdbc/fooAppDs</jta-data-source>
      <properties>
         ... ...
      </properties>
   </persistence-unit>
</persistence>

and of course you should have corresponding resource-ref for jdbc/fooAppDs.

By doing so, you can deploy your application in any environment. You just need to create correct datasource in your container and assign it to jdbc/fooAppDs.


Another approach I believe will work, though I will not recommend, is by creating hibernate.cfg.xml in classpath. You may want to have a local file system location and add that in classpath, instead of putting the file in your JAR/WAR/EAR.

这篇关于Hibernate JTA:每个环境读取数据库连接参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆