EntityManagerFactory emf = Persistence.createEntityManagerFactory("myDb");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
// ...
tx.commit();
} catch (Exception e) {
if (tx.isActive()) {
tx.rollback();
}
} finally {
em.close();
emf.close();
}