Example usage for org.hibernate Session setFlushMode

List of usage examples for org.hibernate Session setFlushMode

Introduction

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

Prototype

@Deprecated
void setFlushMode(FlushMode flushMode);

Source Link

Document

Set the flush mode for this session.

Usage

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

License:Open Source License

public Object read(Object template) throws DataSourceException {
    if (!isManagedEntry(getMetadata(template).getEntityName())) {
        if (logger.isTraceEnabled()) {
            logger.trace("Ignoring template (no mapping in hibernate) [" + template + "]");
        }//  w  w w. j a  v a 2 s .  c  o m
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Read over template [" + template + "]");
    }
    Object object = null;
    Session session = getSessionFactory().openSession();
    session.setFlushMode(FlushMode.NEVER);
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        object = session.get(getMetadata(template).getEntityName(), (Serializable) getId(template));
        tx.rollback();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        throw new DataSourceException("Exception caught while read with template [" + template + "]", e);
    } finally {
        session.close();
    }
    return HibernateIteratorUtils.unproxy(object);
}

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

License:Open Source License

@Override
public Object getById(DataSourceIdQuery idQuery) {

    if (!isManagedEntry(idQuery.getTypeDescriptor().getTypeName())) {
        if (logger.isTraceEnabled()) {
            logger.trace("Ignoring template (no mapping in hibernate) [" + idQuery + "]");
        }//w ww.j av a 2 s  .  c o m
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Read over template [" + idQuery + "]");
    }
    Object object = null;
    Session session = getSessionFactory().openSession();
    session.setFlushMode(FlushMode.NEVER);
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        object = session.get(idQuery.getTypeDescriptor().getTypeName(), (Serializable) idQuery.getId());
        tx.rollback();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        throw new SpaceDataSourceException("Exception caught while read with template [" + idQuery + "]", e);
    } finally {
        session.close();
    }
    return HibernateIteratorUtils.unproxy(object);
}

From source file:org.opentox.toxotis.persistence.db.DeleteTool.java

License:Open Source License

/**
 * Deletes a task of a given URI from the database (Note that this is different
 * from cancelling the task since it completely and permanently deletes the task
 * from the database)./*from w  ww .j av  a 2 s. c  om*/
 * @param session
 * @param taskUri
 */
public int deleteTask(VRI taskUri) {
    Session session = getSession();
    session.setFlushMode(FlushMode.COMMIT);
    Transaction transaction = null;
    Task t = new Task(taskUri);
    t.setMeta(null);
    t.setCreatedBy(null);
    t.setResultUri(null);
    t.setStatus(null);
    t.setDuration(null);
    t.setErrorReport(null);
    t.setOntologicalClasses(null);
    try {
        transaction = session.beginTransaction();
        String sql = "DELETE FROM Task WHERE uri = ?";
        Query q = session.createSQLQuery(sql).setString(0, taskUri.toString());
        return q.executeUpdate();
    } catch (RuntimeException ex) {
        logger.warn("Deletion of task with URI " + taskUri
                + " failed. Logging the corresponding exception for details", ex);
        try {
            if (transaction != null) {
                transaction.rollback();
            }
        } catch (Throwable rte) {
            logger.error("Cannot rollback", rte);
        }
        throw ex;
    } finally {
        closeSession(session);
    }
}

From source file:org.opentox.toxotis.persistence.db.RegisterTool.java

License:Open Source License

public void storeUser(User user) {
    Session session = getSession();
    Transaction transaction = null;/*from  ww  w  . j  a v  a 2 s. co m*/
    session.setFlushMode(FlushMode.COMMIT);
    try {
        transaction = session.beginTransaction();
        session.saveOrUpdate(user);
        transaction.commit();

        session.clear();
    } catch (RuntimeException ex) {
        logger.warn("Storage of User failed. Logging the corresponding exception for details", ex);
        try {
            if (transaction != null) {
                transaction.rollback();
            }
        } catch (Throwable rte) {
            logger.error("Cannot rollback", rte);
        }
        throw ex;
    } finally {
        closeSession(session);
    }
}

From source file:org.opentox.toxotis.persistence.db.RegisterTool.java

License:Open Source License

public void storeFeature(Feature feature) {
    Session session = getSession();
    Transaction transaction = null;// w ww  . j  a va 2  s. c o  m
    session.setFlushMode(FlushMode.COMMIT);
    try {
        preprocessComponent(feature);
        transaction = session.beginTransaction();
        session.saveOrUpdate(feature);
        transaction.commit();
    } catch (RuntimeException ex) {
        logger.warn("Storage of Model failed. Logging the corresponding exception for details", ex);
        try {
            if (transaction != null) {
                transaction.rollback();
            }
        } catch (Throwable rte) {
            logger.error("Cannot rollback", rte);
        }
        throw ex;
    } finally {
        closeSession(session);
    }
}

From source file:org.opentox.toxotis.persistence.db.RegisterTool.java

License:Open Source License

