At runtime, you make persistence unit configuration available to the web application through an entity manager factory. Its main job is to instantiate entity managers for you that provide the persistence context in which you manipulate the database. Although you can create multiple factories based on a single persistence unit, it is not recommended. Instead, you ought to instantiate one statically-scoped entity manager factory and provide a thread-safe accessor that employs the singleton pattern to ensure there's always only one. That accessor initializes a new EMF if none exists and return EntityManager objects.
An example that uses a persistence unit called "puGeldzin":
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public final class DBUtil {
private static EntityManagerFactory emf;
public static synchronized EntityManager entityManager() {
if(emf == null) {
emf = Persistence
.createEntityManagerFactory("puGeldzin");
}
return emf.createEntityManager();
}
}