我决定使用托管EntityManager
的请求范围容器,并且我已经为此创建了一个制作人:
@RequestScoped
public class EntityManagerProducer {
@PersistenceContext(unitName = "PU", type = PersistenceContextType.TRANSACTION)
private EntityManager entityManager;
@Produces
public EntityManager getEntityManager() {
return entityManager;
}
}
我有两个暴露远程客户端视图的EJB:
@Remote
public interface EJB1 {
void createPerson(int id, String firstName, String lastName);
}
@Remote
public interface EJB2 {
void containsEntity(Person person);
}
@Stateless
public class EJB1Impl implements EJB1 {
@Inject
private EntityManager entityManager;
@EJB
private EJB2 ejb2;
@Override
public void createPerson(final int id, final String firstName, final String lastName) {
Person person = new Person();
person.setId(id);
person.setFirstName(firstName);
person.setLastName(lastName);
entityManager.persist(person);
System.out.println("EJB1Impl: persistence context contains entity: " + entityManager.contains(person));
ejb2.containsEntity(person);
}
}
@Stateless
public class EJB2Impl implements EJB2 {
@Inject
private EntityManager entityManager;
@Override
public void containsEntity(final Person person) {
System.out.println("EJB2Impl: PersistenceContext contains entity: " + entityManager.contains(entity));
person.setLastName("new name");
//entityManager.merge(person);
}
}
EJB部署在WildFly 10上。我使用this tutorial通过远程客户端访问它们。当前版本抛出此异常:org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.RequestScoped
。如果我从制作人处删除了@RequestScoped
注释,则我不会获得例外情况,但当被问及是否包含EntityManager
注入第二个EJB
时会返回false
实体,如果我希望对实体所做的更改(更改姓氏)有效,我必须调用entityManager.merge(person)
,这显然意味着实体已分离。我确定第二个EJB
执行相同的事务,因为如果我注入一个EJBContext
并调用setRollbackOnly()
,那么EJB1
中启动的事务将被回滚并且新人未插入数据库。
javax.enterprise.context.RequestScoped
的文档说任何EJB
的任何远程方法调用期间请求范围都是活动的,那么是什么给出了?如何在多个EntityManager
EJB's