GlassFish 5 JNDI查找失败求助:部署Web应用连接MySQL遇异常
Hey there! Let's work through this JNDI lookup issue together—this is a super common snag when setting up database connections in GlassFish, so you're not alone here.
First, let's break down the error: your app is trying to find the datasource at java:app/jdbc_blogdb for your persistence unit appnamePU, but GlassFish can't locate it. Here are the key steps to fix this:
1. Verify MySQL JDBC Driver is Properly Deployed
GlassFish needs the MySQL driver to recognize the datasource. Do this:
- Grab the MySQL JDBC driver JAR (e.g.,
mysql-connector-java-8.0.30.jar—match it to your MySQL version) - Copy it to
glassfish5/glassfish/domains/domain1/lib/ext - Restart GlassFish completely (don't just reload the app; stop and start the server)
If the driver isn't in this folder, GlassFish can't initialize the connection pool, which directly causes the JNDI lookup failure.
2. Validate Your JDBC Connection Pool Configuration
Head to the GlassFish admin console (default: http://localhost:4848) and check these:
- Go to Resources > JDBC > JDBC Connection Pools and select your pool
- Click the Ping button—if this fails, your pool is misconfigured:
- Ensure the Datasource Classname is correct:
- MySQL 8+:
com.mysql.cj.jdbc.MysqlDataSource - Older MySQL versions:
com.mysql.jdbc.jdbc2.optional.MysqlDataSource
- MySQL 8+:
- Check Additional Properties:
serverName: Should belocalhost(or your DB host)databaseName: Must match your MySQL database name (blogdbin your case)user/password: Correct credentials for your MySQL userurl(if used): Format should bejdbc:mysql://localhost:3306/blogdb?useSSL=false&serverTimezone=UTC(adjust SSL and timezone as needed)
- Ensure the Datasource Classname is correct:
- Click the Ping button—if this fails, your pool is misconfigured:
3. Check JDBC Resource JNDI Mapping
Next, go to Resources > JDBC > JDBC Resources:
- Confirm the JNDI name of your resource aligns with what your app is looking for. Your error says the lookup is for
java:app/jdbc_blogdb, so:- If you created a global JDBC resource, its JNDI name should be
jdbc/jdbc_blogdb, and your app would reference it asjava:global/jdbc/jdbc_blogdb - If you're using an application-scoped resource (bundled with your app), ensure the JNDI name is exactly
jdbc_blogdbso it maps tojava:app/jdbc_blogdb
- If you created a global JDBC resource, its JNDI name should be
4. Fix Your persistence.xml Configuration
Open your project's persistence.xml (usually in src/main/resources/META-INF/ or WEB-INF/classes/META-INF/):
- Make sure the
jta-data-sourcevalue matches the full JNDI path GlassFish expects. For example:<persistence-unit name="appnamePU" transaction-type="JTA"> <jta-data-source>java:app/jdbc_blogdb</jta-data-source> <!-- Other configs --> </persistence-unit>
Double-check that the persistence unit name (appnamePU) matches exactly what's referenced in your code.
5. Add Resource Reference in web.xml (If Needed)
For web apps, adding a resource reference helps GlassFish map the JNDI name correctly. Add this to your web.xml:
<resource-ref> <description>MySQL Database Connection</description> <res-ref-name>jdbc/jdbc_blogdb</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
Adjust the res-ref-name to match your JDBC resource's JNDI name.
Final Step: Test Again
After making these changes:
- Restart GlassFish fully
- Ping the JDBC connection pool again to confirm it works
- Deploy your web app once more
This should resolve the JNDI lookup failure. If you're still stuck, check the GlassFish server logs (glassfish5/glassfish/domains/domain1/logs/server.log) for more detailed error messages—they often reveal exactly what's broken.
内容的提问来源于stack exchange,提问作者Marcin

