Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Texo/EntityManagerHandling

< Texo
Revision as of 23:28, 23 September 2011 by Mtaal.springsite.com (Talk | contribs) (New page: __TOC__ == Introduction == The Texo runtime server environment makes use of JPA/EntityManager to access the underlying database. EntityManager instances are often provided by the contain...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Introduction

The Texo runtime server environment makes use of JPA/EntityManager to access the underlying database. EntityManager instances are often provided by the container environment in which your code runs. The Texo generated code needs to be able to retrieve an EntityManager instance from the environment in which it runs. This document explains how Texo works with the EntityManager and it provides pointers on how to integrate your environment with Texo so that one EntityManager/Transaction can be used in the application code, the Texo framework code and as well as in the generated code.

EntityManager handling

The Dao class can get to an EntityManager in different ways:

  • the EntityManager member in the DAO has the @PersistenceContext annotation, so depending on the environment this can work automatically (but you probably need to instantiate your Dao's in a container specific way to make use of this feature).
  • an EntityManager can be explicitly set in the Dao, or
  • if no EntityManager is set then the Dao class will use the EntityManagerProvider.getEntityManager method (described in the next section).

The EntityManagerProvider

The Dao classes make use of the EntityManagerProvider which is the central provider of EntityManager instances for the Dao classes and ObjectStore.

There is a single instance of the EntityManagerProvider available, it can be retrieved using getInstance. You can set your own instance by calling setInstance (both methods are static).

The EntityManagerProvider can be initialized in different ways:

  • setting an EntityManagerFactory in the instance, or
  • by setting the persistence properties needed to create/initialize an EntityManagerProvider

The EntityManagerProvider supports a special way of creating and managing an EntityManager: getCurrentEntityManager. This method will return an EntityManager which is stored in a ThreadLocal. This corresponds to the getCurrentSession method on a Hibernate SessionFactory.

You can let the EntityManagerProvider work in a 'getCurrentEntityManager' mode by calling setUseCurrentEntityManagerPattern with the parameter true. Then getEntityManager will return the EntityManager stored in the ThreadLocal. This means that subsequent calls in one thread will make use of the same EntityManager. This often make sense.

It is very important to clean/clear up the EntityManager when using the getCurrentEntityManager approach. This because Tomcat (and maybe also other servlet containers) will re-use Threads for different requests. To clean up an EntityManager call EntityManagerProvider.getInstance().clearCurrentEntityManager();

Back to the top