Example usage for org.hibernate Session disconnect

List of usage examples for org.hibernate Session disconnect

Introduction

In this page you can find the example usage for org.hibernate Session disconnect.

Prototype

Connection disconnect();

Source Link

Document

Disconnect the session from its underlying JDBC connection.

Usage

From source file:org.hl7.hibernate.Persistence.java

License:Open Source License

public void close() {
    Session session = getSession();
    session.disconnect();
    session.close();/*from  w ww.  ja v a  2 s.  c  om*/
    _session = null;

    if (_connection != null) {
        returnConnection(_connection);
        _connection = null;
    }

    MergerUsingCache.clearObjectCache();
    MergerUsingCache.clearQueryCache();
}

From source file:org.kitodo.data.database.persistence.HibernateUtilOld.java

License:Open Source License

/**
 * Disconnect and return Session from current Thread.
 *
 * @return Session the disconnected Session
 *//*from   www. j  a  v  a  2  s . c  om*/
public static Session disconnectSession() throws InfrastructureException {

    Session session = getSession();
    try {
        threadSession.set(null);
        if (session.isConnected() && session.isOpen()) {
            session.disconnect();
        }
    } catch (HibernateException ex) {
        throw new InfrastructureException(ex);
    }
    return session;
}

From source file:org.life.sl.graphs.PathSegmentGraph.java

License:Open Source License

/**
 * Create a new graph, with linestrings read from the database (optionally using only a buffer around a given track for the network)
 * @param track GPS track consisting of a list of data points
 * @param bufferSize size of the buffer to select around the track
 * @param dumpFile if not empty, the path of a shapefile to dump the network into.
 *///from  w ww.  ja va2 s .c o m
public void addLineStringsFromDatabase(ArrayList<Point> track, float bufferSize, String dumpFile) {
    setLineMergeGraphH4cked(new LineMergeGraphH4cked());
    distancesCalculated = false;

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();

    //Query gpsResults = session.createQuery("from" );

    if (track == null) {
        Query result = session.createQuery("from OSMEdge");

        @SuppressWarnings("unchecked")
        Iterator<OSMEdge> iter = result.iterate();
        while (iter.hasNext()) {
            OSMEdge o = iter.next();
            LineString g = o.getGeometry();
            addLineString(g, o.getId(), o.getEnvtype(), o.getCyktype(), o.getGroenm());
            // let us add this thing to the spatial index
            addOSMEdgeToSpatialIndex(o);

        }
    } else {
        // let us join the nodes of the track to a linestring ....

        Iterator<Point> trackIter = track.iterator();
        Coordinate[] coords = new Coordinate[track.size()];
        int i = 0;
        while (trackIter.hasNext()) {
            Point p = trackIter.next();
            coords[i] = p.getCoordinate();
            i++;
        }
        if (i > 1) {
            LineString l = fact.createLineString(coords);

            Criteria testCriteria = session.createCriteria(OSMEdge.class);
            if (bufferSize > 0.) { // ... build a buffer ...
                Geometry buffer = l.buffer(bufferSize);
                //            testCriteria.add(SpatialRestrictions.within("geometry", buffer));
                testCriteria.add(SpatialRestrictions.intersects("geometry", buffer));
            }
            @SuppressWarnings("unchecked")
            List<OSMEdge> result = testCriteria.list();

            logger.info("Spatial query selected " + result.size() + " edges");

            Iterator<OSMEdge> iter = result.iterator();
            i = 0;
            while (iter.hasNext()) {
                i++;
                OSMEdge o = iter.next();
                LineString g = o.getGeometry();
                addLineString(g, o.getId(), o.getEnvtype(), o.getCyktype(), o.getGroenm());
                // let us add this thing to the spatial index
                addOSMEdgeToSpatialIndex(o);
            }

            // if required, dump the graph to a shapefile:
            if (dumpFile != "") {
                try {
                    this.dumpBuffer(result, dumpFile);
                    logger.info("buffer dumped");
                } catch (Exception e) {
                    logger.error("error dumping buffer: " + e);
                }
            }
        } else {
            logger.error("error: track has <2 coordinates");
        }
    }

    session.disconnect();
}

From source file:org.life.sl.importers.CalculateODMatrix.java

License:Open Source License

