List of usage examples for javax.persistence EntityManager getTransaction
public EntityTransaction getTransaction();
EntityTransaction
object. From source file:it.infn.ct.futuregateway.apiserver.v1.ApplicationCollectionService.java
/** * Register a new application.// w w w .j a va 2 s . c o m * * @param application The application to register * @return The registered application */ @POST @Status(Response.Status.CREATED) @Consumes({ MediaType.APPLICATION_JSON, Constants.INDIGOMIMETYPE }) @Produces(Constants.INDIGOMIMETYPE) public final Application createApplication(final Application application) { if (application.getInfrastructureIds() == null || application.getInfrastructureIds().isEmpty()) { throw new BadRequestException(); } Date now = new Date(); application.setDateCreated(now); EntityManager em = getEntityManager(); EntityTransaction et = null; try { et = em.getTransaction(); et.begin(); List<Infrastructure> lstInfra = new LinkedList<>(); for (String infraId : application.getInfrastructureIds()) { Infrastructure infra = em.find(Infrastructure.class, infraId); if (infra == null) { throw new BadRequestException(); } lstInfra.add(infra); } application.setInfrastructures(lstInfra); em.persist(application); et.commit(); log.debug("New application registered: " + application.getId()); } catch (BadRequestException re) { throw re; } catch (RuntimeException re) { log.error("Impossible to create the application"); log.error(re); throw re; } finally { if (et != null && et.isActive()) { et.rollback(); } em.close(); } return application; }
From source file:nl.b3p.viewer.admin.stripes.CycloramaConfigurationActionBean.java
public Resolution removeKey() { EntityManager em = Stripersist.getEntityManager(); em.remove(account);//from www .j av a2s . c o m em.getTransaction().commit(); account = new CycloramaAccount(); this.context.getMessages().add( new SimpleMessage(getBundle().getString("viewer_admin.cycloramaconfigurationactionbean.keyrem"))); return view(); }
From source file:com.epam.training.taranovski.web.project.repository.implementation.EmployerRepositoryImplementation.java
@Override public List<Vacancy> getActiveVacancys(Employer employer) { EntityManager em = entityManagerFactory.createEntityManager(); List<Vacancy> list = new LinkedList<>(); try {// w w w. ja va2s .c om em.getTransaction().begin(); TypedQuery<Vacancy> query = em.createNamedQuery("Vacancy.findActiveByEmployer", Vacancy.class); query.setParameter("employer", employer); list = query.getResultList(); em.getTransaction().commit(); } catch (RuntimeException e) { Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e); } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } return list; }
From source file:de.zib.gndms.infra.tests.FileTransferActionTest.java
@Test(groups = { "net", "db", "sys", "action", "task" }) public void testIt() throws ResourceException, ExecutionException, InterruptedException { EntityManager em = null; try {/*w w w. jav a2 s.c om*/ em = getSys().getEntityManagerFactory().createEntityManager(); em.getTransaction().begin(); em.persist(task); em.getTransaction().commit(); FileTransferTaskAction action = new FileTransferTaskAction(em, task); Future<AbstractTask> serializableFuture = getSys().submitAction(action, log); assert serializableFuture.get().getState().equals(TaskState.FINISHED); FileTransferResult ftr = (FileTransferResult) task.getData(); for (String s : Arrays.asList(ftr.getFiles())) System.out.println(s); } finally { if (em != null && em.isOpen()) em.close(); } }
From source file:it.infn.ct.futuregateway.apiserver.v1.TaskCollectionService.java
/** * Register a new task.//from ww w.j a v a2s . co m * * @param task The task to register * @return The task registered */ @POST @Status(Response.Status.CREATED) @Consumes({ MediaType.APPLICATION_JSON, Constants.INDIGOMIMETYPE }) @Produces(Constants.INDIGOMIMETYPE) public final Task createTask(final Task task) { if (task.getApplicationId() == null) { throw new BadRequestException("A valid application for the task" + " must be provided"); } task.addObserver(new TaskObserver(getEntityManagerFactory(), getSubmissionThreadPool())); task.setDateCreated(new Date()); task.setUserName(getUser()); task.setStatus(Task.STATUS.WAITING); EntityManager em = getEntityManager(); EntityTransaction et = null; try { et = em.getTransaction(); et.begin(); Application app = em.find(Application.class, task.getApplicationId()); if (app == null) { throw new BadRequestException("Application id not valid"); } task.setApplicationDetail(app); em.persist(task); et.commit(); log.debug("New task registered: " + task.getId()); } catch (BadRequestException bre) { throw bre; } catch (RuntimeException re) { log.error("Impossible to create a task"); log.debug(re); throw re; } finally { if (et != null && et.isActive()) { et.rollback(); } em.close(); } return task; }
From source file:com.epam.training.taranovski.web.project.repository.implementation.CheckDocumentRepositoryImplementation.java
@Override public CheckDocument findByEmployee(Employee employee) { EntityManager em = entityManagerFactory.createEntityManager(); CheckDocument checkDocument = null;/*w ww. ja va 2 s .co m*/ try { em.getTransaction().begin(); TypedQuery<CheckDocument> query = em.createNamedQuery("CheckDocument.findByEmployee", CheckDocument.class); query.setParameter("employee", employee); checkDocument = query.getSingleResult(); em.getTransaction().commit(); } catch (RuntimeException e) { Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e); } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } return checkDocument; }
From source file:com.epam.training.taranovski.web.project.repository.implementation.CheckDocumentRepositoryImplementation.java
@Override public CheckDocument findByVacancy(Vacancy vacancy) { EntityManager em = entityManagerFactory.createEntityManager(); CheckDocument checkDocument = null;/* w ww . ja v a2 s.c o m*/ try { em.getTransaction().begin(); TypedQuery<CheckDocument> query = em.createNamedQuery("CheckDocument.findByVacancy", CheckDocument.class); query.setParameter("vacancy", vacancy); checkDocument = query.getSingleResult(); em.getTransaction().commit(); } catch (RuntimeException e) { Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e); } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } return checkDocument; }
From source file:com.github.jrh3k5.membership.renewal.mailer.service.jpa.JpaMembershipService.java
private void updateMembership(EntityManager entityManager, JpaMembershipRecord membership) { final EntityTransaction transaction = entityManager.getTransaction(); try {/*w w w . ja v a 2 s. c o m*/ transaction.begin(); entityManager.merge(membership); transaction.commit(); } catch (RuntimeException e) { transaction.rollback(); throw e; } }
From source file:ejb.bean.UsuarioDAOJPAImplBean.java
/**Mtodo para a remoo de usurio. * @author Richel Sensineli/*from www . j av a 2s.c om*/ * @param id int - ID do usurio * @throws UsuarioNaoEncontradoException - usurio no encontrado */ @Override public void removeUsuario(final int id) throws UsuarioNaoEncontradoException { EntityManagerFactory emf = Persistence.createEntityManagerFactory("UsuarioPU"); EntityManager em = emf.createEntityManager(); Usuario u = em.find(UsuarioImpl.class, id); em.getTransaction().begin(); if (u == null) { throw new UsuarioNaoEncontradoException("usuario no encontrado"); } else { em.remove(u); em.getTransaction().commit(); } em.clear(); em.close(); emf.close(); }
From source file:fr.univlorraine.ecandidat.controllers.DemoController.java
/** Lance un script sql * @param script/*from w w w . j a v a 2 s .com*/ */ @Transactional private void launchSqlScript(String script) { EntityManager em = entityManagerFactoryEcandidat.createEntityManager(); em.getTransaction().begin(); try { final InputStream inputStream = this.getClass().getResourceAsStream("/db/demo/" + script); final InputStreamReader inputStreamReader = new InputStreamReader(inputStream); final BufferedReader bufferedReader = new BufferedReader(inputStreamReader); while (bufferedReader.ready()) { Query query = em.createNativeQuery(bufferedReader.readLine()); query.executeUpdate(); } } catch (Exception e) { em.getTransaction().rollback(); em.close(); } em.getTransaction().commit(); em.close(); }