users@glassfish.java.net

Re: Singleton EJB with persistence context

From: Marina Vatkina <marina.vatkina_at_oracle.com>
Date: Wed, 21 Dec 2011 16:42:44 -0800

Olivier,

Olivier Chorier wrote:
> Hi community,
>
> I recently wrote an Singleton EJB and I noticed some difficulties to
> manage my entities :
> - The client calls a method which gets an entity from database and
> stores it as private attribute (EJB attribute).
> - Something is returned to the client, and then it calls another
> method from the same EJB.
> - The singleton didn't forget its attribute, but when trying to force
> reload from database, it is said that it is not managed by the
> persistence context ; even if calling merge() juste before the reload().

merge() is to make an entity managed, so it should work.
>
> This error occurs when using injection with @PersistenceContext to the
> EntityManager. But it doesn't occur when using the factory
> (EntityManagerFactory if I remember).

When you create your own EM, its persistence context is extended, and
all your entities retrieved by such EM are always managed (but you need
to remember to close such EM in PreDestroy.

-marina
>
> Is it a bug of Glassfish, or a normal behaviour ? FYI, we are still in
> 3.0.1.
>
> Thanks for any kind of explaination.
> Olivier.