我正在将 liquibase 和 Java EE 与 JBoss 一起使用。我在项目中配置 persistence.xml
文件来指定我要使用的数据源。
在 JBoss 配置中:
<datasources>
<datasource jta="true" jndi-name="java:jboss/datasources/WebStoriesDS" pool-name="java:jboss/datasources/WebStoriesDS" enabled="true" use-java-context="true" use-ccm="true">
<connection-url>jdbc:postgresql://127.0.0.1:5432/mydatabase</connection-url>
<driver>org.postgresql</driver>
<security>
<user-name>postgres</user-name>
<password>postgres</password>
</security>
</datasource>
<drivers>
<driver name="org.postgresql" module="org.postgresql">
<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
在项目的 persistence.xml 中:
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="PersistenceUnit">
<jta-data-source>java:jboss/datasources/WebStoriesDS</jta-data-source>
</persistence-unit>
</persistence>
以及项目的 web.xml:
<context-param>
<param-name>liquibase.datasource</param-name>
<param-value>java:jboss/datasources/WebStoriesDS</param-value>
</context-param>
嗯,正如您所看到的,我在 JBoss 中有 1 个数据源引用,在我的项目中有 2 个引用。我如何在这里应用 DRY 原则并在 JBoss 配置中仅使用 1 个引用,在我的项目中仅使用 1 个引用?使用属性文件来保存数据源名称?
我只需要以这样的方式配置它,当我需要更改名称时,我只在 2 个位置进行更改:在我的项目中和 JBoss 配置中。
最佳答案
标准 Liquibase 和 JPA 都要求显式列出数据源。它是一个逻辑引用,因此您可以更改数据源指向的位置,而不会影响任何配置,除非您想更改数据源名称(如您所列)。
如果您想从 Liquibase 或 JPA 中删除显式引用,最简单的方法可能是子类化 liquibase.integration.servlet.LiquibaseServletListener
并覆盖 getDataSource()
方法从 PersitanceUnit 查找引用并返回它。这将允许您仅在 persistance.xml 中指定数据源,而不再需要 web.xml 中的 liquibase.datasource 配置。
关于java - 如何对 Liquibase 数据源配置和 persistence.xml 应用 DRY?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27019584/