簡體   English   中英

如何在Server.xml中添加兩個或更多資源

[英]How to add two or more resource in Server.xml

我試圖在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文件中添加兩個資源,所以我應該能夠連接到兩個不同的數據庫。 謝謝!

在tomcat中使用多個資源時,我遇到相同的問題。

也許這是tomcat容器中的錯誤。

替代解決方案。

我在應用程序管理連接中定義了第二個資源。

Hibernate Config片段:>屬性名稱=“ connection.driver_class”> com.mysql.jdbc.Driver </ property>>屬性名稱=“ connection.url”> jdbc:mysql:// localhost:3306 / test </ property> >屬性名稱=“ connection.username”>用戶

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM