You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Glassfish 4.1.1创建EntityManagerFactory时数据源获取失败求助

Fixing the "Cannot acquire data source [java:app/quiz]" Error in GlassFish 4.1.1 + JPA

Hey there! Let's work through this JPA issue you're hitting with GlassFish 4.1.1. That javax.persistence.PersistenceException tied to the missing java:app/quiz data source is super common when you're getting started, so let's break down the fixes step by step.

1. Double-Check Your persistence.xml Configuration

First, make sure your persistence unit is pointing to the correct data source, and the file is in the right place:

  • Verify the data source name in your <persistence-unit> block matches exactly (case-sensitive!):
    <persistence-unit name="YourQuizPU" transaction-type="JTA">
        <jta-data-source>java:app/quiz</jta-data-source>
        <!-- Other configs like class mappings go here -->
    </persistence-unit>
    
  • Ensure persistence.xml is located in the correct directory:
    • For Maven/Gradle projects: src/main/resources/META-INF/
    • For standard web apps: WEB-INF/classes/META-INF/

2. Validate the Data Source Setup in GlassFish

The error says GlassFish can't find java:app/quiz, so let's confirm it exists:

  • Option 1: Use the GlassFish Admin Console
    1. Open http://localhost:4848 (default port) and log in.
    2. Navigate to Resources > JDBC > JDBC Data Sources.
    3. Look for a data source with the JNDI name java:app/quiz. If it's missing, you'll need to create it:
      • Create a JDBC Connection Pool first (make sure it can connect to your database—test the pool to confirm).
      • Then create a JDBC Resource pointing to that pool, with the exact JNDI name java:app/quiz.
  • Option 2: Use glassfish-resources.xml (Application-Level Data Source)
    If you want the data source bundled with your app, create a glassfish-resources.xml file in WEB-INF/ or META-INF/ with this structure:
    <resources>
        <jdbc-resource jndi-name="java:app/quiz" pool-name="QuizDBPool"/>
        <jdbc-connection-pool name="QuizDBPool" res-type="javax.sql.DataSource"
                              datasource-classname="com.mysql.cj.jdbc.MysqlDataSource">
            <property name="URL" value="jdbc:mysql://localhost:3306/your_quiz_db"/>
            <property name="user" value="your_db_user"/>
            <property name="password" value="your_db_password"/>
        </jdbc-connection-pool>
    </resources>
    
    Note: Replace the database details with your actual credentials, and make sure your database driver (like MySQL Connector/J) is either in GlassFish's glassfish4/glassfish/lib/ directory or bundled with your app.

3. Verify Connection Pool & Driver Availability

  • Check if your JDBC Connection Pool is in a "Running" state in the GlassFish console. If it's stopped, the data source won't be accessible.
  • Ensure the database driver is properly installed:
    • For global use: Copy the driver JAR to glassfish4/glassfish/lib/ and restart GlassFish.
    • For app-specific use: Add the driver as a dependency in your pom.xml (Maven) or build.gradle (Gradle) so it's packaged with your WAR/EAR.

4. Confirm EntityManagerFactory Creation Code

Make sure you're using the correct persistence unit name when creating the factory:

// Replace "YourQuizPU" with the exact name from your persistence.xml
EntityManagerFactory emf = Persistence.createEntityManagerFactory("YourQuizPU");

A typo here can lead to GlassFish looking for the wrong data source configuration.

Quick Recap

Most of the time, this error comes down to:

  • A typo in the JNDI data source name (remember, it's case-sensitive!)
  • A missing/unconfigured data source in GlassFish
  • A missing database driver
  • A misplaced persistence.xml or glassfish-resources.xml

Start with these checks, and you should be able to resolve the issue quickly.

内容的提问来源于stack exchange,提问作者Bashir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:34:03