April 05
Prof. Ismael H. F. Santos -  ismael@tecgraf.puc-rio.br                                                          2
JPA Transactions
nbegin – commit/rollback
n........
nEntityManager em = emf.createEntityManager();
nEntityTransaction tx = null;
ntry {
n   tx = em.getTransaction();
n   tx.begin();
n   em.setFlushMode(FlushMode.COMMIT); // Only flush at commit time
n                                      // flush automatically –> default
n   Employee e = em.find(Employee.class, id=158); //Usuario recuperado !!!
n   e.setSalary(45000);                 
n   em.createQuery("from Empregado as emp left outer join emp.deps dep")
n                  .getResultList();      //Pode retornar dados desatualizados
n   tx.commit();                       //Executa o flush
n} catch( RuntimeException ex ) {      //Excecoes do em não são
n       if( tx != null && tx.isActive() )  //recuperaveis
n          tx.rollback();                 //Desfaz operações em caso de erro
n       throw ex;
n    } finally {
n      em.close();                          //Libera recursos      
n    }