public HashMap<Integer, OSMEdge> loadEdgesFromOSM() {

    HashMap<Integer, OSMEdge> id_edge = new HashMap<Integer, OSMEdge>();

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*  w ww  .j a  v  a  2  s .c  o m*/

    Query result = session.createQuery("from OSMEdge");
    @SuppressWarnings("unchecked")
    Iterator<OSMEdge> iter = result.iterate();
    while (iter.hasNext()) {
        OSMEdge e = iter.next();
        id_edge.put(e.getId(), e);
    }
    session.disconnect();
    return id_edge;
}

From source file:org.life.sl.importers.CalculateODMatrix.java

License:Open Source License

public HashMap<Integer, OSMNode> loadNodesFromOSM() {

    HashMap<Integer, OSMNode> id_node = new HashMap<Integer, OSMNode>();

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();/*w w w .j  a va 2 s  .  c o  m*/

    Query result = session.createQuery("from OSMNode");
    @SuppressWarnings("unchecked")
    Iterator<OSMNode> iter = result.iterate();
    while (iter.hasNext()) {
        OSMNode e = iter.next();
        id_node.put(e.getId(), e);
    }
    session.disconnect();
    return id_node;
}

From source file:org.n52.sos.inspire.aqd.persistence.ReportingHeaderSQLiteSessionFactory.java

License:Open Source License

@Override
public void cleanup() {
    try {/*from ww w.ja v  a  2  s .c o  m*/
        Session session = getConnection();
        if (session.isOpen()) {
            session.disconnect();
        }
    } catch (ConnectionProviderException cpe) {
        LOG.error("Error while closing connection!", cpe);
    }
    super.cleanup();
}

From source file:org.pentaho.pac.server.common.config.PropertyLoader.java

License:Open Source License

public <E extends PacProperty> PropertyCollection<E> getProperties(Class<E> module) {
    Session s = null;
    Transaction tx = null;//from   w  ww . j  a  v  a 2 s  . co m

    try {
        s = HibernateSessionFactory.getSession("MGMT-SERVICES"); //$NON-NLS-1$
        tx = s.beginTransaction();
        @SuppressWarnings("unchecked")
        PropertyCollection<E> props = new PropertyCollection<E>(s.createCriteria(module).list());
        return props;
    } finally {
        if (s != null)
            s.disconnect();
        if (tx != null)
            tx.commit();
    }

}

From source file:org.pentaho.pac.server.common.config.PropertyLoader.java

License:Open Source License

public <E extends PacProperty> void persist(PropertyCollection<E> props) {
    Session s = null;
    Transaction tx = null;/*from ww w . j ava 2  s  .  c  o m*/

    try {
        s = HibernateSessionFactory.getSession("MGMT-SERVICES"); //$NON-NLS-1$
        tx = s.beginTransaction();
        for (PacProperty prop : props) {
            s.saveOrUpdate(prop);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {

        if (s != null)
            s.disconnect();
        if (tx != null)
            tx.commit();
    }
}

From source file:org.pentaho.platform.repository.hibernate.HibernateUtil.java

License:Open Source License

/**
 * Disconnect and return Session from current Thread.
 * /*from  w  w w. j a  v a2 s.  com*/
 * @return Session the disconnected Session
 */
public static Session disconnectSession() throws RepositoryException {

    Session session = HibernateUtil.getSession();
    try {
        HibernateUtil.threadSession.set(null);
        if (session.isConnected() && session.isOpen()) {
            session.disconnect();
        }
    } catch (HibernateException ex) {
        HibernateUtil.log.error(Messages.getInstance().getErrorString("HIBUTIL.ERROR_0002_DISCONNECT"), ex); //$NON-NLS-1$
        throw new RepositoryException(Messages.getInstance().getErrorString("HIBUTIL.ERROR_0002_DISCONNECT"), //$NON-NLS-1$
                ex);
    }
    return session;
}

From source file:org.projectforge.core.HibernateSearchDependentObjectsReindexer.java

License:Open Source License

public void reindexDependents(final HibernateTemplate hibernateTemplate, final BaseDO<?> obj) {
    new Thread() {
        @Override/*from   ww w  .j a va2  s.c  o m*/
        public void run() {
            final SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
            final HibernateTemplate template = new HibernateTemplate(sessionFactory);
            final Session session = template.getSessionFactory().openSession();
            final Set<String> alreadyReindexed = new HashSet<String>();
            final List<Entry> entryList = map.get(obj.getClass());
            reindexDependents(template, session, obj, entryList, alreadyReindexed);
            session.disconnect();
            final int size = alreadyReindexed.size();
            if (size >= 10) {
                log.info("Re-indexing of " + size + " objects done after updating " + obj.getClass().getName()
                        + ":" + obj.getId());
            }
        }
    }.start();
}