[英]How to apply DRY for Liquibase data source config and persistence.xml?
我在JBoss中使用liquibase和Java EE。 我在項目中配置了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
子類liquibase.integration.servlet.LiquibaseServletListener
並重寫getDataSource()
方法以從PersitanceUnit中查找引用並返回它。 這樣一來,您僅可以在persistance.xml中指定數據源,而不再需要在web.xml中進行liquibase.datasource配置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.