Example usage for javax.persistence EntityTransaction rollback

List of usage examples for javax.persistence EntityTransaction rollback

Introduction

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

Prototype

public void rollback();

Source Link

Document

Roll back the current resource transaction.

Usage

From source file:com.appdynamicspilot.persistence.BasePersistenceImpl.java

/**
 * The method to update the serailizable business objects into the database.
 * //w  ww .  j a  va  2  s  . c o m
 * @param object --
 *            serializable object
 * @throws PersistenceException
 */
@Transactional
public void update(final Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();
    try {
        entityManager.merge(object);
    } catch (Exception ex) {
        logger.error(ex);
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }
}

From source file:com.appdynamicspilot.persistence.BasePersistenceImpl.java

@Transactional
public void delete(Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();//from w ww  . ja  va 2  s .co  m
    try {
        entityManager.remove(object);
    } catch (Exception ex) {
        logger.error(ex);
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }
}

From source file:com.appdynamicspilot.persistence.BasePersistenceImpl.java

@Transactional
public void save(final Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();//w  ww.  j ava2  s. c  om
    try {
        entityManager.persist(object);
    } catch (Exception ex) {
        logger.error(ex);
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }

}

From source file:org.spc.ofp.tubs.domain.purseseine.TripRepository.java

public void save(final PurseSeineTrip trip) {
    final EntityManager mgr = emf.createEntityManager();
    final EntityTransaction xa = mgr.getTransaction();
    try {/* w w  w .  java  2s  .c  o  m*/
        xa.begin();
        mgr.persist(trip);
        mgr.flush();
        xa.commit();
        mgr.refresh(trip);
    } catch (Exception ex) {
        if (xa.isActive()) {
            xa.rollback();
        }
    } finally {
        mgr.close();
    }
}

From source file:facades.PersonFacadeDB.java

@Override
public Person delete(Integer id) throws NotFoundException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();// ww w.j a  va2s .c  om

    Person p = em.find(Person.class, id);

    if (p == null) {
        transaction.rollback();
        throw new NotFoundException("No person for the given id");
    } else {
        em.remove(p);
        transaction.commit();
    }

    return p;
}

From source file:facades.PersonFacadeDB.java

@Override
public Person addPerson(String json) {
    //make person from Json
    Person p = gson.fromJson(json, Person.class);

    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();//from  ww w .j  a  v  a  2s . c o  m

    try {
        em.persist(p);
        transaction.commit();
    } catch (Exception e) {
        transaction.rollback();
    } finally {
        em.close();
    }
    return p;
}

From source file:fr.xebia.demo.wicket.blog.service.GenericService.java

protected void rollbackTransaction() throws ServiceException {
    try {/*  w  w w  .ja  v  a 2s  .  com*/
        EntityTransaction transaction = currentEntityManager().getTransaction();
        if (transaction.isActive()) {
            transaction.rollback();
        }
    } catch (PersistenceException e) {
        throw new ServiceException("Can't rollback transaction", e);
    }
}

From source file:org.apache.james.sieve.jpa.JPASieveRepository.java

private void rollbackTransactionIfActive(EntityTransaction transaction) {
    if (transaction.isActive()) {
        transaction.rollback();
    }
}

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 {//  ww  w.  ja  va  2 s  . co  m
        transaction.begin();
        entityManager.merge(membership);
        transaction.commit();
    } catch (RuntimeException e) {
        transaction.rollback();
        throw e;
    }
}

From source file:org.easy.criteria.BaseDAO.java

/**
 * {@link javax.persistence.EntityTransaction#isActive())}
 * {@link javax.persistence.EntityTransaction#rollback())}
 *///from  w w  w  .j  a  v a 2s .  co  m
public void rollbackTransaction(EntityTransaction transaction) {
    if (transaction.isActive())
        transaction.rollback();
}