我只是测试从Seam3迁移到DeltaSpike,如果bean中只有一个EntityManager,一切正常,但如果添加其他EntityManager(其他数据源)则会出错:
JBAS010152:应用程序错误:状态为0的请求中的事务仍处于活动状态
错误项目:
https://github.com/yuanqixun/hellodeltaspike
运行此项目环境:
EntityManagerProducer代码:
@ApplicationScoped
public class EntityManagerProducer {
@PersistenceUnit(unitName = "hellodeltaspike")
EntityManagerFactory emf;
@PersistenceUnit(unitName = "hellodeltaspike2")
EntityManagerFactory mysqlemf;
@Produces
@ConversationScoped
EntityManager createEntityManager(){
return this.emf.createEntityManager();
}
@Produces
@MySqlEm
@ConversationScoped
EntityManager createMysqlEntityManager(){
return this.mysqlemf.createEntityManager();
}
}
行动代码:
@ConversationScoped
@Named
public class PersonAction implements Serializable{
@Inject
EntityManager em;
@Inject
@MySqlEm
EntityManager mysqlEm;
Person person;
List<Person> personList;
@PostConstruct
void afterCreate(){
person = new Person();
personList = queryPersonList();
}
private List<Person> queryPersonList() {
String jql = "select o from Person o ";
List<Person> result = em.createQuery(jql,Person.class).getResultList();
if(result == null)
return new ArrayList<Person>();
return result;
}
@Transactional
public void btnDoSave(ActionEvent event){
try {
if(StringUtils.isEmpty(person.getUuid())){
em.persist(person);
}else{
em.merge(person);
}
em.flush();
String msg = "Saved:"+person.getName();
FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_INFO,msg,null));
person = new Person();
personList = queryPersonList();
} catch (Exception e) {
e.printStackTrace();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), null));
}
}
...getter and setter
}
并会出现错误:
ERROR [org.jboss.as.txn] (default task-6) JBAS010152: APPLICATION ERROR: transaction still active in request with status 0
答案 0 :(得分:-1)
修改方法的注释,添加正确的EntityManager的特殊限定符,这样问题就解决了。但是还有另外一个问题,如何在一个方法中支持多个entityManager的事务?
@Transactional(qualifier = {H2Em.class})