Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

The Intro page provides an overview, the setup of this module and describes the motivation for the features described below. This page explains the most important APIs and mechanisms of the JPA module provided by CODI. Please note that this page doesn't show all possibilities. If you have any question, please contact the community!

Tip
titleUpdate

This module is completely independent of the rest and already moved to Apache DeltaSpike v0.3+ (including several improvements like easier handling/usage of qualifiers, optional JTA support,...) .

Using one (default) Entity Manager

...

This approach just works if it doesn't come to serialization of this wrapper e.g. in case of session-replication.
If those beans get serialized, you have to overcome this restriction by storing the persistence-unit-name and recreate the EntityManager via Persistence.createEntityManagerFactory(this.persistenceUnitName).createEntityManager(); btw. sync it with the database before closing it on serialization. Furthermore, you have to intercept some methods of the EntityManager to merge detached entities automatically if those entities get serialized as well. However, as mentioned before we don't recommend such an approach.

JTA

CODI itself doesn't support it, but JTA support is available in Apache DeltaSpike.
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 configuration itself is provided by implementing the CODI DataSourceConfig class. The DataSourceConfig determines whether a JNDI path, a JDBC connection or another DataSource should get used, by just returning the proper values. You can use all the flexible CDI and CODI mechanisms like @ProjectStageActivated, @ExpressionActivated, @Alternative, @Specializes, etc and even if/then/else,... to find the perfect settings for your current runtime environment!

TransactionHelper

This helper allows to execute CDI-unmanaged code blocks in a @Transactional manner. E.g. with the test modules provided by MyFaces CODI you can inject CDI beans and therefore transactional services into a JUnit test. So it's possible to use existing implementations e.g. to create sample data for unit tests or to remove such entries again. If a service doesn't (or shouldn't) provide the needed functionality e.g. for deleting entities, it's possible to use TransactionHelper for such additional logic which should be executed within a transaction.

Note
titleAttention

Please be aware that this helper only works for @Transactional with the default qualifier! If you need the functionality for another EntityManager, then you need to copy this code and adopt it.

Code Block
XML
XML
titleOptional usage of TransactionHelper

MyReslt result = TransactionHelper.getInstance().executeTransactional(new Callable<MyReslt>() {
    public MyReslt call() throws Exception
    {
        EntityManager entityManager = BeanManagerProvider.getInstance().getContextualReference(EntityManager.class);
        entityManager.remove(...);
        return entityManager.find(...);
    }
});