Example usage for org.hibernate StatelessSession close

List of usage examples for org.hibernate StatelessSession close

Introduction

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

Prototype

void close();

Source Link

Document

Close the stateless session and release the JDBC connection.

Usage

From source file:kr.debop4j.data.hibernate.tools.StatelessTool.java

License:Apache License

/**
 * Execute transactional.//w  w  w. j a  v a  2  s  . c  om
 *
 * @param session the session
 * @param actions the actions
 */
public static void executeTransactional(Session session, Iterable<Action1<StatelessSession>> actions) {
    if (log.isDebugEnabled())
        log.debug("StatelessSession? ? Transaction ?  ? .");

    StatelessSession stateless = openStatelessSession(session);
    Transaction tx = null;

    try {
        tx = stateless.beginTransaction();
        for (Action1<StatelessSession> action : actions) {
            action.perform(stateless);
        }
        tx.commit();
    } catch (Exception e) {
        log.error("StatelessSession? ? . rollback .", e);
        if (tx != null)
            tx.rollback();
        throw new RuntimeException(e);
    } finally {
        stateless.close();
    }
}

From source file:kr.debop4j.data.hibernate.tools.StatelessTool.java

License:Apache License

/**
 * Execute the action./*from ww  w  .j  a  v  a  2  s. c om*/
 *
 * @param session the session
 * @param action  the action
 */
public static void execute(Session session, Action1<StatelessSession> action) {
    if (log.isDebugEnabled())
        log.debug("StatelessSession? ?  ? .");

    StatelessSession stateless = openStatelessSession(session);
    try {
        action.perform(stateless);
    } catch (Exception e) {
        log.error("StatelessSession? ? .", e);
        throw new RuntimeException(e);
    } finally {
        stateless.close();
    }
}

From source file:kr.debop4j.data.hibernate.tools.StatelessTool.java

License:Apache License

/**
 * Execute the actions./*ww  w. j a  va  2  s .c  om*/
 *
 * @param session the session
 * @param actions the actions
 */
public static void execute(Session session, Iterable<Action1<StatelessSession>> actions) {
    if (log.isDebugEnabled())
        log.debug("StatelessSession? ?  ? .");

    StatelessSession stateless = openStatelessSession(session);
    try {
        for (Action1<StatelessSession> action : actions)
            action.perform(stateless);
    } catch (Exception e) {
        log.error("StatelessSession? ? .", e);
        throw new RuntimeException(e);
    } finally {
        stateless.close();
    }
}

From source file:magoffin.matt.dao.hbm.GenericHibernateDao.java

License:Open Source License

/**
 * Execute a batch callback against a StatelessSession using a named query.
 * /*from   w ww .  ja v  a2 s.c om*/
 * <p>The DELETE, UPDATE, and UPDATE_STOP {@link BatchCallbackResult}
 * values are not supported in this operation, and will throw an 
 * <code>UnsupportedOperationException</code> if returned by the 
 * {@link BatchCallback} instance passed to this method.</p>
 * 
 * @param criteriaBuilder the criteria builder
 * @param callback the callback
 * @param options the options
 * @return the number of items processed
 */
@SuppressWarnings("unchecked")
protected Integer executeStatelessCriteriaBatchCallback(final CriteriaBuilder criteriaBuilder,
        final BatchCallback<T> callback, final BatchOptions options) {
    StatelessSession session = getHibernateTemplate().getSessionFactory().openStatelessSession();
    Transaction tx = session.beginTransaction();
    try {
        Criteria criteria = session.createCriteria(getType());
        criteria.setFetchSize(options.getBatchSize());
        criteriaBuilder.buildCriteria(criteria);
        ScrollableResults items = criteria.scroll(ScrollMode.FORWARD_ONLY);
        int count = 0;

        OUTER: while (items.next()) {
            T item = (T) items.get(0);
            BatchCallbackResult action = callback.handle(item);
            switch (action) {
            case DELETE:
            case UPDATE:
            case UPDATE_STOP:
                throw new UnsupportedOperationException("Action " + action + " not possible during "
                        + options.getMode() + " mode batch processing");

            case STOP:
                break OUTER;

            case CONTINUE:
                // nothing to do
                break;
            }
        }
        tx.commit();
        return count;
    } catch (RuntimeException e) {
        tx.rollback();
        throw e;
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:mitm.common.hibernate.HibernateUtils.java

License:Open Source License

/**
 * Returns true if the database of the sessionSource is active.
 *//*from ww  w.  j  ava  2 s .  c  om*/
public static boolean isDatabaseActive(SessionManager sessionManager, String testQuery) {
    boolean active = false;

    try {
        StatelessSession session = null;

        try {
            session = sessionManager.openStatelessSession();

            SQLQuery query = session.createSQLQuery(testQuery);

            query.list();
        } finally {
            session.close();
        }

        active = true;
    } catch (RuntimeException e) {
        logger.warn("Unable to open a stateless session. Database is probably not active.");
    }

    return active;
}

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 {// ww w . ja  va2 s.co  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 ww  . j a  v a2 s.com*/
        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 {/*from ww  w .j a  v  a2s  .c o  m*/
        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 {//from w  ww . j a  va2s  .  c om
        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:monasca.api.infrastructure.persistence.hibernate.AlarmDefinitionSqlRepoImpl.java

License:Apache License

@Override
public String exists(final String tenantId, final String name) {
    logger.trace(ORM_LOG_MARKER, "exists(...) entering...");

    StatelessSession session = null;
    try {/*from  w w w  .ja  v a  2  s  .c o  m*/
        session = sessionFactory.openStatelessSession();

        List<?> ids = session.createCriteria(AlarmDefinitionDb.class).add(Restrictions.eq("tenantId", tenantId))
                .add(Restrictions.eq("name", name)).add(Restrictions.isNull("deletedAt"))
                .setProjection(Projections.property("id")).setMaxResults(1).list();

        final String existingId = CollectionUtils.isEmpty(ids) ? null : (String) ids.get(0);

        if (null == existingId) {
            logger.debug(ORM_LOG_MARKER, "No AlarmDefinition matched tenantId={} and name={}", tenantId, name);
        }

        return existingId;
    } finally {
        if (session != null) {
            session.close();
        }
    }
}