簡體   English   中英

如何設置glassfish-resource.xml和web.xml?

[英]How to set up glassfish-resource.xml and web.xml?

我有一些疑難解答,我真的不明白為什么會這樣。 我正在制作簡單的網絡服務,試圖轉到數據庫並獲取1條記錄。

我通過NetBeans向導添加新的服務器資源。 NB為它創建了新的資源和連接池。 看起來像這樣:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
  <jdbc-resource enabled="true" jndi-name="jdbc/testdb" object-type="user" pool-name="testdbPool">
    <description/>
  </jdbc-resource>
  <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="oracle.jdbc.pool.OracleDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="testdbPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
    <property name="URL" value="jdbc:oracle:thin:@193.107.2.38:5555:ora10e"/>
    <property name="User" value=""/>
    <property name="Password" value=""/>
  </jdbc-connection-pool>
</resources>

我在Web.xml中進行了更改。 像這樣:

    <?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <resource-ref>
      <description>DB Connection Pool</description>
      <res-ref-name>jdbc/testdb</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
      <res-sharing-scope>Shareable</res-sharing-scope>
   </resource-ref>

</web-app>

但是在測試玻璃魚沒有把我連接到這個基地。 它使用defaultPool。 這是日志:

WARNING:   RAR5038:Unexpected exception while creating resource for pool DerbyPool. Exception : javax.resource.spi.ResourceAllocationException: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
WARNING:   RAR5117 : Failed to obtain/create connection from connection pool [ DerbyPool ]. Reason : com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
WARNING:   RAR5114 : Error allocating connection : [Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.]
SEVERE:   java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.

我究竟做錯了什么?

問題是資源類型不正確。 您將資源列為javax.sql.DataSource 但是,您實際上使用的是javax.sql.ConnectionPoolDataSource

<resource-ref>
   <description>DB Connection Pool</description>
   <res-ref-name>jdbc/testdb</res-ref-name>
   <res-type>javax.sql.ConnectionPoolDataSource</res-type>
   <res-auth>Container</res-auth>
   <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

嘗試在Glassfish服務器和SQLite DB之間建立數據庫連接時遇到了類似的問題。 我認為問題是圖書館不支持它,但改變<res-type>解決了這個問題。

歸因於這個問題的一件大事是我試圖通過NetBeans提供的工具添加此資源。 這一切都很好,但每當您嘗試在web.xml文件中添加Resource Reference時,您只會在下拉列表中看到有限的選擇池,這使我相信這些是唯一有效的選項。

但是,您可以手動鍵入類到下拉列表的文本框或者你可以去Source web.xml中的觀點和手工輸入類有。

可用的Netbeans資源參考

此外,如果您使用sun-resources.xml文件自動將資源添加到服務器,那么當文件最終有效時,我的文件看起來像這樣(為編輯的屬性添加了省略號):

<resources>
   <jdbc-resource 
      enabled="true" 
      jndi-name="jdbc/rpg" 
      object-type="user" 
      pool-name="RpgPool">
      <description>Description</description>
   </jdbc-resource>
   <jdbc-connection-pool 
      ...
      datasource-classname="org.sqlite.SQLiteConnectionPoolDataSource" 
      ...
      res-type="javax.sql.ConnectionPoolDataSource" 
      wrap-jdbc-objects="false">
      <description>Description</description>
      <property 
         name="URL" 
         value="jdbc:sqlite:C:/some/where/out/there"/>
   </jdbc-connection-pool>
</resources>

暫無
暫無

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

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