Get custom database settings from config.xml

General questions regarding the development with JVx.

Get custom database settings from config.xml

Postby jvxdev » Mon Mar 07, 2016 2:50 pm

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?
jvxdev
 
Posts: 19
Joined: Fri Mar 04, 2016 12:40 pm

Re: Get custom database settings from config.xml

Postby Support@SIB » Mon Mar 07, 2016 3:16 pm

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
User avatar
Support@SIB
 
Posts: 201
Joined: Mon Sep 28, 2009 1:56 pm

Re: Get custom database settings from config.xml

Postby Clopton » Wed Oct 11, 2017 7:31 pm

This worked like a charm. Can't believe I didn't think of that. Thanks a lot.
Clopton
 
Posts: 1
Joined: Tue Oct 03, 2017 4:16 pm


Return to Development