Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: @TransactionScoped added

...

Using one (default) Entity Manager

The following example will create a CDI producer method for creating a @RequestScoped EntityManager with qualifier @Default.

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

    @Produces
    @Default
    @RequestScoped
    public EntityManager createDefaultEntityManager()
    {
        return this.entityManager;
    }

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

...

Currently there is no support for it, however, you can use https://github.com/openknowledge/openknowledge-cdi-extensions in combination with MyFaces CODI for using JTA (if it works in combination with the application server of your choice).

TransactionScoped (since v1.0.2)

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.

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();
        }
    }
}