I wonder if these methods are equivalent when shared cache is disabled:
@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;
}
}
These methods are never called inside an existing transaction. The db is exclusively accessed by this application using only JPA and no trigger/stored procedure/other is defined.
My guess is they are equivalent, because:
- em.find() will search the shared cache (L2), but it's empty (disabled)
- em.find() will search its own cache (L1), but it's empty (no previous transaction = em is new)
- em.find() will access db
- em.refresh() will access db a second time, but in this scenario the entity is always the same
Did I miss something?