资源的JNDI查找失败

时间:2018-07-09 21:18:51

标签: jpa glassfish persistence.xml

我想对一些简单的JPA测试进行编程。我的项目结构是

├── pom.xml
└── src
    └── main
        ├── java
        │   └── guest
        │       ├── GuestDao.java
        │       ├── Guest.java
        │       ├── GuestRest.java
        │       └── Rest.java
        └── resources
            └── META-INF
                └── persistence.xml

persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
    version="2.2">
<persistence-unit name="guestDB" transaction-type="JTA">
    <jta-data-source>jdbc/guestDB</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
        <property name="eclipselink.logging.level" value="FINE" />
    </properties>

</persistence-unit>
</persistence>

但是,如果我使用asadmin deploy target/joe.war部署在glassfish 5上,则会得到

remote failure: Error occurred during deployment: 
Exception while deploying the app [joe] : 
JNDI lookup failed for the resource: Name: [guestDB], Lookup: [jdbc/guestDB], Type: [javax.sql.DataSource]. 
Please see server.log for more details.
Command deploy failed.

服务器日志告诉我:

...
Caused by: javax.naming.NameNotFoundException: guestDB not found
...

但我认为,由于

<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />

persistence.xml中,我不必创建数据库或任何连接池。

但是,如果我创建JDBC资源,如下图所示

enter image description here

我收到connection refused错误

remote failure: Error occurred during deployment: Exception while preparing the app : Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException: Fehler beim Herstellen der Verbindung zu Server localhost auf Port 1.527 mit Meldung Verbindungsaufbau abgelehnt (Connection refused).
Error Code: 0. Please see server.log for more details.

好的,您可能会说,“使用像Netbeans这样的IDE”,但是我正在尝试了解基础知识,因此暂时不可以。

1 个答案:

答案 0 :(得分:0)

我忘记了启动数据库:

asadmin start-database

请参阅:

Can't deploy my project on Glassfish

Derby Pool ping fails with java.net.ConnectException in Glassfish