Example usage for org.hibernate StatelessSession beginTransaction

List of usage examples for org.hibernate StatelessSession beginTransaction

Introduction

In this page you can find the example usage for org.hibernate StatelessSession beginTransaction.

Prototype

Transaction beginTransaction();

Source Link

Document

Begin a unit of work and return the associated Transaction object.

Usage

From source file:mitm.application.djigzo.impl.hibernate.UserPreferencesManagerHibernate.java

License:Open Source License

@Override
public CloseableIterator<String> getNameIterator(Integer firstResult, Integer maxResults,
        SortDirection sortDirection) {//from w ww .  j a  v  a2s . c o m
    StatelessSession statelessSession = sessionManager.openStatelessSession();

    Transaction tx = statelessSession.beginTransaction();

    CloseableIterator<String> iterator = getStatelessDAO(statelessSession).getPreferencesIterator(category,
            firstResult, maxResults, sortDirection);

    CloseableIterator<String> commitOnCloseIterator = new CommitOnCloseIterator<String>(iterator,
            SessionAdapterFactory.create(statelessSession), tx);

    return commitOnCloseIterator;
}

From source file:mitm.common.security.certstore.hibernate.X509CertStoreExtHibernate.java

License:Open Source License

public Collection<X509Certificate> getCertificates(CertSelector certSelector, MissingKeyAlias missingKeyAlias,
        Integer firstResult, Integer maxResults) throws CertStoreException {
    StatelessSession statelessSession = sessionManager.openStatelessSession();

    Transaction tx = statelessSession.beginTransaction();

    try {/*from  www.  j a v a 2  s . c  o  m*/
        Collection<X509Certificate> certificates = getStatelessDAO(statelessSession)
                .getCertificates(certSelector, missingKeyAlias, firstResult, maxResults);

        tx.commit();

        return certificates;
    } catch (CertStoreException e) {
        tx.rollback();

        throw e;
    } finally {
        statelessSession.close();
    }
}

From source file:mitm.common.security.certstore.hibernate.X509CertStoreExtHibernate.java

License:Open Source License

public CloseableIterator<X509Certificate> getCertificateIterator(CertSelector certSelector,
        MissingKeyAlias missingKeyAlias, Integer firstResult, Integer maxResults) throws CertStoreException {
    StatelessSession statelessSession = sessionManager.openStatelessSession();

    Transaction tx = statelessSession.beginTransaction();

    try {//from   w w w .  j  a  v a  2 s.  c  om
        CloseableIterator<X509Certificate> iterator = getStatelessDAO(statelessSession)
                .getCertificateIterator(certSelector, missingKeyAlias, firstResult, maxResults);

        CloseableIterator<X509Certificate> commitOnCloseIterator = new CommitOnCloseIterator<X509Certificate>(
                iterator, SessionAdapterFactory.create(statelessSession), tx);

        return commitOnCloseIterator;
    } catch (CertStoreException e) {
        try {
            tx.rollback();
        } finally {
            statelessSession.close();
        }

        throw e;
    }
}

From source file:mitm.common.security.crlstore.hibernate.X509CRLStoreExtHibernate.java

License:Open Source License

@Override
public Collection<X509CRL> getCRLs(CRLSelector crlSelector, Integer firstResult, Integer maxResults)
        throws CRLStoreException {
    StatelessSession statelessSession = sessionManager.openStatelessSession();

    Transaction tx = statelessSession.beginTransaction();

    try {/*ww w  .  j a va 2  s. c  om*/
        Collection<X509CRL> crls = getStatelessDAO(statelessSession).getCRLs(crlSelector, firstResult,
                maxResults);

        tx.commit();

        return crls;
    } catch (CRLStoreException e) {
        tx.rollback();

        throw e;
    } finally {
        statelessSession.close();
    }
}

From source file:mitm.common.security.crlstore.hibernate.X509CRLStoreExtHibernate.java

License:Open Source License

@Override
public CloseableIterator<X509CRL> getCRLIterator(CRLSelector crlSelector, Integer firstResult,
        Integer maxResults) throws CRLStoreException {
    StatelessSession statelessSession = sessionManager.openStatelessSession();

    Transaction tx = statelessSession.beginTransaction();

    try {/*w ww .j  a v a 2  s  .  c o m*/
        CloseableIterator<X509CRL> iterator = getStatelessDAO(statelessSession).getCRLIterator(crlSelector,
                firstResult, maxResults);

        CloseableIterator<X509CRL> commitOnCloseIterator = new CommitOnCloseIterator<X509CRL>(iterator,
                SessionAdapterFactory.create(statelessSession), tx);

        return commitOnCloseIterator;
    } catch (CRLStoreException e) {
        try {
            tx.rollback();
        } finally {
            statelessSession.close();
        }

        throw e;
    }
}

From source file:org.geolatte.demo1.services.RiverService.java

License:Open Source License

@Path("/cities")
@GET/*from   ww  w  .j  a  v  a2 s. c  o m*/
@Produces(MediaType.APPLICATION_JSON)
public List<PlaceTo> getEndangeredCities(@QueryParam("x") final float x, @QueryParam("y") final float y) {

    StatelessSession session = HibernateUtil.getSessionFactory().openStatelessSession();
    try {
        // Begin unit of work
        session.beginTransaction();

        SimpleTransformerSink<PlaceTo> sink = new SimpleTransformerSink<PlaceTo>();

        ClosedTransformerChain chain = TransformerChainFactory.<Geometry, PlaceTo>newChain()
                .add(new RiverSegmentSource(x, y, session)) // <Geometry>
                .add(new Buffer()) // <Geometry> -> <Geometry>
                .add(new GetCitiesWithinBounds(session)) // <Geometry> -> [<Place>]
                .addFilter(new FilterDuplicates<Place>()) // <Place>    -> <Place>
                .add(new PlaceToTransferObject()) // <Place>    -> <PlaceTo>
                .last(sink); // collect

        chain.addTransformerEventListener(new TransformerEventListener() {
            @Override
            public void ErrorOccurred(TransformerErrorEvent event) {
                // Log errors here
            }
        });

        chain.run();

        List<PlaceTo> endangeredPlaces = sink.getCollectedOutput();

        session.getTransaction().commit();

        return endangeredPlaces;
    } catch (Exception ex) {
        HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
    } finally {
        session.close();
    }

    return Collections.emptyList();
}

