0

Please bear with my english and please let me know if my question is not clear or confusing, I will try to rephrase it :)

I have multiple Tomcat running with different data source like below in server.xml:

<GlobalNamingResources>
    <Resource
        auth="Container"
        driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
        logAbandoned="true"
        maxActive="20"
        maxIdle="10"
        maxWait="5000"
        name="jdbc/datasource1"
        removeAbandoned="true"
        removeAbandonedTimeout="60"
        type="javax.sql.DataSource"
        url="jdbc:sqlserver://localhost;databaseName=database1"
        username="username"
        password="password"/>
</GlobalNamingResources>

I would like to create a webapp which I can simply deploy to each Tomcat and test out all the data source configured in server.xml by triggering a servlet.

By saying test out the data source, I mean execute below snippet for each data source:

try {
    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    DataSource ds = (DataSource) envCtx.lookup("datasource1");
    ds.getConnection(); // If this line doesn't throw SQLException, the db connection is established successfully
} catch(NamingException ex) {
    System.out.println("JNDI lookup failed: " + ex.getMessage());
} catch(SQLException se) {
    System.out.println("Unable to establish connection: " + se.getMessage());
}

Problem

I can't find a way to retrieve all data source from server.xml without keeping the "datasource1" in my context.xml, for example:

<ResourceLink global="jdbc/datasource1" name="datasource1" type="javax.sql.DataSource"/>

So my questino is that, is there a way to read all data source configured in server.xml without having the context.xml file ?

Thanks in advance.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
ann20502
  • 1
  • 2

1 Answers1

0

Your problem is not more than reading XML File from

$CATALINA_BASE/conf/server.xml

Enumerate all Datasource configurations from XML by What is the best/simplest way to read in an XML file in Java application?

For every connection information you read, Do the Connection Test.

As you have multiple Tomcat running , you will need to mention the Tomcat Configuration Server xml path in a different xml and reuse in future.

Sundar Rajan
  • 556
  • 4
  • 25