1

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?

Apollo
  • 1,296
  • 2
  • 11
  • 24
  • 1
    Your basic approach looks correct to me: you have a defaultVar in server.xml , overridden by a regular server.xml var specified in pom.xml, and the value of this regular server.xml var is in turn overridden at the CLI via `-Dliberty.var.postgres.host=localhost`. You should indeed end up with "localhost". – Scott Kurz Jun 28 '20 at 15:12
  • 2
    The next thing I would do in a case like this is to verify if the Liberty server config is actually what I expect it to be, yet maybe this is still not "correctly" pointing to an actual DB at this config. E.g. see this blog: https://openliberty.io/blog/2019/09/13/testing-database-connections-REST-APIs.html. So add `restConnector-2.0` and an admin user (if you don't have one), e.g. `` and hit URL: https://localhost:9443/ibm/api/config/dataSource/todoListDS. What does it show for "postgres.host" ? – Scott Kurz Jun 28 '20 at 15:15
  • 1
    If that still looks correct I wonder what version of Open Liberty and liberty-maven-plugin you are using? – Scott Kurz Jun 28 '20 at 15:15
  • @ScottKurz liberty-maven-plugin 3.2 and Open Liberty 20.0.06. I'll check the restConnector. Good idea. – Apollo Jun 28 '20 at 15:25
  • Okay, you're right. The database host seem to be fine: ``` "properties.postgresql": { "databaseName": "postgres", "password": "******", "portNumber": 5432, "preparedStatementCacheQueries": 0, "serverName": "localhost", "user": "postgres" },``` – Apollo Jun 28 '20 at 15:36
  • @ScottKurz Now it works. I have no idea what has changed. Thanks for all the tips! – Apollo Jun 28 '20 at 15:41

1 Answers1

0

See the comments on the question for the answer.

Apollo
  • 1,296
  • 2
  • 11
  • 24