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:com.appdynamics.loan.persistence.BasePersistenceImpl.java

@Transactional
public void save(final Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();
    try {//from   w ww.  jav  a  2s . c  o m
        entityManager.persist(object);
    } catch (Exception ex) {
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }

}

From source file:name.livitski.tools.persista.schema.example.CustomerCRUDTest.java

@Before
public void createRecord() throws ApplicationBeanException {
    if (null != storedRecordId)
        return;//from w w  w  . j av  a 2  s  . co m
    setUpJPA();
    final EntityManager db = getEntityManager();
    final EntityTransaction txn = db.getTransaction();
    txn.begin();
    Customer customer = new Customer();
    customer.setName(CUSTOMER_NAME);
    customer.setAddress(CUSTOMER_ADDRESS);
    customer.setPhone(CUSTOMER_PHONE);
    customer.setEmail(CUSTOMER_EMAIL);
    db.persist(customer);
    txn.commit();
    storedRecordId = customer.getId();
}

From source file:org.apache.juddi.v3.auth.JUDDIAuthenticator.java

public UddiEntityPublisher identify(String authInfo, String authorizedName) throws AuthenticationException {
    EntityManager em = PersistenceManager.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {/*  ww  w.  jav  a2s .c  om*/
        tx.begin();
        Publisher publisher = em.find(Publisher.class, authorizedName);
        if (publisher == null) {
            throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));
        }

        return publisher;
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
}

From source file:nl.b3p.kaartenbalie.service.MapFileListener.java

private void saveServiceProvider(String wmsUrl, String name) {
    Object identity = null;//from  www.  jav  a 2 s. com
    try {
        identity = MyEMFDatabase.createEntityManager(MyEMFDatabase.MAIN_EM);
        EntityManager em = MyEMFDatabase.getEntityManager(MyEMFDatabase.MAIN_EM);

        EntityTransaction tx = em.getTransaction();
        tx.begin();

        try {
            String getCap = "&service=WMS&request=GetCapabilities&version=1.1.1";

            Long number = getUniqueAbbr(name, em);
            String abbr = name + number;

            ServiceProvider saveServiceProvider = WmsServerAction.saveServiceProvider(wmsUrl + getCap, null,
                    name, abbr, em);
            Organization org = (Organization) em.createQuery("FROM Organization WHERE name = :name")
                    .setParameter("name", organization).getSingleResult();
            WmsServerAction.addAllLayersToGroup(org, saveServiceProvider, em);

            tx.commit();
        } catch (Exception ex) {
            tx.rollback();
            log.error("Kan nieuwe server niet posten", ex);
        }
    } catch (Throwable e) {
        log.error("Exception occured while getting EntityManager: ", e);
    } finally {
        log.debug("Closing entity manager .....");
        MyEMFDatabase.closeEntityManager(identity, MyEMFDatabase.MAIN_EM);
    }
}

From source file:name.livitski.tools.persista.schema.example.CustomerCRUDTest.java

@Test
public void testUpdateDelete() {
    assertNotNull("stored object must have a positive id, got null", storedRecordId);
    final Log log = log();
    final EntityManager db = getEntityManager();
    final EntityTransaction txn = db.getTransaction();
    txn.begin();
    Customer customer = db.find(Customer.class, storedRecordId);
    assertNotNull("cannot find record of type " + Customer.class + " with id=" + storedRecordId, customer);
    customer.setName(UPDATED_NAME);/*  ww  w.j ava 2s . c  om*/
    customer.setAddress(UPDATED_ADDRESS);
    customer.setPhone(UPDATED_PHONE);
    customer.setEmail(UPDATED_EMAIL);
    txn.commit();
    log.info("Updated customer record with id=" + storedRecordId);
    expected = new String[] { UPDATED_NAME, UPDATED_ADDRESS, UPDATED_PHONE, UPDATED_EMAIL };
    testRead();
    txn.begin();
    db.refresh(customer);
    db.remove(customer);
    txn.commit();
    log.info("Deleted customer record with id=" + storedRecordId);
    customer = db.find(Customer.class, storedRecordId);
    assertNull("record of type " + Customer.class + " with id=" + storedRecordId + " was not deleted",
            customer);
    storedRecordId = null;
}

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();
    try {/*from   w  w  w.j a va  2 s . c  o  m*/
        mgr.persist(vessel);
        xa.commit();
        success = true;
    } catch (Exception ignoreMe) {
        rollbackQuietly(xa);
    } finally {
        mgr.close();
    }
    return success;
}

From source file:de.peterspan.csv2db.converter.location.LocationConverter.java

@Override
protected Void doInBackground() throws Exception {
    EntityTransaction tx = null;
    try {//  ww  w  .  j  a  v a  2s .  co  m
        tx = entityManager.getTransaction();
        tx.begin();
        List<String[]> allLines = readFile();

        double increment = 100.0 / allLines.size();
        double progress = 0.0;
        // Removing the header
        // Remove the empty line
        for (String[] line : allLines) {
            progress = progress + increment;
            setProgress((int) Math.round(progress));
            if (line[0].equals("Standort-Nr.")) {
                continue;
            }
            if (line[0].equals("")) {
                continue;
            }
            readLine(line);

        }

        entityManager.flush();
        tx.commit();
    } catch (HibernateException he) {
        if (tx != null) {
            tx.rollback();
        }
    }

    return null;
}

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

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

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

@Transactional
public void deleteAllCartItems(Long userId) {
    EntityManager em = getEntityManager();
    EntityTransaction txn = em.getTransaction();
    txn.begin();
    Query q = em.createQuery("DELETE FROM Cart c where c.user.id=:id");
    q.setParameter("id", userId);
    q.executeUpdate();/*from   w w  w .  j a  v  a2  s. c  o m*/
    txn.commit();
}

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

/**
 * The method to update the serailizable business objects into the database.
 * //from  w w  w  . java  2s .co  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();
        }
    }
}