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:com.globalsight.persistence.hibernate.HibernateUtil.java

License:Apache License

/**
 * Save or update some instance. More informations you can see
 * <code>saveOrUpdate(Object object)</code>.
 * /*from w  ww. j a v a 2 s  .  c  om*/
 * @param objects
 *            persistent classes
 * @throws Exception
 */
public static void saveOrUpdate(Collection<?> objects) throws Exception {
    if (objects != null && objects.size() > 0) {
        Session session = getSession();
        Transaction tx = getTransaction();
        try {
            Iterator<?> iterator = objects.iterator();
            int n = 0;
            while (iterator.hasNext()) {
                session.saveOrUpdate(iterator.next());

                // Release memory.
                n++;
                if (n % 20 == 0) {
                    session.flush();
                    session.clear();
                }
            }

            commit(tx);
        } catch (Exception e) {
            rollback(tx);
            e.printStackTrace();
            throw e;
        }
    }
}

From source file:com.globalsight.persistence.hibernate.HibernateUtil.java

License:Apache License

/**
 * /*from  w  w  w. ja  va2  s. c o  m*/
 * 
 * @param session
 *            the session is not null, managed by invoked method.
 * @param objects
 *            need updated data
 * @throws Exception
 */
public static void saveOrUpdate(Session session, Collection<?> objects) throws Exception {
    if (objects != null && objects.size() > 0) {

        if (session == null) {
            throw new Exception("No Hibernate session provided, can not access database.");
        }
        Iterator<?> iterator = objects.iterator();
        int n = 0;
        while (iterator.hasNext()) {
            session.saveOrUpdate(iterator.next());
            // Release memory.
            n++;
            if (n % 20 == 0) {
                session.flush();
                session.clear();
            }
        }
    }
}

From source file:com.google.api.ads.adwords.awreporting.model.persistence.sql.SqlReportEntitiesPersister.java

License:Open Source License

/**
 * Persists all the given entities into the DB configured in the {@code SessionFactory}.
 *///from   w  w w. j  a va  2  s  .com
@Override
@Transactional
public void persistReportEntities(List<? extends Report> reportEntities) {
    int batchFlush = 0;
    Session session = sessionFactory.getCurrentSession();

    for (Report report : reportEntities) {
        report.setRowId();
        session.saveOrUpdate(report);
        batchFlush++;

        if (batchFlush == batchSize) {
            session.flush();
            session.clear();
            batchFlush = 0;
        }
    }

    if (batchFlush > 0) {
        session.flush();
        session.clear();
    }
}

From source file:com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.sql.SqlReportEntitiesPersister.java

License:Open Source License

/**
 * Persists all the given entities into the DB configured in the {@code SessionFactory}
 *///from ww  w. j a v a  2 s  .  c o  m
@Override
@Transactional
public void persistReportEntities(List<? extends Report> reportEntities) {

    int batchFlush = 0;

    Session session = this.sessionFactory.getCurrentSession();

    for (Report report : reportEntities) {
        report.setId();
        session.saveOrUpdate(report);
        batchFlush++;

        if (batchFlush == BATCH_SIZE) {
            session.flush();
            session.clear();
        }
    }
}

From source file:com.griddynamics.jagger.storage.rdb.HibernateKeyValueStorage.java

License:Open Source License

