users@glassfish.java.net

Re: Working with JPA in a non EE environment

From: <glassfish_at_javadesktop.org>
Date: Mon, 07 May 2007 15:33:07 PDT

You can look at Java EE tutorial (http://java.sun.com/javaee/5/docs/tutorial/doc) that has a section of using JPA in the web tier.

You can also read blueprints best practices documents on the same subject: https://blueprints.dev.java.net/bpcatalog/ee5/persistence/webonlyapp.html (the wider list of JPA topics that is being covered can be found here: https://blueprints.dev.java.net/bpcatalog/ee5/persistence/index.html)

Now the questions/notes about your code:

1. PersistenceManager.getEntityManagerFactory() is not thread safe. It's ok to create the EMF eagerly as usually the actual PU processing happens at the first emf.getEntityManager() request.

2. The following code will not close the EM in case of an exception:

try {
  em.getTransaction().begin();
  ...
  em.getTransaction().commit();
 } finally {
  if (em.getTransaction().isActive()) em.getTransaction().rollback();
}
em.close();

Regards,
-marina
[Message sent by forum member 'mvatkina' (mvatkina)]

http://forums.java.net/jive/thread.jspa?messageID=215908