public void storeModel(Model model) {
    Session session = getSession();
    Transaction transaction = null;/*from   ww  w . j  av  a  2  s  . c om*/
    session.setFlushMode(FlushMode.COMMIT);
    try {
        preprocessComponent(model);
        transaction = session.beginTransaction();

        session.evict(model.getAlgorithm());

        if (model.getParameters() != null) {
            for (Parameter p : model.getParameters()) {
                session.saveOrUpdate(p);
                session.flush();
                session.evict(p);
            }
        }

        if (model.getIndependentFeatures() != null) {
            for (Feature ft : model.getIndependentFeatures()) {
                storeFeature(ft);
                session.evict(ft);
            }
        }

        if (model.getCreatedBy() != null) {
            session.saveOrUpdate(model.getCreatedBy());
            session.flush();
            session.evict(model.getCreatedBy());
        }
        if (model.getDependentFeatures() != null) {
            for (Feature depFeature : model.getDependentFeatures()) {
                session.saveOrUpdate(depFeature);
                session.flush();
                session.evict(depFeature);
            }
        }

        if (model.getPredictedFeatures() != null) {
            for (Feature predictedFeature : model.getPredictedFeatures()) {
                session.saveOrUpdate(predictedFeature);
                session.flush();
                session.evict(predictedFeature);
            }
        }

        session.saveOrUpdate(model);
        transaction.commit();
        session.clear();
    } catch (StaleObjectStateException ex) {
        logger.error("Serious exception that cannot be recovered! Stale Object!!!!");
        try {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        } catch (Throwable rte) {
            logger.error("Cannot rollback", rte);
        }
        throw ex;
    } catch (RuntimeException ex) {
        logger.warn("Storage of Model failed. Logging the corresponding exception for details", ex);
        try {
            if (transaction != null) {
                transaction.rollback();
            }
        } catch (Throwable rte) {
            logger.error("Cannot rollback", rte);
        }
        throw ex;
    } finally {
        closeSession(session);
    }
}

From source file:org.opentox.toxotis.persistence.db.RegisterTool.java

License:Open Source License

public void storeErrorReport(ErrorReport er) {
    Session session = getSession();
    Transaction transaction = null;/*w  ww.j  av  a2s  .  co m*/
    session.setFlushMode(FlushMode.COMMIT);
    try {
        transaction = session.beginTransaction();
        session.saveOrUpdate(er);
        transaction.commit();
    } catch (RuntimeException ex) {
        logger.warn("Storage of Error Report failed. Logging the corresponding exception for details", ex);
        try {
            if (transaction != null) {
                transaction.rollback();
            }
        } catch (Throwable rte) {
            logger.error("Cannot rollback", rte);
        }
        throw ex;
    } finally {
        closeSession(session);
    }
}

From source file:org.opentox.toxotis.persistence.db.RegisterTool.java

License:Open Source License

public void storeMetaData(MetaInfo meta) {
    Session session = getSession();
    Transaction transaction = null;/* www.j  a  v  a  2s  .c  o m*/
    session.setFlushMode(FlushMode.COMMIT);
    try {
        //TODO: Code for storing meta data!
    } catch (RuntimeException ex) {
        logger.warn("Storage of MetaInfo failed. Logging the corresponding exception for details", ex);
        try {
            if (transaction != null) {
                transaction.rollback();
            }
        } catch (Throwable rte) {
            logger.error("Cannot rollback", rte);
        }
        throw ex;
    } finally {
        closeSession(session);
    }

}

From source file:org.opentox.toxotis.persistence.db.RegisterTool.java

License:Open Source License

public synchronized void storeTask(Task task) {
    Session session = getSession();
    Transaction transaction = null;//from ww w  .  j  a v  a 2 s . co m
    session.setFlushMode(FlushMode.COMMIT);

    try {
        transaction = session.beginTransaction();
        User createdBy = task.getCreatedBy();
        if (createdBy != null) {
            session.saveOrUpdate(createdBy);
            session.flush();
            session.evict(createdBy);
        }
        ErrorReport er = task.getErrorReport();
        if (er != null) {
            session.saveOrUpdate(er);
            session.flush();
            session.evict(er);
        }
        session.saveOrUpdate(task);
        session.flush();
        transaction.commit();

    } catch (RuntimeException ex) {
        logger.warn("Storage of Task failed. Logging the corresponding exception for details.", ex);
        try {
            if (transaction != null) {
                transaction.rollback();
            }
        } catch (Throwable rte) {
            logger.error("Cannot rollback", rte);
        }
        throw ex;
    } finally {
        closeSession(session);
    }
}

From source file:org.osaf.cosmo.hibernate.ThrowAwayHibernateSessionOnErrorInterceptor.java

License:Apache License

private void handleException() {

    // If session is bound to transaction, close it and create/bind
    // new session to prevent stale data when retrying transaction
    if (TransactionSynchronizationManager.hasResource(sessionFactory)) {

        if (log.isDebugEnabled())
            log.debug("throwing away bad session and binding new one");

        // Get current session and close
        SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
                .unbindResource(sessionFactory);

        SessionFactoryUtils.closeSession(sessionHolder.getSession());

        // Open new session and bind (this session should be closed and
        // unbound elsewhere, for example OpenSessionInViewFilter)
        Session session = SessionFactoryUtils.getSession(sessionFactory, true);
        session.setFlushMode(FlushMode.MANUAL);
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
    }//w w  w  .  j  av  a  2  s .  c  o  m
}