public void putAll(Namespace namespace, Multimap<String, Object> valuesMap) {
    Session session = null;
    int count = 0;
    try {/*  w  w w  .j  a v  a  2 s  .c  o  m*/
        session = getHibernateTemplate().getSessionFactory().openSession();
        session.beginTransaction();
        for (String key : valuesMap.keySet()) {
            Collection<Object> values = valuesMap.get(key);
            for (Object val : values) {
                session.save(createKeyValue(namespace, key, val));
                count++;
                if (count % getHibernateBatchSize() == 0) {
                    session.flush();
                    session.clear();
                }
            }
        }
        session.getTransaction().commit();
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:com.hihsoft.sso.business.service.TaclModuleinfoServiceTestCase.java

License:Apache License

/**??:hibernate5000??spring???<50000?
 * 4000???CPU//from  w w w .jav  a2 s . c om
 * Testsave or update tacl role.
 */
@Test
public void testsaveOrUpdateTaclModuleinfo() {
    TsysModuleinfo moduleinfo = null;
    // Session
    Configuration cfg = new Configuration();
    SessionFactory sf = cfg.configure().buildSessionFactory();
    Session session = sf.openSession();
    // 
    Transaction tx = session.beginTransaction();
    // 100 000?100 000?
    for (int i = 1000000; i < 2000000; i++) {
        // Moduleinfo
        moduleinfo = new TsysModuleinfo();
        moduleinfo.setModulename("moduleinfo" + i);
        moduleinfo.setModulename("00000001" + i);
        moduleinfo.setFlatid("8a9e848a2ed74230012ed7424737000a");
        session.save(moduleinfo);
        // ?20?Session??Session
        if (i % 5000 == 0) {
            session.flush();
            session.clear();
            tx.commit();
            tx = session.beginTransaction();
        }
    }
    // ??
    tx.commit();
    // 
    session.close();

}

From source file:com.humber.java.dao.AlbumDAO.java

public Album getAlbumById(int albumId) {
    Album album = null;//from www .j  a  va2  s. c om
    Transaction trns = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        trns = session.beginTransaction();
        String queryString = "from Album where id = :id";
        Query query = session.createQuery(queryString);
        query.setInteger("id", albumId);
        album = (Album) query.uniqueResult();
    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally {
        session.clear();
        session.flush();
        session.close();
    }
    return album;
}

From source file:com.hyzy.core.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * ??/*w w w  .  jav  a2  s . c  o m*/
 * @param lsit
 * @return
 * @throws SQLException 
 */
@Transactional
public void saveOrupdatBatch(List<Object> list) {

    int batchSize = 100;
    if (list == null || list.isEmpty())
        return;
    Session session = null;
    Transaction tx = null;
    try {
        session = getSessionFactory().openSession();
        tx = session.getTransaction();
        tx.begin();
        for (int i = 0; i < list.size(); i++) {
            session.merge((list.get(i)));
            if (i % batchSize == 0) {
                session.flush();
                session.clear();
            }
        }
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        tx.rollback();
    } finally {
        if (null != tx) {
            tx = null;
        }
        if (null != session) {
            session = null;
        }
    }
}

From source file:com.iaito.atsws.services.ClientServiceImpl.java

@Override
public List<Reader> getReaderList() {
    Transaction tx;/*from   w ww  . j  a  v a2 s  . c  o m*/
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {
        tx = session.beginTransaction();
        Query query = session.createQuery("from Reader");
        List<Reader> readerList = (List<Reader>) query.list();
        session.flush();
        session.clear();
        tx.commit();

        return readerList;
    } catch (Exception e) {
        session.getTransaction().rollback();
        System.out.println(
                "Exception from -------------ClientServiceImpl.java----getReaderList()--" + e.getMessage());
    }
    return null;
}

From source file:com.iaito.atsws.services.ClientServiceImpl.java

@Override
public List<TagLog> getTagLogs(String reader) {
    Transaction tx;//from  ww w.j  av  a 2s.  c om
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {
        tx = session.beginTransaction();
        String s = "select * from tag_log w where at_reader='" + reader
                + "' and arrival=1 and departure=0 and entry_done=0";
        Query query = session.createSQLQuery(s).addEntity(TagLog.class);
        List<TagLog> readerList = (List<TagLog>) query.list();
        session.flush();
        session.clear();
        tx.commit();

        return readerList;
    } catch (Exception e) {
        session.getTransaction().rollback();
        System.out.println(
                "Exception from -------------ClientServiceImpl.java----getTagLogs()--" + e.getMessage());
    }
    return null;
}