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

时间:2014-01-29 08:49:19

标签: java xml oracle netbeans glassfish

我有一些疑难解答,我真的不明白为什么会这样。 我正在制作简单的网络服务,试图转到数据库并获取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.

我做错了什么?

1 个答案:

答案 0 :(得分:8)

问题是资源类型不正确。您将资源列为 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数据库之间建立数据库连接时遇到了类似的问题。我认为问题在于图书馆并不支持它,但更改<res-type>解决了这个问题。

归因于此问题的一件大事是我尝试通过NetBeans提供的工具添加此资源。这一切都很好,但每当您尝试在web.xml文件中添加Resource Reference时,您只会在下拉列表中看到有限的选择池,这让我相信那些是只有有效的选择。

但是,您可以在下拉列表 OR 的文本框中手动输入类,您可以转到web.xml的Source视图并手动输入课程

Available Netbeans Resource References

此外,如果您使用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>