Which CDI scope should be preferred for a JPA repository

How do I determine the appropriate CDI scope for a JPA repository like below?

@Transactional
public class CategoryRep extends EntityRepository<Integer, Category> {

    private static final long serialVersionUID = 856370976984333182L;

    public List<Category> getAllCategories() {
        // TODO
    }

    public List<Post> getAllPostsOfCategory(Integer categoryId) {
        // TODO
    }

    @Transactional(value = TxType.REQUIRES_NEW, rollbackOn = RuntimeException.class)
    public void createCategory(final String name, final String description)
            throws DaoJpaException {
        // TODO
    }
}

I can think of only two CDI scopes:

  • @SessionScope : For me this seems to be the most appropriate scope, since a single instance is dedicated to an active session.
  • @Dependent : Reasonable too, since such an instance will be fit into the lifecycle of the object where it will be injected.
  • All other CDI-scopes aren't appropriate in my opinion:

  • @ApplicationScoped : Since an application scoped repository will be created just once a time, a call like: entityManager.clear() will cause that all instances of all current sessions will be detached as well. Additionally this single repository have to handle all incoming requests. Possibly this scope is appropriate for read only repository?
  • @RequestScope : I think that there is no need for a new repository instance for each incoming request.
  • @ConversionScoped : Conversions are normally enforced at the GUI layer. If I want the repository to die after the end of a conversion, I can define it as @Dependent .
  • So, are there any best practices known to this issue?


    To describe all of the options

  • Session Scope doesn't really make sense because your repository should be stateless thus session should have no meaning for it (although it would solve the concurrency problem
  • Dependent scope could be a good option, however it can lead to memory leaks (you can never be sure what the dependent object will be removed from memory)
  • Conversation scope doesn't make any sense either, it's the same case as the Session scope
  • So I would go with either

  • Request scope - I would say this the best option, it's stateless by it's nature so you will avoid any concurrency problems. Also don't worry about performance, creating new objects in really, really fast in new JVMs
  • Application scope could be a good option too, however you need to worry about concurrency problems(multiple clients calling the same method at the same time).
  • So if I were you I would go for Request scope repository, it's the most easier option and you will avoid all the problems that way.


    You should use @ApplicationScoped.

    An EntityManager should be used per request and not reused.

    At the begging of the request you create the entityManager, when it finishes commit or rollback it.

    @Dependent will have the same scope of its 'host', if you inject it in a @RequestScoped and bean will have the same scope.

    @SessionScoped should be used only for small data (like user login and permissions).

    链接地址: http://www.djcxy.com/p/62212.html

    上一篇: 跨两个GF服务器部署JSF ViewScoped CDI Bean不一致

    下一篇: JPA存储库应该首选哪个CDI范围