Example usage for org.hibernate Session clear

List of usage examples for org.hibernate Session clear

Introduction

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

Prototype

void clear();

Source Link

Document

Completely clear the session.

Usage

From source file:de.unisb.cs.st.javalanche.mutation.results.persistence.QueryManager.java

License:Open Source License

public static void saveMutations(Collection<Mutation> mutationsToSave) {
    Session session = openSession();
    Transaction tx = session.beginTransaction();
    int counter = 0;
    for (Mutation mutation : mutationsToSave) {
        if (getMutationOrNull(mutation, session) != null) {
            logger.debug("Not saving mutation. Mutation already in db " + mutation);
        } else {/*  w  w w  .  ja  v a2s .c  o  m*/
            counter++;
            if (counter % 1000 == 0) {
                session.flush();
                session.clear();
            }
            try {
                logger.debug(counter + ": Trying to save mutation :" + mutation);
                session.save(mutation);
            } catch (Exception e) {
                logger.warn("Exception thrown: " + e.getMessage());
                logger.info(Util.getStackTraceString());
                logger.info("Mutations to save: " + mutationsToSave);
                throw new RuntimeException(e);
            }
        }
    }
    tx.commit();
    session.close();

}

From source file:de.xatc.server.importdataprocessors.AirportProcessor.java

@Override
public void run() {

    if (!airportFile.exists()) {
        LOG.error("FirDetailList not found.... returning");
        return;/*from   www  . j a v a 2s. com*/
    }

    Session s = DBSessionManager.getSession();

    Transaction tx = s.beginTransaction();
    Query q = s.createQuery("delete from PlainAirport");
    q.executeUpdate();
    tx.commit();

    DBSessionManager.closeSession(s);

    s = DBSessionManager.getSession();

    try (BufferedReader br = new BufferedReader(new FileReader(airportFile))) {

        String line = null;
        while ((line = br.readLine()) != null) {

            boolean continueDueToErrors = false;

            if (StringUtils.isEmpty(line)) {
                continue;
            }

            String[] splitted = line.split(":");
            if (splitted.length != 6) {
                continue;
            }
            for (String st : splitted) {
                if (StringUtils.isEmpty(st)) {
                    LOG.trace(line + " Values in Line are empty.... continue");
                    continueDueToErrors = true;
                    break;
                }
            }
            if (continueDueToErrors) {
                continue;
            }
            PlainAirport airport = new PlainAirport();
            airport.setAirportIcao(splitted[0]);
            airport.setAirportName(splitted[1]);
            airport.setCityName(splitted[2]);
            airport.setCountryCode(splitted[3]);

            PlainNavPoint position = new PlainNavPoint();
            position.setLat(Double.parseDouble(splitted[4]));
            position.setLon(Double.parseDouble(splitted[5]));
            airport.setPosition(position);

            s.saveOrUpdate(airport);
            s.flush();
            s.clear();
            LOG.trace(line + "... imported");

        }
        DBSessionManager.closeSession(s);
        br.close();
    } catch (IOException e) {
        LOG.error(e.getLocalizedMessage());
        e.printStackTrace(System.err);
    }

}

From source file:de.xatc.server.importdataprocessors.CountryProcessor.java

@Override
public void run() {

    File countryCodes = new File(ServerConfig.getCountryCodesFile());
    if (!countryCodes.exists()) {
        LOG.error("CountryCodes File does not exist!");
        return;//  w ww.ja  v  a  2s  . c  o m
    }

    Session s = DBSessionManager.getSession();

    Transaction tx = s.beginTransaction();
    Query q = s.createQuery("delete from Country");
    q.executeUpdate();
    tx.commit();
    DBSessionManager.closeSession(s);

    s = DBSessionManager.getSession();

    try (BufferedReader br = new BufferedReader(new FileReader(countryCodes))) {
        String line;
        while ((line = br.readLine()) != null) {

            Country c = new Country();
            String[] splitted = line.split(":");
            if (splitted.length == 2) {

                LOG.trace(line + "... imported");
                c.setCountryCode(splitted[0]);
                c.setCountryName(splitted[1]);
                s.saveOrUpdate(c);
                s.flush();
                s.clear();

            }

        }
        DBSessionManager.closeSession(s);
        br.close();
        LOG.trace("Returning....");

    } catch (IOException e) {
        LOG.error(e.getLocalizedMessage());
        e.printStackTrace(System.err);
    }

}

From source file:de.xatc.server.importdataprocessors.FirAirportsProcessor.java

@Override
public void run() {

    Session s = DBSessionManager.getSession();

    List<Fir> firList = s.createCriteria(Fir.class).list();
    List<PlainAirport> airportList = s.createCriteria(PlainAirport.class).list();

    for (Fir fir : firList) {

        LOG.trace(fir.getFirName() + " " + fir.getFirNameIcao() + " " + fir.getPoligonList().size());

        ArrayList<PlainAirport> airportListInFir = FirNavigationalTools.findAirportsInFir(airportList,
                fir.getPoligonList());//from   w  ww . j  a v a2  s  .co m

        LOG.trace("Airports in FIR: " + airportListInFir.size());
        fir.setIncludedAirports(airportListInFir);
        s.saveOrUpdate(fir);
        s.flush();
        s.clear();
    }
    DBSessionManager.closeSession(s);

}

From source file:de.xatc.server.importdataprocessors.FirPoligonProcessor.java

