简体   繁体   中英

How to apply DRY for Liquibase data source config and persistence.xml?

I am using liquibase and Java EE with JBoss. I configure the persistence.xml file in the project to specify the Data Source I want to use.

In JBoss configs:

<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>

In the project's 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>

And the project's web.xml:

<context-param>
  <param-name>liquibase.datasource</param-name>
  <param-value>java:jboss/datasources/WebStoriesDS</param-value>
</context-param>

Well, as you can see I have 1 data source reference in JBoss and 2 references in my project. How would I apply the DRY principle here and use only 1 reference in JBoss configs and 1 reference in my project? Use a properties file to hold the data source name?

I need just to configure it in a way that, when I need to change the name, I change only in 2 places: In my project and in JBoss configs.

Both standard Liquibase and JPA require the datasource to be explicitly listed. It is a logical reference, so you can change where the datasource points without affecting either configuration unless you want to change the datasource name (as you listed).

If you are wanting to remove the explicit reference from either Liquibase or JPA, the easiest is probably to subclass liquibase.integration.servlet.LiquibaseServletListener and override the getDataSource() method to look up reference from your PersitanceUnit and return it. That will allow you to only specify the datasource in persistance.xml and no longer need the liquibase.datasource configuration in web.xml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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