3

我正在使用 JBoss 6.1 Final,并在我的 Web 应用程序运行一段时间后收到以下错误消息(注意应用程序不会崩溃),然后是很长的堆栈跟踪。我注意到只有在有状态会话 bean 中注入了其他有状态会话 bean 时才会出现此问题。

16:10:59,769 错误 [org.jboss.ejb3.cache.simple.SimpleStatefulCache.UutSerialNumberServiceBean] 问题钝化线程:javax.ejb.EJBException:无法钝化;未能保存状态

这是有问题的有状态会话 bean...

package com.ray.TEI.ejb;

import java.io.Serializable;

import javax.ejb.Stateful;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import com.ray.TEI.model.TestProcedure;
import com.ray.TEI.model.Uut;
import com.ray.TEI.model.UutSerialNumber;

@Stateful
public class UutSerialNumberServiceBean implements Serializable {
  private static final long serialVersionUID = 1L;
  @PersistenceContext(unitName="jasoni", type=PersistenceContextType.EXTENDED)
  protected EntityManager em;
  @Inject private ExecProcedureServiceBean execProcedureServiceBean;

  public boolean isDuplicateSerialNumber(Uut uut, String serialNumber) {
    return ((Number)em.createNamedQuery("UutSerialNumber.getCountByUutIdAndSerialNumber")
             .setParameter("uut", uut)
             .setParameter("serialNumber", serialNumber)
             .getSingleResult()).intValue() > 0;
  }

  public UutSerialNumber findUutSerialNumberByUutSerialNumberId(Integer uutSerialNumberId) {
    return em.find(UutSerialNumber.class, uutSerialNumberId);
  }

  public UutSerialNumber editSerialNumber(Integer uutSerialNumberId, String serialNumber) {
    UutSerialNumber uutSerialNumber = findUutSerialNumberByUutSerialNumberId(uutSerialNumberId);
    uutSerialNumber.setSerialNumber(serialNumber);
    return uutSerialNumber;
  }

  public UutSerialNumber createSerialNumber(Uut uut, String serialNumber) {
    UutSerialNumber uutSerialNumber = new UutSerialNumber();
    uutSerialNumber.setSerialNumber(serialNumber);
    uutSerialNumber.setUut(uut);
    uut.getSerialNumbers().add(uutSerialNumber);
    em.persist(uutSerialNumber);
    for (TestProcedure testProcedure : uut.getTestProcedures()) {
      execProcedureServiceBean.createExecProcedure(uutSerialNumber, testProcedure);
    }
    return uutSerialNumber;
  }
}

这是注入的有状态会话 bean...

package com.ray.TEI.ejb;

import java.io.Serializable;

import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import com.ray.TEI.model.ExecProcedure;
import com.ray.TEI.model.TestProcedure;
import com.ray.TEI.model.UutSerialNumber;


@Stateful
public class ExecProcedureServiceBean implements Serializable {
  private static final long serialVersionUID = 1L;
  @PersistenceContext(unitName="jasoni", type=PersistenceContextType.EXTENDED)
  protected EntityManager em;

  public ExecProcedure createExecProcedure(UutSerialNumber uutSerialNumber, TestProcedure testProcedure) {
    ExecProcedure execProcedure = new ExecProcedure();
    execProcedure.setUutSerialNumber(uutSerialNumber);
    execProcedure.setTestProcedure(testProcedure);
    execProcedure.setIterationCount(0);
    em.persist(execProcedure);
    return execProcedure;
  }
}

有人知道这有什么问题吗?

谢谢,杰森

4

1 回答 1

2

EntityManager 不可序列化,我想这就是它失败的原因。

从这里有几个选择:

  • 停用钝化(如果您不需要该机制,最简单的选择)
  • 从 SFSB 中删除持久性上下文(这很可能会杀死您的扩展模式)
  • 用可序列化的 Hibernate-Session 替换 EntityManager (考虑到实验性,我自己从未尝试过)
于 2012-04-19T11:23:40.223 回答