共享EntityManager上下文中使用的事务的范围是什么?

时间:2015-07-29 15:49:54

标签: java jpa persistence

在JPA中: 请考虑以下示例,它使用Container托管事务范围的实体管理器。

public class ItemDAOImpl implements ItemDAO { 
    @PersistenceContext(unitName="ItemService") 
    EntityManager em; 

    LoggingService ls; 

    public void createItem(Item item) { 
        em.persist(item); 
        ls.log(item.getId(), "created item"); 
    } 

    // ... 
}  

public class LoggingService implements AuditService { 
    @PersistenceContext(unitName="ItemService") 
    EntityManager em; 


    public void log(int itemId, String action) { 
        // verify item id is valid 
        if (em.find(Item.class, itemId) == null) { 
            throw new IllegalArgumentException("Unknown item id"); 
        } 
        LogRecord lr = new LogRecord(itemId, action); 
        em.persist(lr); 
    } 

} 

我是否正确地假设ls.log()方法
将使用调用方法的事务 我现在对这些事情很困惑,你能帮忙吗?

1 个答案:

答案 0 :(得分:1)

如果您使用的是EJB,那么很可能这些方法将使用相同的事务,因为default transaction propagation方法。只需检查它们的配置方式,因为它们似乎是在XML文件中配置的。

相关问题