I’m using IBM RAD 7.5 and WebSphere 7.
I have an EJB (@Stateless CustomerService), and I have two EJBs that are DAOs (@Stateless CustomerDAO and @Stateless OrderDAO).
My method in CustomerService works fine when I do customerDAO.getAll().
But if I call customerDAO.getAll() and then orderDAO.getByCustomerId(int id), the second getAll throws an EJBTransactionRolledBackException.
Why does this happen, and how to prevent it?
Thanks, Rob
update with code
I have this following …
@Stateless
public class CustomerService
@EJB
private CustomerDAO customerDAO;
@EJB
private OrderDAO orderDAO;
public void myMethod() {
List<Customer> customers = customerDAO.getAll();
for (Customer c : customers) {
List<Order> orders = orderDAO.getByCustomerId(c.getId());
/*** THIS THROWS EJBTransactionRolledBackException ***/
}
}
… and my DAOs look like this …
@Stateless
public class CustomerDAO
@PersistenceUnit
private EntityManagerFactory emf;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public List<Customer> getAll() {
try {
em = getEntityManager();
Query query = em.createQuery(" /*...*/ ");
query.getResultList();
} finally {
em.close();
}
}
… and …
@Stateless
public class OrderDAO
@PersistenceUnit
private EntityManagerFactory emf;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public List<Customer> getByCustomerId(int customerid) {
try {
em = getEntityManager();
Query query = em.createQuery(" /*...*/ ");
/* ... */
query.getResultList();
} finally {
em.close();
}
}
Any ideas?
Thanks, Rob
I would guess you have an issue with your EJB transaction demarcations. Firstly, your DAOs should not be EJBs. EJBs should be used at the service layer where one of the main advantages is declarative transaction management. By making your DAOs EJBs, you are engaging container managed transaction operations at the DAO method level.
If you provide your dao code and configuration, we can make a more definitive assessment.