我想知道禁用共享缓存时这些方法是否等效:
@Stateless
public class EntityService
{
@PersistenceContext
private EntityManager em;
public <T> T findEntity1(Class<T> clazz, long id)
{
return em.find(clazz, id);
}
public <T> T findEntity2(Class<T> clazz, long id)
{
T entity = em.find(clazz, id);
em.refresh(entity);
return entity;
}
}
这些方法永远不会在现有事务中调用。该应用程序仅使用 JPA 独占访问数据库,并且未定义触发器/存储过程/其他。
我的猜测是它们是等价的,因为:
- em.find() 将搜索共享缓存(L2),但它是空的(禁用)
- em.find() 将搜索自己的缓存(L1),但它是空的(没有以前的事务 = em 是新的)
- em.find() 将访问数据库
- em.refresh() 将第二次访问 db,但在这种情况下,实体总是相同的
我错过了什么?