I'm wondering what would be the most efficient (in terms of needed resources) scope for a repository class in CDI. Imagine the following scenario:
@RequestScoped
//OR @ApplicationScoped OR @SessionScoped OR @ConversationScoped?
public class SomeRepository{
@Inject
private EntityManager em;
public SomeClass getSomeClassById(int id){
return em.createNamedQuery("getSomeClassById",SomeClass.class).
setParameter("id",id).getSingleResult();
}
}
The EntityManager in ths example is produced with a @RequestScoped
scope.
Interesting question I think. Unexpectedly I can't think of anything that makes these classes special. So I would make them @ApplicationScoped
as a new instance would function exactly the same as the one I just discarded. Not sure it would have any noticeable impact on the heap, probably not but maybe if they had to be recreated a lot?
I think it's fine to go with what conceptually feels more right for you.