configuring dev and prod datasource in liberty dynamically to load based on the environment

1.4k views Asked by At

I am running java batch(jsr352) using liberty server. The datasource configured in server.xml. I would like to load server.xml based on the region(like dev, sit, prod). How can I pass arguments to start liberty server and load the datasource dynamically There could be possiblity with server.env file and bootstrap.properties. since new to this.. can anyone help on this.

2

There are 2 answers

1
Andy Guibert On BEST ANSWER

An easy way to do this is to use variables in your server.xml like this:

<dataSource jndiName="jdbc/db2">
    <jdbcDriver libraryRef="DB2JCCLib"/>
    <properties.db2.jcc databaseName="${evn.db2_name}" 
                        serverName="${env.db2_server}" 
                        portNumber="${env.db2_port}"/>
</dataSource>

Then, you could can set the variables in your server.env like this:

db2_name=mydb
db2_server=whatever.com
db2_port=50000

Alternatively, if you use any sort of scripting to start your Liberty servers, you can export them in the bash env like this:

$ export db2_name=mydb
$ etc...
$ wlp/bin/server start myServer
2
Andy Guibert On

If you have configuration differences that go beyond attribute values, variables may not be sufficient. For example, suppose you use an in-memory database in dev (like Derby embedded) and a more robust database in production (like DB2).

In your primary server.xml, you can include another config xml file using a variable like this:

<server>
  <include location="dbconfig-${env.ENV_LOCATION}.xml"/>

  <!-- rest of common config here -->
</server>

Then you can have dev-only config in dbconfig-dev.xml like this:

<server>
  <dataSource id="db" jndiName="jdbc/db">
    <jdbcDriver libraryRef="DerbyLib"/>
    <properties.derby.embedded databaseName="memory:testdb" createDatabase="create"/>
  </dataSource>
  <library id="DerbyLib">
    <fileset dir="/path/to/derby.jar"/>
  </library>
</server>

And production-only config in dbconfig-prod.xml like this:

<server>
  <dataSource id="db" jndiName="jdbc/db">
    <jdbcDriver libraryRef="DB2JCCLib"/>
    <properties.db2.jcc databaseName="PRODUCTION_DB" 
                        serverName="serious.business.com" 
                        portNumber="50000"/>
  </dataSource>
  <library id="DB2JCCLib">
    <fileset dir="/path/to/db2.jar"/>
  </library>
</server>

Then, based on which value is set for ENV_LOCATION, either dbconfig-dev.xml or dbconfig-prod.xml will be included in your primary server.xml config.