Servlet 异常 +类转换异常 +玻璃鱼 + Netbeans + JPA 实体 +瓦丁

发布于 2024-08-13 08:49:12 字数 525 浏览 8 评论 0原文

我收到此错误:

StandardWrapperValve[Vaadin Servlet]:PWC1406:servlet Vaadin Servlet 的 Servlet.service() 抛出异常 com.delhi.entities.Category 无法转换为 com.delhi.entities.Category

java.lang.ClassCastException:当我尝试在 glassfish v2 上运行 web 应用程序时,

。类别是一个 JPA 实体对象,

根据服务器日志,有问题的代码是:

for (Category c : categories) {
          mymethod();
   }

类别源自:

List<Category> categories = q.getResultList();

知道出了什么问题吗?

I get this error:

StandardWrapperValve[Vaadin Servlet]: PWC1406: Servlet.service() for servlet Vaadin Servlet threw exception
java.lang.ClassCastException: com.delhi.entities.Category cannot be cast to com.delhi.entities.Category

when I try to run my webapps on glassfish v2.

Category is a JPA entity object

the offending code according to the server log is:

for (Category c : categories) {
          mymethod();
   }

categories is derived from:

List<Category> categories = q.getResultList();

Any idea what went wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

诗笺 2024-08-20 08:49:12

这是一个类加载器问题。如果一个类由不同的类加载器加载,则它的对象不能互相分配。您可能已将一个对象从一个 WAR 传递到另一个 WAR 中。有几个选项可以解决这个问题:

  • 将所有代码放入一个 WAR 中。
  • 在 WAR 之间使用某种形式的远程处理。序列化解决了类加载器问题。
  • 尝试将所有 WAR 放入一个 EAR 中。如果这不起作用,请将所有代码放入 MANIFEST.MF 中 EAR 类路径上的 JAR 中。

This is a class loader issue. If a class is loaded by different class loaders, it's objects cannot be assigned to each other. You have probably passed an object from one WAR into another one. There are several options to resolve this:

  • Put all your code into a single WAR.
  • Use some form of remoting between your WARs. Serialization takes care of the class loader problem.
  • Try putting all you WARs into a single EAR. If that doesn't work, put all code into JARs that are on the EAR's Classpath in the MANIFEST.MF.
醉殇 2024-08-20 08:49:12

我曾经遇到过同样的问题,我的环境如下:

  • 我有 Glassfish v4
  • Netbeans 和以下项目
    • 包含实体的网页战争项目
    • 以及带有该网页战争项目的 Ear 项目

问题​​是,在战争的项目设置中,我检查了[x]运行>保存时部署。这导致每次我点击保存时都会部署战争项目。它有时会导致 PermGen(内存)问题并且无法正确部署 EAR(因为例如在取消部署和部署 EAR 之间 - 这个“疯狂”的 Netbeans 正在部署这场战争)。

解决方案:如果 Netbeans &&使用 EAR,然后在项目属性中取消选中保存时部署。

编辑:

看来这个错误与

