资源的 JNDI 查找失败

JNDI lookup failed for the resource

我想编写一些简单的 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>

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

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 资源,如下图所示

我收到 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.

好吧,你可能会说,"use an IDE like Netbeans",但我正在努力学习基础知识,所以现在还不行。

我忘了启动数据库:

asadmin start-database

参见:

Can't deploy my project on Glassfish