Example usage for javax.persistence EntityManager getTransaction

List of usage examples for javax.persistence EntityManager getTransaction

Introduction

In this page you can find the example usage for javax.persistence EntityManager getTransaction.

Prototype

public EntityTransaction getTransaction();

Source Link

Document

Return the resource-level EntityTransaction object.

Usage

From source file:com.epam.training.taranovski.web.project.repository.implementation.UserSkillRepositoryImplementation.java

@Override
public boolean update(UserSkill userSkill) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    try {// www.  jav  a 2s.  co  m
        em.getTransaction().begin();
        em.merge(userSkill);
        em.getTransaction().commit();
        success = true;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.VacancySkillRepositoryImplementation.java

@Override
public boolean create(VacancySkill vacancySkill) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    try {//  w  ww.j  av a  2  s  .  c  om
        em.getTransaction().begin();
        em.persist(vacancySkill);
        em.getTransaction().commit();
        success = true;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.VacancySkillRepositoryImplementation.java

@Override
public boolean update(VacancySkill vacancySkill) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    try {//w  w w  .j  a v  a  2 s  . c  om
        em.getTransaction().begin();
        em.merge(vacancySkill);
        em.getTransaction().commit();
        success = true;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.VacancySkillRepositoryImplementation.java

@Override
public VacancySkill getById(int id) {
    EntityManager em = entityManagerFactory.createEntityManager();
    VacancySkill vacancySkill;/*from   w w  w .ja  v  a 2 s . co m*/
    try {
        em.getTransaction().begin();
        vacancySkill = em.find(VacancySkill.class, id);
        em.getTransaction().commit();
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return vacancySkill;
}

From source file:org.spc.ofp.tubs.domain.common.CommonRepository.java

public boolean saveVessel(final Vessel vessel) {
    boolean success = false;
    final EntityManager mgr = emf.createEntityManager();
    final EntityTransaction xa = mgr.getTransaction();
    xa.begin();/*w  ww. j a v  a 2  s .com*/
    try {
        mgr.persist(vessel);
        xa.commit();
        success = true;
    } catch (Exception ignoreMe) {
        rollbackQuietly(xa);
    } finally {
        mgr.close();
    }
    return success;
}

From source file:com.pocketgorilla.stripesem.TransactionFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (log.isDebugEnabled())
        log.debug("filtering " + ((HttpServletRequest) request).getRequestURI());

    try {//from ww w .j  ava  2s  .  c  om
        chain.doFilter(request, response);
    } catch (Exception ex) {
        try {
            EntityManager em = provider.getEntityManager(false);
            if (em != null) {
                EntityTransaction tx = em.getTransaction();
                if (tx.isActive()) {
                    tx.setRollbackOnly();
                }
            }
        } finally {
            throw new ServletException(ex);
        }
    } finally {
        doAfter();
    }
}

From source file:org.spc.ofp.tubs.domain.common.CommonRepository.java

public boolean saveImportStatus(final ImportStatus status) {
    boolean success = false;
    final EntityManager mgr = emf.createEntityManager();
    final EntityTransaction xa = mgr.getTransaction();
    xa.begin();//from   w  ww.  jav a2 s. c o m
    try {
        if (status.getId() > 0L) {
            mgr.merge(status);
        } else {
            mgr.persist(status);
        }
        xa.commit();
        success = true;
    } catch (Exception ignoreMe) {
        rollbackQuietly(xa);
    } finally {
        mgr.close();
    }
    return success;
}

From source file:com.sdl.odata.datasource.jpa.JPADataSource.java

@Override
public void delete(ODataUri uri, EntityDataModel entityDataModel) throws ODataException {
    Option<Object> entity = extractEntityWithKeys(uri, entityDataModel);

    if (entity.isDefined()) {
        Object jpaEntity = entityMapper.convertODataEntityToDS(entity.get(), entityDataModel);
        if (jpaEntity != null) {
            EntityManager entityManager = getEntityManager();
            EntityTransaction transaction = entityManager.getTransaction();
            try {
                transaction.begin();//from   w w  w.  j  a  va2  s  .co m

                Object attached = entityManager.merge(jpaEntity);
                entityManager.remove(attached);
            } catch (PersistenceException e) {
                LOG.error("Could not remove entity: {}", entity);
                throw new ODataDataSourceException("Could not remove entity", e);
            } finally {
                if (transaction.isActive()) {
                    transaction.commit();
                } else {
                    transaction.rollback();
                }
            }
        } else {
            throw new ODataDataSourceException("Could not remove entity, could not be loaded");
        }
    }
}

From source file:com.nokia.helium.metadata.tests.TestORMFMPPLoader.java

/**
 * Populates the LogFile table with basic data.
 * @throws MetadataException/*  w  ww . j a  v  a  2s  .com*/
 * @throws IOException
 */
@Before
public void populateDatabase() throws MetadataException, IOException {
    File tempdir = new File(System.getProperty("test.temp.dir"));
    tempdir.mkdirs();
    database = new File(tempdir, "test_db");
    if (database.exists()) {
        FileUtils.forceDelete(database);
    }
    EntityManagerFactory factory = FactoryManager.getFactoryManager().getEntityManagerFactory(database);
    EntityManager em = factory.createEntityManager();
    try {
        em.getTransaction().begin();
        for (int i = 0; i < 2000; i++) {
            LogFile lf = new LogFile();
            lf.setPath("log" + String.format("%04d", i));
            em.persist(lf);
        }
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().commit();
        }
        em.close();
        factory.close();
    }
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.BasicSkillRepositoryImplementation.java

@Override
public BasicSkill getById(int id) {
    EntityManager em = entityManagerFactory.createEntityManager();
    BasicSkill skill = null;//  ww w .java  2 s  . co m
    try {
        em.getTransaction().begin();
        skill = em.find(BasicSkill.class, id);
        em.getTransaction().commit();
    } catch (RuntimeException e) {
        Logger.getLogger(BasicSkillRepositoryImplementation.class.getName()).info(e);
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return skill;
}