我有一些疑难解答,我真的不明白为什么会这样.我正在制作简单的网络服务,试图转到数据库并获取1条记录.
我通过NetBeans向导添加新的服务器资源.NB为它创建了新的资源和连接池.看起来像这样:
我在Web.xml中进行了更改.像这样:
30 DB Connection Pool jdbc/testdb javax.sql.DataSource Container Shareable
但是在测试玻璃鱼没有把我连接到这个基地.它使用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提供的工具添加此资源.这一切都很好,但是每当你尝试Resource Reference
在你的web.xml文件中添加一个,你只会在下拉列表中看到有限的选择池,这让我相信这些是唯一有效的选项.
但是,您可以手动键入类到下拉列表的文本框或者你可以去Source
web.xml中的观点和手工输入类有.
此外,如果您使用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>