@Override
public void run() {

    if (!firDetail.exists()) {
        LOG.error("FirDetailList not found.... returning");
        return;/*from  w  w w.j av a  2s  .c o m*/
    }

    Session s = DBSessionManager.getSession();

    List<Fir> firList = s.createCriteria(Fir.class).list();

    for (Fir fir : firList) {

        ArrayList<PlainNavPoint> poligonPoints = this.getFirPoligon(fir.getId());

        LOG.trace("FOUND POLIGONPOINTS: " + poligonPoints.size());
        fir.setPoligonList(poligonPoints);

        LOG.trace(fir.toString());
        s.saveOrUpdate(fir);
        s.flush();
        s.clear();

    }

    DBSessionManager.closeSession(s);

}

From source file:de.xatc.server.importdataprocessors.FirProcessor.java

@Override
public void run() {

    if (!firList.exists()) {
        LOG.warn("Firlist not found.... returning");
        return;// w  ww.j  ava 2  s  . co  m
    }

    Session s = DBSessionManager.getSession();

    Transaction tx = s.beginTransaction();
    Query q = s.createQuery("delete from Fir");
    q.executeUpdate();
    tx.commit();

    DBSessionManager.closeSession(s);

    s = DBSessionManager.getSession();
    int lineNumber = 0;

    try (BufferedReader br = new BufferedReader(new FileReader(firList))) {

        String line;
        while ((line = br.readLine()) != null) {

            boolean continueDueToErrors = false;
            lineNumber++;

            if (StringUtils.isEmpty(line)) {
                LOG.trace("Line is empty.... continue");
                continue;
            }
            Fir fir = new Fir();
            String[] splitted = line.split(":");

            for (String st : splitted) {
                if (StringUtils.isEmpty(st)) {
                    LOG.trace(line + " Values in Line are empty.... continue");
                    continueDueToErrors = true;
                    break;
                }
            }
            if (continueDueToErrors) {
                continue;
            }

            if (splitted.length == 6) {

                fir.setCountryCode(splitted[2]);
                fir.setFirName(splitted[1]);
                fir.setFirNameIcao(splitted[0]);
                PlainNavPoint position = new PlainNavPoint();
                position.setLat(Double.parseDouble(splitted[3]));
                position.setLon(Double.parseDouble(splitted[4]));
                fir.setPosition(position);

                fir.setId(Integer.parseInt(splitted[5]));

                s.saveOrUpdate(fir);
                s.flush();
                s.clear();
                LOG.trace(line + "... imported");

            }

        }
        DBSessionManager.closeSession(s);
        br.close();
        LOG.trace("Returning....");

    } catch (IOException e) {
        LOG.error(e.getLocalizedMessage());
        e.printStackTrace(System.err);
    }

}

From source file:dz.alkhwarizmix.framework.java.dao.impl.AbstractAlKhwarizmixDAO.java

License:Open Source License

private void clearHibernateCurrentSession() {
    final Session s = getHibernateCurrentSession();
    if (s != null)
        s.clear();
}

From source file:edu.amrita.aview.audit.daos.UserActionDAO.java

/**
 * Creates the user actions./*from   w  w w  .j a  v  a  2 s  .  c om*/
 *
 * @param actions the actions
 * @throws AViewException
 */
public static void createUserActions(List<UserAction> actions) throws AViewException {
    Session session = null;
    try {
        session = HibernateUtils.getAuditHibernateConnection();
        session.beginTransaction();
        int counter = 0;
        for (UserAction action : actions) {
            session.save(action);
            counter++;
            if (counter % HibernateUtils.HIBERNATE_BATCH_SIZE == 0) {
                session.flush();
                session.clear();
            }
        }

        session.getTransaction().commit();
    } catch (HibernateException he) {
        processException(he);
        session.getTransaction().rollback();
    } finally {
        HibernateUtils.closeConnection(session);
    }
}

From source file:edu.emory.library.tast.spss.Import.java

License:Open Source License

private void importData() throws IOException {

    RecordReader voyagesRdr = recordIOFactory.createReader(dataFileName);

    int recordNo = 0;
    Record voyageRecord = null;/*from   w w  w.  ja v a2 s  .  c o m*/

    Session sess = HibernateConn.getSession();
    Transaction transaction = sess.beginTransaction();

    while ((voyageRecord = voyagesRdr.readRecord()) != null) {

        long t1 = System.currentTimeMillis();

        AbstractDescriptiveObject obj = objectFactory.newInstance();
        updateValues(sess, obj, voyageRecord, ++recordNo);

        obj.saveOrUpdate(sess);

        if (recordNo % FLUSH_INTERVAL == 0) {
            sess.flush();
            sess.clear();
        }

        long t2 = System.currentTimeMillis();

        System.out.println("vid = " + voyageRecord.getKey() + " " + (t2 - t1) + " ms");

    }

    transaction.commit();
    sess.close();

}

From source file:edu.scripps.fl.pipeline.CommitStage.java

License:Apache License

@Override
public void process(Object obj) throws StageException {
    try {//from w  ww. java 2s  .com
        Session session = getSession();
        int count = counter.incrementAndGet();
        doSave(obj);
        if (count % getCommitFrequency() == 0) {
            getTransaction().commit();
            session.clear();
            setTransaction(session.beginTransaction());
        }
    } catch (Exception ex) {
        throw new StageException(this, ex);
    }
}