Hi friends

I wanted to use two EntityManager in my EJB 3 application but i encountered this exception from container : java.lang.IllegalStateException: Local transaction already has 1 non-XA Resource: cannot add more resources.

For solving this problem i find a solution . I inject a UserTransaction object into my stateless session bean then when i want to use two EntityManager i begin UserTransaction and finally commit UserTransaction like below :

@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class BusinessBean implements BusinessRemote
{
@Resource()
private UserTransaction utx;

@PersistenceContext()
private EntityManager em1 ;

@PersistenceContext()
private EntityManager em2 ;

public void someMethod()
{
try
{
utx.begin();
//use em1
utx.commit();

utx.begin();
//use em2
utx.commit();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}


Please insert ur comment.

bye friends