如何在Server.xml中添加两个或多个资源

时间:2016-08-23 14:53:47

标签: java mysql xml tomcat server

我正在尝试在Server.xml文件中添加两个资源配置 名称为test1和test2。

<GlobalNamingResources>
<Resource auth="Container"
        name="jdbc/test1"
        driverClassName="com.mysql.jdbc.Driver"
        type="javax.sql.DataSource"
        url="xxxxxx"
        username="xxxxx"
        password="xxxxx"
        validationQuery="SELECT 1"
        initialSize="40"
        maxActive="-2"
        maxIdle="40"
        maxWait="10000"
        minEvictableIdleTimeMillis="25"
        removeAbandoned="true"
        removeAbandonedTimeout="120"
        testOnBorrow="true"
        testWhileIdle="true"
        timeBetweenEvictionRunsMillis="1800000"
        maxHttpHeaderSize="8192"
        maxThreads="150"
        minSpareThreads="25"
        maxSpareThreads="75"
        enableLookups="false"
        acceptCount="100"
        disableUploadTimeout="true"
        useBodyEncodingForURI="true"
        compression="on"
        compressionMinSize="1024"
        noCompressionUserAgents="gozilla, traviata"
        factory="xxxxxxxx" />

<Resource auth="Container"
        name="jdbc/test2"
        driverClassName="com.mysql.jdbc.Driver"
        type="javax.sql.DataSource"
        url="yyyyyy"
        username="yyyyyy"
        password="yyyyyyyyy"
        validationQuery="SELECT 1"
        initialSize="40"
        maxActive="-2"
        maxIdle="40"
        maxWait="10000"
        minEvictableIdleTimeMillis="25"
        removeAbandoned="true"
        removeAbandonedTimeout="120"
        testOnBorrow="true"
        testWhileIdle="true"
        timeBetweenEvictionRunsMillis="1800000"
        maxHttpHeaderSize="8192"
        maxThreads="150"
        minSpareThreads="25"
        maxSpareThreads="75"
        enableLookups="false"
        acceptCount="100"
        disableUploadTimeout="true"
        useBodyEncodingForURI="true"
        compression="on"
        compressionMinSize="1024"
        noCompressionUserAgents="gozilla, traviata"
        factory="xxxxxxxx" />
  </GlobalNamingResources>

但是当我尝试获取上下文时

Context initContext = new InitialContext();
            Context envContext = (Context) initContext.lookup("java:/comp/env");
            DataSource dataSource = (DataSource) envContext.lookup("jdbc/test2");

我收到以下错误

javax.naming.NameNotFoundException: Name [test2] is not bound in this Context. Unable to find [test2].

我不确定我是否正确。任何人都可以帮我在server.xml文件中添加两个资源,所以我应该能够连接到两个不同的数据库。 谢谢!

1 个答案:

答案 0 :(得分:0)

在tomcat中使用多个资源时遇到同样的问题。

也许这是tomcat容器中的错误。

替代解决方案。

我在应用程序管理连接中定义了第二个资源。

Hibernate Config Snippet: &gt;属性名称=&#34; connection.driver_class&#34;&gt; com.mysql.jdbc.Driver&lt; / property&gt; &gt;属性名称=&#34; connection.url&#34;&gt; jdbc:mysql:// localhost:3306 / test&lt; / property&gt; &gt;属性名称=&#34; connection.username&#34;&gt;用户