Example usage for javax.persistence EntityTransaction begin

List of usage examples for javax.persistence EntityTransaction begin

Introduction

In this page you can find the example usage for javax.persistence EntityTransaction begin.

Prototype

public void begin();

Source Link

Document

Start a resource transaction.

Usage

From source file:org.apache.juddi.v3.auth.jboss.JBossAuthenticator.java

public UddiEntityPublisher identify(String authInfo, String authorizedName) throws AuthenticationException {
    EntityManager em = PersistenceManager.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    Publisher publisher = null;/*from   www.ja  va 2 s.co m*/
    try {
        tx.begin();
        publisher = em.find(Publisher.class, authorizedName);
        if (publisher == null)
            throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));

        AuthToken at = em.find(AuthToken.class, authInfo);
        if (at == null)
            throw new AuthTokenRequiredException(new ErrorMessage("E_authTokenRequired", authInfo));
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
    return publisher;
}

From source file:org.eclipse.jubula.client.core.persistence.Persistor.java

/**
 * /*  w ww. ja  v a  2 s.com*/
 * @param em
 *            entity manager
 * @throws PersistenceException
 *             if we blow up
 */
private static void createOrUpdateDBVersion(EntityManager em) throws PersistenceException {
    EntityTransaction tx = null;
    try {
        tx = em.getTransaction();
        tx.begin();

        try {
            DBVersionPO version = (DBVersionPO) em.createQuery("select version from DBVersionPO as version") //$NON-NLS-1$
                    .getSingleResult();
            version.setMajorVersion(IVersion.JB_DB_MAJOR_VERSION);
            version.setMinorVersion(IVersion.JB_DB_MINOR_VERSION);
            em.merge(version);
        } catch (NoResultException nre) {
            em.merge(new DBVersionPO(IVersion.JB_DB_MAJOR_VERSION, IVersion.JB_DB_MINOR_VERSION));
        }

        tx.commit();
    } catch (PersistenceException pe) {
        if (tx != null) {
            tx.rollback();
        }
        throw pe;
    }
}

From source file:facades.PersonFacadeDB.java

@Override
public String getPerson(Integer id) throws NotFoundException {
    String result = "";
    //get person with this id from DB
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();
    try {//from  w w  w.  j  av  a  2s  .c  o m
        Person p = em.find(Person.class, id);

        result = om.writeValueAsString(p);
        //            Query query = em.createNamedQuery("Person.findById").setParameter("id", id);
        //            List<Person> people = query.getResultList();

        //result = gson.toJson(people.get(0));
        //result = om.writeValueAsString(people.get(0));
    } catch (Exception e) {
        throw new NotFoundException("No person exists for the given id");
    } finally {
        em.close();
    }
    return result;
}

From source file:facades.PersonFacadeDB.java

@Override
public String getPersons() {

    String result = "";
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();

    try {/* w  ww  .j a  v a2  s  . co  m*/
        Query query = em.createNamedQuery("Person.findAll");
        List<Person> people = query.getResultList();

        try {
            result = om.writeValueAsString(people);
        } catch (JsonProcessingException ex) {
            Logger.getLogger(PersonFacadeDB.class.getName()).log(Level.SEVERE, null, ex);
        }

    } finally {
        em.close();
    }
    return result;
}

From source file:info.san.books.app.model.listener.LivreListener.java

@EventHandler
public void handle(LivreDeletedEvent e) {
    EntityManager em = Persistence.getInstance().createEntityManager();

    EntityTransaction t = em.getTransaction();

    t.begin();

    LivreEntry entry = em.getReference(LivreEntry.class, e.getIsbn());
    em.remove(entry);//www  . j  a v  a2s.  co m

    t.commit();
}

From source file:it.infn.ct.futuregateway.apiserver.v1.ApplicationCollectionService.java

/**
 * Retrieve the application list.//from   www . j  a v  a2  s .  co  m
 * The fields not requested for the list are cleaned.
 * @return The list of applications
 */
private List<Application> retrieveApplicationList() {
    List<Application> lstApps;
    EntityManager em = getEntityManager();
    EntityTransaction et = null;
    try {
        et = em.getTransaction();
        et.begin();
        lstApps = em.createNamedQuery("applications.all", Application.class).getResultList();
        et.commit();
    } catch (RuntimeException re) {
        if (et != null && et.isActive()) {
            et.rollback();
        }
        log.error("Impossible to retrieve the Application list");
        log.error(re);
        throw new RuntimeException("Impossible to access the " + "application list");
    } finally {
        em.close();
    }
    if (lstApps != null) {
        for (Application ap : lstApps) {
            ap.setDescription(null);
            ap.setParameters(null);
        }
    }
    return lstApps;
}

From source file:info.san.books.app.model.listener.LivreListener.java

@EventHandler
public void handle(LivreAssignedAuteurEvent e) {
    EntityManager em = Persistence.getInstance().createEntityManager();

    EntityTransaction t = em.getTransaction();

    t.begin();

    LivreEntry livre = em.find(LivreEntry.class, e.getIsbn());
    AuteurEntry auteur = em.find(AuteurEntry.class, e.getAuteurId());

    livre.getAuteurs().add(auteur);//from  ww w. j  a  va2s  .c  o  m
    auteur.getLivres().add(livre);

    t.commit();
}

From source file:info.san.books.app.model.listener.LivreListener.java

@EventHandler
public void handle(LivreUnassignedAuteurEvent e) {
    EntityManager em = Persistence.getInstance().createEntityManager();

    EntityTransaction t = em.getTransaction();

    t.begin();

    LivreEntry livre = em.find(LivreEntry.class, e.getIsbn());
    AuteurEntry auteur = em.find(AuteurEntry.class, e.getAuteurId());

    livre.getAuteurs().remove(auteur);/*from w w w  .j  ava 2 s.  c  om*/
    auteur.getLivres().remove(livre);

    t.commit();
}

From source file:org.nuxeo.ecm.core.persistence.PersistenceProvider.java

protected void doBegin(EntityManager em) {
    EntityTransaction et = getTransaction(em);
    if (et != null) {
        et.begin();
    }
}

From source file:it.infn.ct.futuregateway.apiserver.v1.ApplicationCollectionService.java

/**
 * Register a new application.//from   w w  w.j  a v a  2s .  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;
}