SEVERE:   The web application [/faces] created a ThreadLocal with key of type [org.glassfish.pfl.dynamic.codegen.impl.CurrentClassLoader$1] (value [org.glassfish.pfl.dynamic.codegen.impl.CurrentClassLoader$1@249ea63a]) and a value of type [org.glassfish.web.loader.WebappClassLoader] (value [WebappClassLoader (delegate=true; repositories=WEB-INF/classes/)]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

I once had the same problem and the environment I had was following:

  • I had Glassfish v4
  • Netbeans with following projects
    • webpage war project containing entities
    • and ear project with that webpage war project

The problem was that in war's project settings I had checked [x] Run>Deploy on save. This was causing deploying war project everyime I hit save. It was sometimes leading to PermGen (memory) problems and unability to deploy EAR correctly (because e.g. in between undeploying and deploying EAR - this "crazy" Netbeans was deploying this war).

Solution: If Netbeans && using EAR, then uncheck deploy on save in project properties.

EDIT:

it seems that this error is connected with

SEVERE:   The web application [/faces] created a ThreadLocal with key of type [org.glassfish.pfl.dynamic.codegen.impl.CurrentClassLoader$1] (value [org.glassfish.pfl.dynamic.codegen.impl.CurrentClassLoader$1@249ea63a]) and a value of type [org.glassfish.web.loader.WebappClassLoader] (value [WebappClassLoader (delegate=true; repositories=WEB-INF/classes/)]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
画▽骨i 2024-08-20 08:49:12

我今天也遇到了同样的问题。解决方案是在使用后关闭EntityManagerFactory。

这个答案帮助了我:
https://stackoverflow.com/a/13823219/2455506

I've had same problem today. Solution was closing EntityManagerFactory after use.

This answer helped me:
https://stackoverflow.com/a/13823219/2455506

野却迷人 2024-08-20 08:49:12

我在使用 Glassfish v2 和 Glassfish v3 时也遇到了这个问题。

我可以问您一个问题:您是否在部署应用程序时尝试初始化任何持久性对象(通过启动时加载的 servlet 或上下文侦听器)?

就像 bguiz,我注意到这个问题只发生在重新部署时。新部署到刚重新启动的 Glassfish 服务器上,永远不会出现此问题。

就像 FelixM 提到的,我'我确信这是一个类加载器问题,但我不认为这是多次战争的问题(我只在我的服务器上部署了 1 个)。在 Glassfish 3 中,我可以看到我的 WAR 使用 2 个 Glassfish“引擎”。一种用于网络(war),一种用于 jpa。据我了解,这些是不同的容器,每个容器都有自己的类加载器。我猜 Glassfish v2 也以同样的方式工作。

我正在使用 Spring 并在(重新)部署时(重新)初始化一些持久性对象。我的想法是,当 Web 引擎重新初始化战争时,jpa 引擎仍在使用旧的类定义。通常,如果我在初次失败后重试重新部署,它可能会成功(有时可能需要多次重试,但最终我可以在不重新启动的情况下使其成功 - Glassfish v3 比 v2 取得更好的成功)。

此时,我认为这两个类加载器不同步,或者重新部署时存在某种竞争条件,从而允许此操作有时成功。我尝试强制类加载器,编写这样的代码

HashMap<Object, Object> properties = new HashMap<Object, Object>();
properties.put(PersistenceUnitProperties.CLASSLOADER, this.getClass().getClassLoader());
entityManagerFactory = Persistence.createEntityManagerFactory(jpaContext, properties);

,但似乎没有任何影响。

我还想知道消除启动时的初始化是否可以解决问题,让应用服务器有时间在使用任何 jpa 类之前重新同步两个引擎(这就是我问后续问题的原因)。

I'm experiencing this problem too with Glassfish v2 and Glassfish v3.

Can I ask you a question: Are you attempting to initialize any persistence object when the application is deployed (through a servlet loaded on startup or a context listener)?

Like bguiz, I've noticed this problem only happens on redeploy. A new deploy to a freshly restarted Glassfish server, never has this problem.

Like FelixM mentioned, I'm convinced this is a class loader issue, however I don't believe it's an issue with multiple wars (I only have 1 deployed to my server). In Glassfish 3, I can see that my WAR is utilizing 2 Glassfish "engines". One for the web(war) and one for the jpa. From what I understand, these are different containers each with their own classloader. I'm guessing Glassfish v2 works in the same manner.

I'm using Spring and (re)initialize some persistence objects on (re)deploy. What I'm thinking, is that while the web engine is reinitializing the war, the jpa engine is still using the old class definitions. Often if I retry the redeploy after this initial failure, it may succeed (sometimes it may take more than one retry but eventually I can get it to succeed without a restart - having better success with Glassfish v3 than v2).

At this point I'm thinking that either these two classloaders are out of sync or there is some sort of race condition on redeploy allowing this operation to sometimes succeed. I've tried to force the classloader, writing code like this

HashMap<Object, Object> properties = new HashMap<Object, Object>();
properties.put(PersistenceUnitProperties.CLASSLOADER, this.getClass().getClassLoader());
entityManagerFactory = Persistence.createEntityManagerFactory(jpaContext, properties);

but it didn't seem to have any affect.

I'm also wondering if eliminating the initialization at startup could fix the problem, giving the appserver time to resynchronize both engines before using any jpa classes (which is why I asked my follow up question).

一梦等七年七年为一梦 2024-08-20 08:49:12

我的观察是,只有在使用热重新部署或静态重新部署时才会发生这种情况。当然,这仅适用于当您收到类转换异常且其中 to 和 from 类相同时。

解决方法:

  • 不要使用取消部署和部署,而不是重新部署
  • 重新启动应用程序服务器
  • 删除受影响类的静态成员
  • 使用远程接口(序列化使这种情况消失)

IMO我认为类加载器无法重新加载类并且旧版本是重复使用,导致错误。


本文没有直接讨论此错误,但它是很好的背景信息类加载器如何工作。

My observation is that it only happens when using a hot redeploy or a static redeploy. This only applies, of course, if you get a class cast exception where both the to and from classes are the same.

Workarounds:

  • Don't use undeploy and deploy instead of redeploy
  • Restart app server
  • Remove static members of the affected classes
  • Use a remote interface (serialization makes this go away)

IMO I think the class loader was unable to reload the class and the old version was reused, resulting in the error.


This article doesn't talk about this error directly, but it is good background info on how the class loader works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文