Hi! Sorry for my bad english skills. When I execute fulfilOrder, only Order entity is updated in the database, and User entity remains modified only in code, even if I go:
user = smdb.find(User.class, user.id);
In this source highlighted lines modifying entity, but not the database.
@Stateless public class SMDB { @PersistenceContext(unitName = "SM") private EntityManager smdb; @PersistenceContext(unitName = "SMShop") private EntityManager shopdb; ... public Order fulfilOrder(Long orderID, Long userID, int itemID) { Order order = shopdb.find(Order.class, orderID); if (order == null) { User user = smdb.find(User.class, userID); ShopItem item = shopdb.find(ShopItem.class, itemID); if (item == null || user == null) return null; user.money += item.total; // <-this smdb.merge(user); // <-and this order = new Order(orderID, user, item); shopdb.persist(order); } return order; }
If I delete them and add them outside, everything work fine.
@EJB SMDB lib; ... Order orderInfo = lib.fulfilOrder(orderID, userID,itemID); User user = lib.getUser(userID);//it's em.find(user_id) user.money += item.total; // <- moved this lines here lib.updateUser(user);// it's em.merge(user);
So my question : Is it impossible to perform two transactions in one method or it's a bug?
Thanks.