Glassfish 4.1.1创建EntityManagerFactory时数据源获取失败求助
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.xmlis located in the correct directory:- For Maven/Gradle projects:
src/main/resources/META-INF/ - For standard web apps:
WEB-INF/classes/META-INF/
- For Maven/Gradle projects:
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
- Open
http://localhost:4848(default port) and log in. - Navigate to
Resources > JDBC > JDBC Data Sources. - 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.
- Open
- Option 2: Use
glassfish-resources.xml(Application-Level Data Source)
If you want the data source bundled with your app, create aglassfish-resources.xmlfile inWEB-INF/orMETA-INF/with this structure:
Note: Replace the database details with your actual credentials, and make sure your database driver (like MySQL Connector/J) is either in GlassFish's<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>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) orbuild.gradle(Gradle) so it's packaged with your WAR/EAR.
- For global use: Copy the driver JAR to
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.xmlorglassfish-resources.xml
Start with these checks, and you should be able to resolve the issue quickly.
内容的提问来源于stack exchange,提问作者Bashir

