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

JPA EntityManager无法查询实体,恢复旧代码仍无效求助

Troubleshooting Your Broken EntityManager in Glassfish/Jersey/MySQL Setup

Hey there, let's break down why your once-working EntityManager is now throwing errors in that Jersey endpoint. That truncated java.lang.Ille... error is a hint that something's off with the EM's state, access method, or underlying configuration—let's walk through the most likely fixes:

1. Stop manually fetching EntityManager from the context

Your current approach of grabbing the EM via context.getAttribute("em") is error-prone. The standard Java EE way to handle this (and far more reliable) is using dependency injection with @PersistenceContext:

// Inject the EntityManager directly instead of pulling from context
@PersistenceContext(unitName = "YourPersistenceUnitName")
private EntityManager em;

@GET @Path("movie/{id}") @JSONP @Produces({"application/javascript", MediaType.APPLICATION_JSON})
public Movie getMovie(@PathParam("id") int id) {
    Movie requestedMovie = em.find(Movie.class, id);
    return requestedMovie;
}
  • Double-check that YourPersistenceUnitName matches exactly what's defined in your persistence.xml file.
  • This lets Glassfish handle the EM's lifecycle (opening, closing, managing transactions) automatically, avoiding issues with stale or closed EM instances.

2. Verify your EntityManager isn't in an invalid state

If you're still set on manual EM management, confirm the instance you're pulling from the context isn't already closed. Common scenarios:

  • A previous part of your code calls em.close() but doesn't refresh the context attribute with a new, open EM.
  • The EM was never properly initialized at app startup, so the context holds a null or uninitialized reference.

3. Check transaction and persistence configuration

  • Transaction context: For read operations like find(), Glassfish usually handles transactions automatically, but if your setup is misconfigured, adding @Transactional to your method might resolve issues with inactive transaction contexts.
  • Persistence.xml validity: Ensure your persistence unit points to the correct MySQL JDBC datasource, with valid credentials and connection details. If the datasource itself is broken (e.g., MySQL is down, or Glassfish's JDBC pool is misconfigured), the EM can't function.
  • Glassfish datasource check: Log into your Glassfish admin console, navigate to JDBC Connection Pools, and run a "Ping" test on your MySQL pool. If this fails, fix the pool connection first—it's almost certainly the root cause.

4. Rule out recent configuration changes

Did you update Glassfish/Jersey versions, modify web.xml, or tweak persistence settings lately? Even small changes (like switching Jersey entity providers) can break EM integration. Roll back recent changes temporarily to see if the issue resolves.

One last thing: Share the full error stack trace if you can—the truncated java.lang.Ille... only tells half the story. A full error (like IllegalStateException or IllegalArgumentException) will pinpoint exactly whether the EM is closed, uninitialized, or facing a configuration mismatch.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:33:04