Search code examples
javamavenopen-liberty

How to switch database with Open Liberty on runtime?


I am trying to switch the database host in Open Liberty using the maven-liberty-plugin via setting a variable I use in the server.xml.

In my pom.xml I have the following property:

<liberty.var.postgres.host>database</liberty.var.postgres.host>

In my server.xml I have this (excerpt):

<variable name="postgres.host" defaultValue="database"/>

<dataSource id="todoListDS"
            jndiName="jdbc/TodoListDS"
            type="javax.sql.ConnectionPoolDataSource">

    <jdbcDriver id="postgresql-driver"
                javax.sql.XADataSource="org.postgresql.xa.PGXADataSource"
                javax.sql.DataSource="org.postgresql.ds.PGDataSource"
                javax.sql.ConnectionPoolDataSource="org.postgresql.ds.PGConnectionPoolDataSource"
                libraryRef="postgresql-library"/>

    <properties.postgresql serverName="${postgres.host}"
                           portNumber="${postgres.port}"
                           databaseName="${postgres.database}"
                           user="${postgres.user}"
                           password="${postgres.password}"/>
</dataSource>

Then I run my server via: mvn -Dliberty.var.postgres.host=localhost clean package liberty:run but the connection seem to fail. I tried the same with the https port and that worked fine. Just the database host does not seem to work. It works when I change liberty.var.postgres.host in the pom.xml to localhost.

How can I switch the database host here?


Solution

  • See the comments on the question for the answer.