Problems migrate from Seam3 to DeltaSpike

117 Views Asked by At

I'm just testing to migrate from Seam3 to DeltaSpike, everything is ok if there is only one EntityManager in a bean, but there will be a error if add other EntityManager(other datasource):

JBAS010152: APPLICATION ERROR: transaction still active in request with status 0

the error project:

https://github.com/yuanqixun/hellodeltaspike

run this project environment:

  1. wildfly 8.2.0.Final
  2. H2 datasource
  3. MySql datasource

The EntityManagerProducer code:

@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();
  }

}

The Action code:

@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
}

and there will be error: ERROR [org.jboss.as.txn] (default task-6) JBAS010152: APPLICATION ERROR: transaction still active in request with status 0

1

There are 1 best solutions below

1
yuan On

Modify the method's annotation,add the special qualifier of the right EntityManager, so the problem will be solved. But also has another problem, how to support multiple entityManager's transaction in one method?

@Transactional(qualifier = {H2Em.class})