Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The @Transactional annotation of Apache MyFaces CODI will maintain an own transcaction context which automatically starts when an @Transactional method gets invoked, and ends when the outermost @Transactional method returns. This might for example be used to create @TransactionScoped EntityManagers. The effect of such an EntityManager is similar to using EJBs: when the outermost @Transactional method returns, the @TransactionScoped EntityManager will get destroyed and thus em.close() will get called effectively detaching all entities.

This scope is esp. useful if the loaded entities have to be cached.

Code Block
java
java
titleProducer for a @TransactionScoped entity manager
public class DataBaseProducer
{
    @PersistenceContext(unitName="default")
    private EntityManager entityManager;

    @Produces
    @TransactionScoped
    public EntityManager createEntityManager()
    {
        return this.entityManager;
    }

    public void dispose(@Disposes EntityManager entityManager)
    {
        if(entityManager.isOpen())
        {
            entityManager.close();
        }
    }
}

...