Overview
Other MyFaces Extensions
- ExtVal
- Ext-Script
- [Orchestra]
- [Portlet Bridge]
Community
Development
Sponsorship
Your browser does not support iframes
...
The following example will create a CDI producer method for creating a @RequestScoped EntityManager with qualifier @Default.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
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).
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 | ||||||
---|---|---|---|---|---|---|
| ||||||
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(); } } } |