Page 1 of 1

Get custom database settings from config.xml

PostPosted: Mon Mar 07, 2016 2:50 pm
by jvxdev
My config.xml contains multiple datasources:

Code: Select all
<application>
  <datasource>
    <db name="default">
      <url>jdbc:oracle:thin:@first.world</url>
      <username>user1</username>
      <password>pass1</password>
    </db>
    <db name="external">
      <url>jdbc:oracle:thin:@second.world</url>
      <username>user2</username>
      <password>pass2</password>
    </db>
  </datasource>
</application>


I tried to call:

Code: Select all
SessionContext.getCurrentSessionConfig().getProperty("/application/datasource/db")

but got an Exception:

Code: Select all
java.lang.IllegalArgumentException: Missing index


I need the "external" datasource. Do I need to parse the file with an XML parser?

Re: Get custom database settings from config.xml

PostPosted: Mon Mar 07, 2016 3:16 pm
by Support@SIB
The solution is super simple:

Code: Select all
String url = SessionContext.getCurrentSessionConfig().getProperty(
                                                "/application/datasource/db(1)/url");

or

Code: Select all
XmlNode node = SessionContext.getCurrentSessionConfig().getNode(
                                               "/application/datasource/db(1)");

But it's better to use DBCredentials:

Code: Select all
DBCredentials cred = DataSourceHandler.createDBCredentials(
                           SessionContext.getCurrentSessionConfig(), "external");


JVx has a smart class for reading custom XML files, see XmlWorker and XmlNode

Re: Get custom database settings from config.xml

PostPosted: Wed Oct 11, 2017 7:31 pm
by Clopton
This worked like a charm. Can't believe I didn't think of that. Thanks a lot.