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:nl.b3p.kaartenbalie.service.MapFileListener.java

private void saveServiceProvider(String wmsUrl, String name) {
    Object identity = null;/*  www . j  a va2s.c o  m*/
    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:org.lamop.riche.dao.DAOGenericImpl.java

private void finallyCloseEmTransaction(EntityManager em) {
    if (em != null && em.getTransaction().isActive()) {
        em.getTransaction().rollback();//from w ww.j  a v a2s. com
    }

}

From source file:eu.optimis.trustedinstance.DBStorage.java

@SuppressWarnings("finally")
public boolean update(DBStorageEntry entry) {

    boolean result = true;
    EntityManager em = emf.createEntityManager();

    try {//from w ww .ja va2s . com
        em.getTransaction().begin();
        DBStorageEntry update = em.find(DBStorageEntry.class, entry.getKey());
        update.setLicenseToken(entry.getLicenseToken());
        em.getTransaction().commit();
    } catch (Exception e) {
        result = false;
    } finally {
        em.close();
        return result;
    }
}

From source file:eu.optimis.trustedinstance.DBStorage.java

@SuppressWarnings("finally")
public DBStorageEntry get(String resourceKey) throws Exception {

    DBStorageEntry result = null;/*from ww  w  .j  av a2 s  .c o  m*/
    EntityManager em = emf.createEntityManager();

    try {
        em.getTransaction().begin();
        result = (DBStorageEntry) em.find(DBStorageEntry.class, resourceKey);
        em.getTransaction().commit();
    } catch (Exception e) {
        result = null;
        throw e;
    } finally {
        em.close();
        return result;
    }
}

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

@Override
public Principal getAuthenticatedPrincipal(String username, String password) {
    Object identity = null;/*from   www .jav  a 2s . c o  m*/
    EntityTransaction tx = null;
    try {
        identity = MyEMFDatabase.createEntityManager(MyEMFDatabase.REALM_EM);
        EntityManager em = MyEMFDatabase.getEntityManager(MyEMFDatabase.REALM_EM);
        tx = em.getTransaction();
        tx.begin();
        try {
            User user = (User) em.createQuery("from User u where " + "lower(u.username) = lower(:username) ")
                    .setParameter("username", username).getSingleResult();
            return user;
        } catch (NoResultException nre) {
            return null;
        }
    } catch (Exception e) {
        log.error("Exception getting authenticated user from database", e);
        if (tx != null && tx.isActive()) {
            tx.rollback();
        }
    } finally {
        if (tx != null && tx.isActive() && !tx.getRollbackOnly()) {
            tx.commit();
        }
        MyEMFDatabase.closeEntityManager(identity, MyEMFDatabase.REALM_EM);
    }
    return null;
}

From source file:com.doculibre.constellio.wicket.pages.SQLPage.java

public SQLPage() {
    super();/*from  ww  w  . ja  va  2s.  c o  m*/
    ConstellioSession session = ConstellioSession.get();
    ConstellioUser user = session.getUser();
    boolean redirect;
    if (user == null) {
        redirect = true;
    } else if (user.isAdmin()) {
        redirect = false;
    } else {
        redirect = true;
        RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices();
        for (RecordCollection collection : collectionServices.list()) {
            if (user.hasCollaborationPermission(collection) || user.hasAdminPermission(collection)) {
                redirect = false;
                break;
            }
        }
    }
    if (redirect) {
        setResponsePage(getApplication().getHomePage());
    } else {
        form = new Form("form");
        feedbackPanel = new FeedbackPanel("feedback");
        textArea = new TextArea("query", queryModel);
        submitButton = new Button("submitButton") {
            @Override
            public void onSubmit() {
                String sql = (String) queryModel.getObject();
                if (StringUtils.isNotBlank(sql)) {
                    EntityManager entityManager = ConstellioPersistenceContext.getCurrentEntityManager();
                    if (!entityManager.getTransaction().isActive()) {
                        entityManager.getTransaction().begin();
                    }
                    Query sqlQuery = entityManager.createNativeQuery(sql);
                    try {
                        int rowCount = sqlQuery.executeUpdate();
                        entityManager.getTransaction().commit();
                        info(rowCount + " " + getLocalizer().getString("affectedRows", this));
                    } catch (Exception e) {
                        String stack = ExceptionUtils.getFullStackTrace(e);
                        error(stack);
                    }
                }
            }
        };

        add(form);
        form.add(feedbackPanel);
        form.add(textArea);
        form.add(submitButton);
    }
}

From source file:hd.controller.AddImageToProjectServlet.java

public void persist(Object object) {
    EntityManager em = emf.createEntityManager();
    try {/*from  ww w  .j  av  a 2  s.  c  o  m*/
        em.getTransaction().begin();
        em.persist(object);
        em.getTransaction().commit();
    } catch (Exception e) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
        em.getTransaction().rollback();
    } finally {
        em.close();
    }
}

From source file:pl.datamatica.traccar.api.fcm.Daemon.java

protected Daemon() {
    runnable = new Runnable() {
        @Override//from  w w  w  . j a v a 2s  .  c om
        public void run() {
            EntityManager em = Context.getInstance().createEntityManager();
            try {
                em.getTransaction().begin();
                doWork(em);
                em.getTransaction().commit();
            } catch (Exception e) {
                Logger.getLogger(Daemon.class.getName()).log(Level.SEVERE, null, e);
            } finally {
                em.close();
            }
        }
    };
}

From source file:eu.optimis.trustedinstance.DBStorage.java

@SuppressWarnings("finally")
public boolean store(DBStorageEntry entry) throws Exception {

    if (update(entry)) {
        return true;
    }/*from   www  .ja  v  a  2s.  c  om*/

    boolean result = true;
    EntityManager em = emf.createEntityManager();

    try {
        em.getTransaction().begin();
        em.persist(entry);
        em.getTransaction().commit();
    } catch (Exception e) {
        result = false;
        throw e;
    } finally {
        em.close();
        return result;
    }
}

From source file:com.doculibre.constellio.wicket.panels.admin.synonyms.AddEditSynonymsPanel.java

@Override
protected void onSave(AjaxRequestTarget target) {
    AdminCollectionPanel collectionAdminPanel = (AdminCollectionPanel) findParent(AdminCollectionPanel.class);
    RecordCollection collection = collectionAdminPanel.getCollection();

    SynonymList synonymList = synonymListModel.getObject();
    synonymList.setRecordCollection(collection);

    SynonymServices synonymServices = ConstellioSpringUtils.getSynonymServices();
    EntityManager entityManager = ConstellioPersistenceContext.getCurrentEntityManager();
    if (!entityManager.getTransaction().isActive()) {
        entityManager.getTransaction().begin();
    }/*from  www.ja v  a 2s.c om*/
    synonymServices.makePersistent(synonymList);
    entityManager.getTransaction().commit();

    if (SolrServices.synonymsFilterActivated) {
        //necessaire si : utilisation d un autre filtre des synonymes
        synonymServices.writeSynonymsFile(collection);
        //relancer la lecture des synonymes
        SolrServices solrServices = ConstellioSpringUtils.getSolrServices();
        solrServices.initCore(collection);
    }

}