gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:02:08 28 4
gpt4 key购买 nike

我有一些疑难解答,但我真的不明白为什么会这样。我正在制作简单的网络服务,它会尝试转到数据库并获取 1 条记录。

我通过 NetBeans 向导添加新的服务器资源。注意为其创建了新的资源和连接池。看起来像这样:

<?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>

但是在测试中,glassfish 没有将我连接到这个基地。它使用默认池。这是日志:

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 数据库之间建立数据库连接时遇到了类似的问题。我认为问题是图书馆不支持它,而是改变了 <res-type>解决了问题。

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

但是,您可以在下拉列表的文本框中手动输入类别或者您可以转到Source查看 web.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>

关于java - 如何设置glassfish-resource.xml 和web.xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21426128/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com