Skip to content

Add @RequestScoped EntityManager Example #238

Description

@codylerum

A very common pattern is to use an EntityManager which is @RequestScoped with CDI. This is often referred to as the entitymanager-per-request pattern.

Currently I am unable to make this work in coordination with @Transactional without going SynchronizationType.UNSYNCHRONIZED and manually calling an entityManager.joinTransaction()

public class PersistenceResources {

    @PersistenceContext(type = PersistenceContextType.EXTENDED, synchronization = SynchronizationType.SYNCHRONIZED)
    private EntityManager em;

    @Produces
    @RequestScoped
    private EntityManager requestScopedEntityManager() {
        return em;
    }
}

Unfortunately all the entities appear to get detached after the first transaction commits. Is this pattern possible with EE 7 without having to manually join to the transaction?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions