测试server.xml中配置的所有数据源

时间:2018-02-22 10:07:06

标签: java tomcat

请承担我的英语,如果我的问题不清楚或令人困惑,请告诉我,我会尝试改写它:)

我在server.xml中运行了多个不同数据源的Tomcat,如下所示:

<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>

我想创建一个webapp,我可以简单地将其部署到每个Tomcat,并通过触发servlet来测试server.xml中配置的所有数据源。

通过说出测试数据源,我的意思是为每个数据源执行以下代码段:

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());
}

问题

我无法找到一种从server.xml检索所有数据源的方法,而无需保留&#34; datasource1&#34;在我的context.xml中,例如:

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

所以我的questino是,有没有办法在没有context.xml文件的情况下读取server.xml中配置的所有数据源?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您的问题不仅仅是从

读取XML文件
$CATALINA_BASE/conf/server.xml

通过What is the best/simplest way to read in an XML file in Java application?

从XML枚举所有数据源配置

对于您阅读的每个连接信息,请进行连接测试。

当您运行多个Tomcat时,您需要在不同的xml中提及Tomcat Configuration Server xml路径并在将来重用。

相关问题