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:org.jahia.services.content.impl.external.ExternalSessionImpl.java

License:Open Source License

public Node getNodeByUUID(String uuid) throws ItemNotFoundException, RepositoryException {
    if (!repository.getDataSource().isSupportsUuid() || uuid.startsWith("translation:")) {
        // Translate uuid to external mapping
        SessionFactory hibernateSession = repository.getStoreProvider().getHibernateSession();
        StatelessSession statelessSession = hibernateSession.openStatelessSession();
        try {/*  w  ww .  ja  va2s. c  o m*/
            Criteria criteria = statelessSession.createCriteria(UuidMapping.class);
            criteria.add(Restrictions.eq("internalUuid", uuid));
            List list = criteria.list();
            if (list.size() > 0) {
                uuid = ((UuidMapping) list.get(0)).getExternalId();
            } else {
                throw new ItemNotFoundException("Item " + uuid + " could not been found in this repository");
            }
        } finally {
            statelessSession.close();
        }
    }
    return getNodeByLocalIdentifier(uuid);
}

From source file:org.jahia.services.content.nodetypes.NodeTypesDBServiceImpl.java

License:Open Source License

public String readFile(String filename) throws RepositoryException {
    StatelessSession session = null;
    try {//from ww  w. ja  v  a 2 s . c  om
        session = getHibernateSessionFactory().openStatelessSession();
        session.beginTransaction();
        NodeTypesDBProvider nodeTypesDBProvider = (NodeTypesDBProvider) session
                .createQuery("from NodeTypesDBProvider where filename=:filename")
                .setString("filename", filename).setReadOnly(true).uniqueResult();
        session.getTransaction().commit();
        if (nodeTypesDBProvider != null) {
            return nodeTypesDBProvider.getCndFile();
        }
    } catch (Exception e) {
        if (session != null) {
            session.getTransaction().rollback();
        }
        throw new RepositoryException(e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return null;
}

From source file:org.jahia.services.content.nodetypes.NodeTypesDBServiceImpl.java

License:Open Source License

public List<String> getFilesList() throws RepositoryException {
    StatelessSession session = null;
    try {/*from   w  w w  .  ja va  2 s  .com*/
        session = getHibernateSessionFactory().openStatelessSession();
        session.beginTransaction();
        List<String> nodeTypesDBProviderList = session
                .createQuery("select filename from NodeTypesDBProvider order by id").setReadOnly(true).list();
        session.getTransaction().commit();
        if (nodeTypesDBProviderList != null) {
            return nodeTypesDBProviderList;
        }
    } catch (Exception e) {
        if (session != null) {
            session.getTransaction().rollback();
        }
        throw new RepositoryException(e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return null;
}

From source file:org.jahia.services.content.nodetypes.NodeTypesDBServiceImpl.java

License:Open Source License

public void saveCndFile(String filename, String content, Properties properties) throws RepositoryException {
    StatelessSession session = null;
    try {/* w ww  .j  a  v  a 2s.c o m*/
        session = getHibernateSessionFactory().openStatelessSession();
        session.beginTransaction();
        NodeTypesDBProvider nodeTypesDBProvider = (NodeTypesDBProvider) session
                .createQuery("from NodeTypesDBProvider where filename=:filename")
                .setString("filename", filename).setReadOnly(false).uniqueResult();
        if (nodeTypesDBProvider != null && content != null) {
            nodeTypesDBProvider.setCndFile(content);
            session.update(nodeTypesDBProvider);
        } else if (nodeTypesDBProvider != null) {
            session.delete(nodeTypesDBProvider);
        } else if (content != null) {
            nodeTypesDBProvider = new NodeTypesDBProvider();
            nodeTypesDBProvider.setFilename(filename);
            nodeTypesDBProvider.setCndFile(content);
            session.insert(nodeTypesDBProvider);
        }

        final StringWriter writer = new StringWriter();
        properties.store(writer, "");

        nodeTypesDBProvider = (NodeTypesDBProvider) session
                .createQuery("from NodeTypesDBProvider where filename=:filename")
                .setString("filename", DEFINITIONS_PROPERTIES).setReadOnly(false).uniqueResult();
        if (nodeTypesDBProvider != null) {
            nodeTypesDBProvider.setCndFile(writer.toString());
            session.update(nodeTypesDBProvider);
        } else {
            nodeTypesDBProvider = new NodeTypesDBProvider();
            nodeTypesDBProvider.setFilename(DEFINITIONS_PROPERTIES);
            nodeTypesDBProvider.setCndFile(writer.toString());
            session.insert(nodeTypesDBProvider);
        }

        session.getTransaction().commit();
    } catch (Exception e) {
        if (session != null) {
            session.getTransaction().rollback();
        }
        throw new RepositoryException(e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:org.jasig.ssp.dao.DirectoryPersonSearchDao.java

License:Apache License

public void exportableSearch(CaseloadCsvWriterHelper csvWriterHelper, PersonSearchRequest personSearchRequest)
        throws IOException {

    StatelessSession openStatelessSession = null;

    try {/*w  w  w  .j  ava 2 s . c o  m*/
        openStatelessSession = sessionFactory.openStatelessSession();
        Pair<Long, Query> querySet = prepSearchQuery(openStatelessSession, personSearchRequest);

        querySet.getSecond().setResultTransformer(
                new NamespacedAliasToBeanResultTransformer(PersonSearchResult2.class, "person_"));
        Query query = querySet.getSecond().setFetchSize(10).setReadOnly(true);
        ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);

        csvWriterHelper.write(results, -1L);
    } finally {
        if (openStatelessSession != null) {
            try {
                openStatelessSession.close();
            } catch (Exception e) {
                // nothing to do and likely harmless
                LOGGER.info("Failed to close Hibernate StatelessSession", e);
            }
        }
    }

}

From source file:org.jboss.seam.wiki.core.nestedset.listener.NestedSetOperation.java

License:LGPL

public void execute(EventSource session) {
    StatelessSession ss = null;
    Connection jdbcConnection = null;
    try {//from w  w w.  ja v  a2s .c  o  m
        jdbcConnection = getConnection(session);
        log.trace("opening new session on existing connection");
        ss = session.getSessionFactory().openStatelessSession(jdbcConnection);

        beforeExecution();
        executeOnDatabase(ss);

        // Find all NestedSetNode instances in the persistence context
        Collection<NestedSetNode> nodesInPersistenceContext = new HashSet<NestedSetNode>();
        Iterator contextIterator = new LazyIterator(session.getPersistenceContext().getEntitiesByKey());
        while (contextIterator.hasNext()) {
            Object o = contextIterator.next();
            if (NestedSetNode.class.isAssignableFrom(o.getClass()))
                nodesInPersistenceContext.add((NestedSetNode) o);
        }

        executeInMemory(nodesInPersistenceContext);
        afterExecution();

    } catch (Exception ex) {
        log.error(ex);
        throw new RuntimeException(ex);
    } finally {
        if (ss != null) {
            try {
                log.trace("closing session and connection");
                jdbcConnection.close();
                ss.close();
            } catch (SQLException ex) {
                log.error("could not close connection or session", ex);
            }
        }
    }
}

From source file:org.ng200.openolympus.services.TaskService.java

License:Open Source License

@PreAuthorize(SecurityExpressionConstants.IS_ADMIN)
@Transactional/*from   w  w w .java 2  s.c o m*/
public void rejudgeTask(final Task task) throws ExecutionException, IOException {

    final Lock lock = task.writeLock();
    lock.lock();

    try {

        this.verdictRepository.flush();
        this.solutionRepository.flush();

        this.verdictRepository.deleteBySolutionTask(task);
        this.verdictRepository.flush();

        final StatelessSession session = ((Session) this.entityManager.getDelegate()).getSessionFactory()
                .openStatelessSession();
        try {
            final Query query = session.createQuery("from Solution where task_id=:taskID");
            query.setParameter("taskID", task.getId());
            query.setFetchSize(Integer.valueOf(1000));
            query.setReadOnly(false);
            final ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);
            try {
                while (results.next()) {
                    final Solution solution = (Solution) results.get(0);
                    this.testingService.testSolutionOnAllTests(solution);
                }
            } finally {
                results.close();
            }
        } finally {
            session.close();
        }
    } finally {
        lock.unlock();
    }
}

From source file:org.openremote.beehive.api.service.impl.GenericDAO.java

License:Open Source License

/**
 * Closes the StatelessSession/*from w  ww.j  a  va  2s.com*/
 * 
 * @param session
 *           the session to close
 */
private void closeStatelessSession(StatelessSession session) {
    if (session != null) {
        logger.debug("Closing Hibernate StatelessSession");
        try {
            session.close();
        } catch (HibernateException ex) {
            logger.warn("Could not close Hibernate StatelessSession", ex);
        } catch (Throwable ex) {
            logger.warn("Unexpected exception on closing Hibernate StatelessSession", ex);
        }
    }
}

From source file:org.openspaces.persistency.hibernate.StatelessHibernateExternalDataSource.java

License:Open Source License

private void closeSession(StatelessSession session) {
    session.close();
}

From source file:org.ow2.proactive.db.HibernateDatabaseManager.java

License:Open Source License

protected void closeSession(StatelessSession session) {
    try {//from ww  w.  j a v  a  2 s . c om
        session.close();
    } catch (Exception e) {
        getLogger().error("Error while closing session", e);
        this.exceptionHandler.handle("Error while closing session", e);
    }
}