From source file:org.jahia.modules.external.id.ExternalProviderInitializerServiceImpl.java

License:Open Source License

@Override
public void delete(List<String> externalIds, String providerKey, boolean includeDescendants)
        throws RepositoryException {
    if (externalIds.isEmpty()) {
        return;/*from   w w w.  j  a  va2  s .com*/
    }
    StatelessSession session = null;
    try {
        List<Integer> hashes = new LinkedList<Integer>();
        for (String externalId : externalIds) {
            int hash = externalId.hashCode();
            hashes.add(hash);
        }
        session = hibernateSessionFactory.openStatelessSession();
        session.beginTransaction();

        // delete all
        session.createQuery(
                "delete from UuidMapping where providerKey=:providerKey and externalIdHash in (:externalIds)")
                .setString("providerKey", providerKey).setParameterList("externalIds", hashes).executeUpdate();

        if (includeDescendants) {
            // delete descendants
            Query selectStmt = session
                    .createQuery(
                            "from UuidMapping where providerKey=:providerKey and externalId like :externalId")
                    .setString("providerKey", providerKey);
            for (String externalId : externalIds) {
                selectStmt.setString("externalId", externalId + "/%");
                List<?> descendants = selectStmt.list();

                for (Object mapping : descendants) {
                    UuidMapping m = (UuidMapping) mapping;
                    session.delete(m);
                    invalidateCache(m.getExternalIdHash(), providerKey);
                }
            }
        }

        session.getTransaction().commit();

        for (String externalId : externalIds) {
            int hash = externalId.hashCode();
            invalidateCache(hash, providerKey);
        }
    } catch (Exception e) {
        if (session != null) {
            session.getTransaction().rollback();
        }
        throw new RepositoryException(e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:org.jahia.modules.external.id.ExternalProviderInitializerServiceImpl.java

License:Open Source License

@Override
public String getExternalIdentifier(String internalId) throws RepositoryException {
    String externalId = null;//from w ww  .j  a va 2s  .  c o  m
    StatelessSession session = null;
    try {
        session = getHibernateSessionFactory().openStatelessSession();
        session.beginTransaction();
        UuidMapping mapping = (UuidMapping) session.get(UuidMapping.class, internalId);
        if (mapping != null) {
            externalId = mapping.getExternalId();
        }
        session.getTransaction().commit();
    } catch (Exception e) {
        if (session != null) {
            session.getTransaction().rollback();
        }
        throw new RepositoryException(e);
    } finally {
        if (session != null) {
            session.close();
        }
    }

    return externalId;
}

From source file:org.jahia.modules.external.id.ExternalProviderInitializerServiceImpl.java

License:Open Source License

@Override
public String getInternalIdentifier(String externalId, String providerKey) throws RepositoryException {
    int hash = externalId.hashCode();
    String cacheKey = getCacheKey(hash, providerKey);
    String uuid = getIdentifierCache().get(cacheKey) != null
            ? (String) getIdentifierCache().get(cacheKey).getObjectValue()
            : null;/*w  w  w .j a v  a  2  s .com*/
    if (uuid == null) {
        StatelessSession session = null;
        try {
            session = getHibernateSessionFactory().openStatelessSession();
            session.beginTransaction();
            List<?> list = session
                    .createQuery("from UuidMapping where providerKey=:providerKey and externalIdHash=:idHash")
                    .setString("providerKey", providerKey).setLong("idHash", hash).setReadOnly(true).list();
            if (list.size() > 0) {
                uuid = ((UuidMapping) list.get(0)).getInternalUuid();
                getIdentifierCache().put(new Element(cacheKey, uuid, true));
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session != null) {
                session.getTransaction().rollback();
            }
            throw new RepositoryException(e);
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    return uuid;
}

From source file:org.jahia.modules.external.id.ExternalProviderInitializerServiceImpl.java

License:Open Source License

@Override
public void removeProvider(String providerKey) throws RepositoryException {
    SessionFactory hibernateSession = getHibernateSessionFactory();
    StatelessSession session = null;
    try {/*from ww  w .  jav a  2 s  .  c om*/
        session = hibernateSession.openStatelessSession();
        session.beginTransaction();
        int deletedCount = session.createQuery("delete from ExternalProviderID where providerKey=:providerKey")
                .setString("providerKey", providerKey).executeUpdate();
        if (deletedCount > 0) {
            logger.info("Deleted external provider entry for key {}", providerKey);
            deletedCount = session.createQuery("delete from UuidMapping where providerKey=:providerKey")
                    .setString("providerKey", providerKey).executeUpdate();
            logger.info("Deleted {} identifier mapping entries for external provider with key {}", deletedCount,
                    providerKey);
        } else {
            logger.info("No external provider entry found for key {}", providerKey);
        }
        session.getTransaction().commit();
    } catch (Exception e) {
        if (session != null) {
            session.getTransaction().rollback();
        }
        throw new RepositoryException(
                "Issue when removing external provider entry and identifier mappings for provider key "
                        + providerKey,
                e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
}