Example usage for org.hibernate Session setCacheMode

List of usage examples for org.hibernate Session setCacheMode

Introduction

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

Prototype

void setCacheMode(CacheMode cacheMode);

Source Link

Document

Set the cache mode.

Usage

From source file:com.adsapient.shared.dao.HibernateEntityDao.java

License:Open Source License

public Integer executeQueryInsert(final String queryName) {
    return (Integer) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException {
            session.setCacheMode(CacheMode.IGNORE);
            Transaction tx = session.beginTransaction();
            Query query = session.createSQLQuery(queryName);
            Integer res = query.executeUpdate();
            tx.commit();//w w  w .  j a  va 2  s .c o  m
            return res;
        }
    });
}

From source file:com.astonish.dropwizard.routing.hibernate.RoutingUnitOfWorkRequestDispatcher.java

License:Apache License

/**
 * Configures the session.//from  w  w  w  . j a  v  a  2s .  c  o m
 * @param session
 *            the session
 */
private void configureSession(Session session) {
    session.setDefaultReadOnly(unitOfWork.readOnly());
    session.setCacheMode(unitOfWork.cacheMode());
    session.setFlushMode(unitOfWork.flushMode());
}

From source file:com.booleanworks.kryptopterus.application.MainHibernateUtil.java

License:Apache License

public Session getNewSession(FlushMode flushMode, CacheMode cacheMode) {
    System.out.println("com.booleanworks.kryptopterus.application.MainHibernateUtil.getNewSession()");

    Session session;
    session = this.sessionFactory.openSession();
    session.setHibernateFlushMode(flushMode);
    session.setCacheMode(cacheMode);

    System.out.println("return session; => " + session.hashCode());
    return session;
}

From source file:com.creative.dao.repository.GenericBatchDaoImpl.java

License:Apache License

@Override
public int executeUpdate(Query query) {
    Session session = sessionFactory.getCurrentSession();
    session.setCacheMode(CacheMode.IGNORE);
    session.setFlushMode(FlushMode.MANUAL);
    int rows = query.executeUpdate();
    session.flush();/*from  w w w  .ja v  a  2 s  .  c  om*/

    logger.info("Updated rows  " + rows + " for " + query.getQueryString());
    return rows;
}

From source file:com.creative.dao.repository.GenericBatchDaoImpl.java

License:Apache License

private <T> int executeBatch(BatchType batchType, List<T> list) {
    Session session = sessionFactory.getCurrentSession();
    session.setCacheMode(CacheMode.IGNORE);
    session.setFlushMode(FlushMode.MANUAL);
    logger.info("Executing  Batch of size :" + list.size() + " given batch size is:" + batchSize);

    for (int i = 0; i < list.size(); i++) {
        switch (batchType) {
        case BATCH_INSERT:
            session.save(list.get(i));/*from w  w  w .ja v a  2 s . c  om*/
            break;
        case BATCH_DELETE:
            session.delete(list.get(i));
            break;
        case BATCH_INSERT_OR_UPDATE:
            session.saveOrUpdate(list.get(i));
        default:
            // nothing;
        }
        if (i > 0 && i % batchSize == 0) {
            logger.info("Flushing and clearing the cache" + " after row number :" + i);
            session.flush();
            session.clear();
        }

    }
    session.flush();
    return list.size();
}

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

public List<Album> getAllAlbums() {
    List<Album> albums = new ArrayList<Album>();
    Transaction trns = null;// w  ww. j ava 2  s .  c o  m
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        trns = session.beginTransaction();
        albums = session.createQuery("from Album").list();
        session.setCacheMode(CacheMode.IGNORE);
    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally {
        session.flush();
        session.close();
    }
    return albums;
}

From source file:com.ikon.dao.LanguageDAO.java

License:Open Source License

/**
 * Find all languages//ww  w . j a v  a  2  s . co m
 * 
 * @param cacheMode Execute language query with the designed cache mode.
 */
@SuppressWarnings("unchecked")
private static List<Language> findAll(CacheMode cacheMode) throws DatabaseException {
    log.debug("findAll({})", cacheMode);
    String qs = "from Language lg order by lg.name asc";
    Session session = null;
    Transaction tx = null;

    try {
        session = HibernateUtil.getSessionFactory().openSession();
        session.setCacheMode(cacheMode);
        Query q = session.createQuery(qs);
        List<Language> ret = q.list();
        log.debug("findAll: {}", ret);
        return ret;
    } catch (HibernateException e) {
        HibernateUtil.rollback(tx);
        throw new DatabaseException(e.getMessage(), e);
    } finally {
        HibernateUtil.close(session);
    }
}

From source file:com.model.dao.DepartmentDao.java

public static boolean addDepartment(Department d) {
    boolean result = false;
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.setCacheMode(CacheMode.IGNORE);
    Transaction tx = null;/*from w  w w. ja va2s .  c  om*/
    try {
        tx = session.beginTransaction();
        session.save(d);
        tx.commit();
        result = true;
    } catch (HibernateException ex) {
        if (tx != null) {
            tx.rollback();
            result = false;
        }
        ex.printStackTrace();
    } finally {
        session.close();
    }
    return result;
}

From source file:com.model.dao.DepartmentDao.java

public static boolean updateDepartment(Department d) {
    boolean result = false;
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.setCacheMode(CacheMode.IGNORE);
    Transaction tx = null;/*from  w  w w  .  j  av a2 s .c om*/
    try {
        tx = session.beginTransaction();
        session.update(d);
        tx.commit();
        result = true;
    } catch (HibernateException ex) {
        if (tx != null) {
            tx.rollback();
            result = false;
        }
        ex.printStackTrace();
    } finally {
        session.close();
    }
    return result;
}

From source file:com.model.dao.DepartmentDao.java

public static boolean deleteDepartment(Department d) {
    boolean result = false;
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.setCacheMode(CacheMode.IGNORE);
    Transaction tx = null;/*from w w w  . java  2 s  .com*/
    try {
        tx = session.beginTransaction();
        session.delete(d);
        tx.commit();
        result = true;
    } catch (HibernateException ex) {
        if (tx != null) {
            tx.rollback();
            result = false;
        }
        ex.printStackTrace();
    } finally {
        session.close();
    }
    return result;
}