@Documented @Inherited @Retention(value=RUNTIME) @Target(value={TYPE,METHOD,FIELD}) @NormalScope(passivating=false) public @interface TransactionScoped
A @TransactionScoped contextual instance will be unique for a given
transaction controlled by Transactional
. The context will get started when the outermost
Transactional
method gets invoked and will get closed when
the call chain leaves the outermost Transactional
method.
The classic use-case is for producing JPA EntityManagers.
@Dependent public class EntityManagerProducer { private @PersistenceContext(unitName = "test") EntityManager entityManager; public @Produces @TransactionScoped EntityManager createEntityManager() { return entityManager; } public void closeEntityManager(@Disposes EntityManager em) { if (em.isOpen()) //this check is optional -not needed if #close doesn't get called by the transactional bean { em.close(); } } }or
@Dependent public class EntityManagerProducer { private @PersistenceUnit(unitName = "test") EntityManagerFactory entityManagerFactory; public @Produces @TransactionScoped EntityManager createEntityManager() { return entityManagerFactory.createEntityManager(); } public void closeEntityManager(@Disposes EntityManager em) { if (em.isOpen()) //this check is optional -not needed if #close doesn't get called by the transactional bean { em.close(); } } }
Furthermore, it's possible to use different persistence-units with (simple) qualifiers (for the producer- and dispose-methods and therefore also at the injection-points).
It's also possible to use @Transactional and @TransactionScoped in an application-server.
Therefore it's only needed to configure one of the
TransactionStrategy
s which support JTA.
Transactional
Copyright © 2020 The Apache Software Foundation. All rights reserved.