Daniel,
The BluePrints page [1] has a good overview of thread-safety and other important
choices that can be helpful.
[1]
https://blueprints.dev.java.net/bpcatalog/ee5/persistence/webonlyapp.html
-marina
Doug Clarke wrote:
> I would create a shared EntityManagerFactory (EMF) and then create an
> EntityManager (EM) per thread/requests instead of an EMF per thread/request.
>
> Doug
>
> -----Original Message-----
> *From:* Daniel Cavalcanti [mailto:dhcavalcanti_at_gmail.com]
> *Sent:* Friday, September 01, 2006 12:01 PM
> *To:* persistence_at_glassfish.dev.java.net
> *Subject:* Re: thread-safety
>
> Ok,
> thanks,
>
> So, a solution would be to either serializa access to the method, or
> the following, right?
>
> class Foo {
>
> // method accessed by several concurrent threads
> public void foo() {
>
> EntityManagerFactory emf =
> Persistence.createEntityManagerFactory("my-persistence-unit");
> EntityManager manager = emf.createEntityManager();
>
> try {
> manager.getTransaction().begin();
> // TODO: do somethinf
> manager.getTransaction().commit();
> } catch(Exception e) {
> manager.getTransaction().rollback();
> }
>
> manager.close();
>
> }
>
> }
>
> On 9/1/06, *Doug Clarke* <douglas.clarke_at_oracle.com
> <mailto:douglas.clarke_at_oracle.com>> wrote:
>
> Daniel,
>
> The specification states in section 5.2:
> "An entity manager may not be shared among multiple concurrently
> executing threads. Entity managers may only be accessed in a
> single-threaded manner."
>
> The underlying TopLink implementation is thread safe but I
> cannot guarantee the JPA implementation on top of it is as it is
> not required to be.
>
> Doug Clarke
> Principal Product Manager
> Oracle TopLink
> douglas.clarke_at_oracle.com <mailto:douglas.clarke_at_oracle.com>
>
> -----Original Message-----
> *From:* Daniel Cavalcanti [mailto:dhcavalcanti_at_gmail.com
> <mailto:dhcavalcanti_at_gmail.com>]
> *Sent:* Friday, September 01, 2006 11:13 AM
> *To:* persistence_at_glassfish.dev.java.net
> <mailto:persistence_at_glassfish.dev.java.net>
> *Subject:* thread-safety
>
> Hello,
>
> Quick question: Is the EntityManager thread safe?
>
> For example,
>
> class Foo {
>
> private EntityManagerFactory emf =
> Persistence.createEntityManagerFactory("my-persistence-unit");
> private EntityManager manager = emf.createEntityManager();
>
> // method accessed by several concurrent threads
> public void foo() {
>
> try {
> manager.getTransaction().begin();
> // TODO: do somethinf
> manager.getTransaction().commit();
> } catch(Exception e) {
> manager.getTransaction().rollback();
> }
>
> }
>
> }
>
> thanks,
> Daniel.
>
>