Search code examples
javawebsphere-libertyjsr352open-liberty

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


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.


Solution

  • 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