EntityManagerFactory emf = Persistence.createEntityManagerFactory( "MeineJpaPU" ); try { EntityManager em = emf.createEntityManager(); EntityTransaction tx = null; try { tx = em.getTransaction(); tx.begin(); em.persist( entity1 ); em.merge( entity2 ); em.find( MyEntity.class, id ); tx.commit(); } catch( RuntimeException ex ) { if( tx != null && tx.isActive() ) tx.rollback(); throw ex; } finally { em.close(); } } finally { emf.close(); }