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.EmployeeRepositoryImplementation.java

@Override
public boolean clearSkills(Employee employee) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    try {// w  w w .  j  a v  a2s.  co  m
        em.getTransaction().begin();

        Query query = em.createNamedQuery("UserSkill.clearSkillsForEmployee");
        query.setParameter("employee", employee);

        em.getTransaction().commit();
        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(EmployeeRepositoryImplementation.class.getName()).info(e);
        success = false;
    } 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.VacancyRepositoryImplementation.java

@Override
public boolean create(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    try {// w ww  .j a  v a2  s. c o m
        em.getTransaction().begin();
        em.persist(vacancy);
        em.getTransaction().commit();
        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(VacancyRepositoryImplementation.class.getName()).info(e);
    } 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.VacancyRepositoryImplementation.java

@Override
public boolean update(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    try {/*  ww w .j a  va  2s . c  o  m*/
        em.getTransaction().begin();
        em.merge(vacancy);
        em.getTransaction().commit();
        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(VacancyRepositoryImplementation.class.getName()).info(e);
    } 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.VacancyRepositoryImplementation.java

@Override
public boolean clearSkills(Vacancy vacancy) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    try {//from www .jav a 2 s .  c om
        em.getTransaction().begin();

        Query query = em.createNamedQuery("VacancySkill.clearSkillsForVacancy");
        query.setParameter("vacancy", vacancy);
        query.executeUpdate();

        em.getTransaction().commit();
        success = true;
    } catch (RuntimeException e) {
        Logger.getLogger(VacancyRepositoryImplementation.class.getName()).info(e);
        success = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

From source file:fr.mby.portal.coreimpl.auth.DbPortalUserAuthenticationProvider.java

/** Init the DB with default users : user:user456 & admin:admin456 */
protected void initDefaultUsers() {
    final PortalUser user = this.buildPortalUser("user", "user456");
    final PortalUser admin = this.buildPortalUser("admin", "admin456");

    final EntityManager portalUserEm = this.portalUserEmf.createEntityManager();

    // Remove transaction
    portalUserEm.getTransaction().begin();

    final PortalUser foundAdmin = this.findPortalUserByLogin(portalUserEm, "admin");
    if (foundAdmin != null) {
        portalUserEm.remove(foundAdmin);
    }/*from  w  w  w .  j a v a  2  s  . c o m*/

    final PortalUser foundUser = this.findPortalUserByLogin(portalUserEm, "user");
    if (foundUser != null) {
        portalUserEm.remove(foundUser);
    }

    portalUserEm.getTransaction().commit();

    // Insert transaction
    portalUserEm.getTransaction().begin();

    portalUserEm.persist(user);
    portalUserEm.persist(admin);

    portalUserEm.getTransaction().commit();

    portalUserEm.close();
}

From source file:net.sf.ehcache.openjpa.datacache.TestEhCache.java

@Test
public void testClearCache() {
    EntityManagerFactory emf = em.getEntityManagerFactory();

    EntityManager entityManager = emf.createEntityManager();
    EntityTransaction tx = entityManager.getTransaction();
    tx.begin();//from w  w  w.  j ava2 s.c  o m
    SubQObject subQObject = new SubQObject("one", "two");
    QObject qObject = new QObject("one");
    PObject pObject = new PObject("one");
    entityManager.persist(subQObject);
    entityManager.persist(qObject);
    entityManager.persist(pObject);
    tx.commit();
    assertTrue(getCache(subQObject.getClass()).contains(getOpenJPAId(subQObject, subQObject.getId())));
    assertTrue(getCache(qObject.getClass()).contains(getOpenJPAId(qObject, qObject.getId())));
    assertTrue(getCache(pObject.getClass()).contains(getOpenJPAId(pObject, pObject.getId())));
    evictAllOfType(qObject.getClass(), false);
    assertFalse("QObject entries should be all gone",
            OpenJPAPersistence.cast(emf).getStoreCache().contains(qObject.getClass(), qObject.getId()));
    assertTrue("SubQObject entries should still be in the cache",
            OpenJPAPersistence.cast(emf).getStoreCache().contains(subQObject.getClass(), subQObject.getId()));
    assertTrue("This PObject object should still be in the cache",
            OpenJPAPersistence.cast(emf).getStoreCache().contains(pObject.getClass(), pObject.getId()));
    tx = entityManager.getTransaction();
    tx.begin();
    qObject = new QObject("two");
    entityManager.persist(qObject);
    tx.commit();
    evictAllOfType(qObject.getClass(), true);
    assertFalse("QObject entries should be all gone",
            OpenJPAPersistence.cast(emf).getStoreCache().contains(qObject.getClass(), qObject.getId()));
    assertFalse("SubQObject entries should be all gone",
            OpenJPAPersistence.cast(emf).getStoreCache().contains(subQObject.getClass(), subQObject.getId()));
    assertTrue("This PObject object should still be in the cache",
            OpenJPAPersistence.cast(emf).getStoreCache().contains(pObject.getClass(), pObject.getId()));
    tx = entityManager.getTransaction();
    tx.begin();
    qObject = new QObject("three");
    entityManager.persist(qObject);
    subQObject = new SubQObject("two", "two");
    entityManager.persist(subQObject);
    tx.commit();
    evictAllOfType(subQObject.getClass(), false);
    assertTrue("QObject entries should still be in the cache",
            OpenJPAPersistence.cast(emf).getStoreCache().contains(qObject.getClass(), qObject.getId()));
    assertFalse("SubQObject entries should be all gone",
            OpenJPAPersistence.cast(emf).getStoreCache().contains(subQObject.getClass(), subQObject.getId()));
    assertTrue("This PObject object should still be in the cache",
            OpenJPAPersistence.cast(emf).getStoreCache().contains(pObject.getClass(), pObject.getId()));
    tx = entityManager.getTransaction();
    tx.begin();
    subQObject = new SubQObject("three", "three");
    entityManager.persist(subQObject);
    tx.commit();
    evictAllOfType(pObject.getClass(), true);
    assertTrue("QObject entries should still be in the cache",
            OpenJPAPersistence.cast(emf).getStoreCache().contains(qObject.getClass(), qObject.getId()));
    assertTrue("SubQObject entries should still be in the cache",
            OpenJPAPersistence.cast(emf).getStoreCache().contains(subQObject.getClass(), subQObject.getId()));
    assertFalse("This PObject object should be gone",
            OpenJPAPersistence.cast(emf).getStoreCache().contains(pObject.getClass(), pObject.getId()));
}

From source file:com.github.jinahya.persistence.ShadowTest.java

@Test(enabled = false, invocationCount = 1)
public void testPersist() {
    final EntityManager manager = LocalPU.createEntityManager();
    try {/*from   w w  w . java 2  s . c o m*/
        final EntityTransaction transaction = manager.getTransaction();
        transaction.begin();
        try {
            final Shadow shadow = persistInstance(manager, null, null);
            transaction.commit();
        } catch (Exception e) {
            LocalPU.printConstraintViolation(e);
            transaction.rollback();
            e.printStackTrace(System.err);
            Assert.fail(e.getMessage());
        }
    } finally {
        manager.close();
    }
}

From source file:ejb.bean.UsuarioDAOJPAImplBean.java

/**Mtodo para a atualizao do usurio
 * @author Richel Sensineli//from www. j  a va 2  s  . c om
 * @param id int - ID do usurio
 * @param nome String - Nome do usurio
 * @param sobrenome String - Nome do usurio
 */
@Override
public void updateUsuario(final int id, final String nome, final String sobrenome)
        throws UsuarioNaoEncontradoException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("UsuarioPU");
    EntityManager em = emf.createEntityManager();

    UsuarioImpl user = em.find(UsuarioImpl.class, id);
    user.setNome(nome);
    user.setSobrenome(sobrenome);

    em.getTransaction().begin();
    try {
        em.merge(user);
        em.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        em.getTransaction().rollback();
    }

    em.clear();
    em.close();
    emf.close();
}

From source file:com.thruzero.domain.jpa.transaction.JpaDatabaseTransactionMgr.java

@Override
public void commitTransaction() {
    if (isTransactionActive()) {
        EntityManager entityManager = doGetCurrentPersistenceManager(false);

        entityManager.getTransaction().commit();

        if (entityManager.isOpen()) {
            entityManager.close();/*  ww  w. j  a va  2s .co m*/
        }
    }
}

From source file:com.thruzero.domain.jpa.transaction.JpaDatabaseTransactionMgr.java

@Override
public void rollbackTransaction() {
    if (isTransactionActive()) {
        EntityManager entityManager = doGetCurrentPersistenceManager(false);

        entityManager.getTransaction().rollback();

        if (entityManager.isOpen()) {
            entityManager.close();/*from  w w w .  j a va  2s .c  o  m*/
        }
    }
}