有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何设置glassfishresource。xml和web。xml?

我有一些麻烦,我真的不明白为什么会发生。 我正在制作一个简单的web服务,它试图转到DB并录制一条记录

我通过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>

我在网络上做出改变。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>

但在测试中,glassfish并没有把我和这个基地联系起来。它使用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) 个答案

  1. # 1 楼答案

    问题是资源类型不正确。您正在以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提供的工具添加这个资源。这一切都很好,但每当你试图在你的网络中添加Resource Reference。xml文件,下拉列表中只显示了有限的选项池,这让我相信这些是唯一有效的选项

    但是,您可以在下拉列表的文本框中手动键入该类。或者您可以转到web的Source视图。xml并手工输入该类

    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>