Example usage for org.hibernate Session getCacheMode

List of usage examples for org.hibernate Session getCacheMode

Introduction

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

Prototype

CacheMode getCacheMode();

Source Link

Document

Get the current cache mode.

Usage

From source file:org.alfresco.repo.workflow.jbpm.JBPMEngine.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<WorkflowInstance> cancelWorkflows(final List<String> workflowIds) {
    return (List<WorkflowInstance>) jbpmTemplate.execute(new JbpmCallback() {
        public Object doInJbpm(JbpmContext context) {
            // Bypass the cache making sure not to flush it
            Session session = context.getSession();
            CacheMode cacheMode = session.getCacheMode();
            FlushMode flushMode = session.getFlushMode();
            session.setCacheMode(CacheMode.GET);
            session.setFlushMode(FlushMode.MANUAL);
            try {
                List<WorkflowInstance> workflowInstances = new ArrayList<WorkflowInstance>(workflowIds.size());
                Map<String, ProcessInstance> processInstances = new HashMap<String, ProcessInstance>(
                        workflowIds.size() * 2);
                GraphSession graphSession = context.getGraphSession();

                // retrieve and cancel process instances
                for (String workflowId : workflowIds) {
                    try {
                        ProcessInstance processInstance = getProcessInstance(graphSession, workflowId);
                        processInstance.getContextInstance().setVariable("cancelled", true);
                        processInstance.end();
                        processInstances.put(workflowId, processInstance);
                    } catch (JbpmException e) {
                        String msg = messageService.getMessage(ERR_CANCEL_WORKFLOW, workflowId);
                        throw new WorkflowException(msg, JbpmAccessor.convertJbpmException(e));
                    }/*from  w ww .  j a  v a 2  s. c  o m*/
                }

                // Flush at the end of the batch
                session.flush();

                for (String workflowId : workflowIds) {
                    try {
                        // retrieve process instance
                        ProcessInstance processInstance = processInstances.get(workflowId);
                        // TODO: Determine if this is the most appropriate way to cancel workflow...
                        //       It might be useful to record point at which it was cancelled etc
                        try {
                            workflowInstances.add(createWorkflowInstance(processInstance));
                        } catch (Exception ex) {
                            logger.warn("Unable to load workflow instance: '" + processInstance
                                    + "' due to exception.", ex);
                        }

                        // delete the process instance
                        graphSession.deleteProcessInstance(processInstance, true, true);
                    } catch (JbpmException e) {
                        String msg = messageService.getMessage(ERR_CANCEL_WORKFLOW, workflowId);
                        throw new WorkflowException(msg, JbpmAccessor.convertJbpmException(e));
                    }
                }

                // Flush at the end of the batch
                session.flush();
                return workflowInstances;
            } finally {
                session.setCacheMode(cacheMode);
                session.setFlushMode(flushMode);
            }
        }
    });
}

From source file:org.alfresco.repo.workflow.jbpm.JBPMEngine.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<WorkflowTask> queryTasks(final WorkflowTaskQuery query, final boolean sameSession) {
    try {/*from w w w. j  a v  a  2s.co  m*/
        return (List<WorkflowTask>) jbpmTemplate.execute(new JbpmCallback() {
            public List<WorkflowTask> doInJbpm(JbpmContext context) {
                // Bypass the cache making sure not to flush it
                Session session = context.getSession();
                CacheMode cacheMode = session.getCacheMode();
                try {
                    session.setCacheMode(CacheMode.GET);
                    Criteria criteria = createTaskQueryCriteria(session, query);
                    List<TaskInstance> tasks = criteria.list();
                    return getWorkflowTasks(tasks, sameSession);
                } finally {
                    session.setCacheMode(cacheMode);
                }
            }
        });
    } catch (JbpmException e) {
        String msg = messageService.getMessage(ERR_QUERY_TASKS, query);
        throw new WorkflowException(msg, e);
    }
}

From source file:org.alfresco.repo.workflow.jbpm.JBPMEngine.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<WorkflowTask> getStartTasks(final List<String> workflowInstanceIds, final boolean sameSession) {
    try {/*from  ww w .j a va 2 s  . c om*/
        return (List<WorkflowTask>) jbpmTemplate.execute(new JbpmCallback() {
            public Object doInJbpm(JbpmContext context) {
                List<Long> jbpmIds = new ArrayList<Long>(workflowInstanceIds.size());
                Set<String> startTaskNames = new TreeSet<String>();
                for (String workflowInstanceId : workflowInstanceIds) {
                    // retrieve process instance
                    GraphSession graphSession = context.getGraphSession();
                    ProcessInstance processInstance = getProcessInstanceIfExists(graphSession,
                            workflowInstanceId);
                    if (processInstance != null) {
                        jbpmIds.add(processInstance.getId());
                        Task startTask = processInstance.getProcessDefinition().getTaskMgmtDefinition()
                                .getStartTask();
                        startTaskNames.add(startTask.getName());
                    }
                }

                if (jbpmIds.isEmpty()) {
                    return Collections.emptyList();
                }

                // retrieve tasks
                Session session = context.getSession();
                Criteria taskCriteria = session.createCriteria(TaskInstance.class);
                taskCriteria.add(Restrictions.in("name", startTaskNames));
                Criteria process = taskCriteria.createCriteria("processInstance");
                process.add(Restrictions.in("id", jbpmIds));

                // Bypass the cache making sure not to flush it
                CacheMode cacheMode = session.getCacheMode();
                try {
                    session.setCacheMode(CacheMode.GET);
                    List<TaskInstance> tasks = process.list();
                    return getWorkflowTasks(tasks, sameSession);
                } finally {
                    session.setCacheMode(cacheMode);
                }
            }
        });
    } catch (JbpmException e) {
        String msg = messageService.getMessage(ERR_QUERY_TASKS, workflowInstanceIds);
        throw new WorkflowException(msg, e);
    }
}

From source file:org.hyperic.hq.events.server.session.EventLogDAO.java

License:Open Source License

/**
 * Insert the event logs in batch, with batch size specified by the
 * <code>hibernate.jdbc.batch_size</code> configuration property.
 * /* ww w .j a  va2 s .  com*/
 * @param eventLogs The event logs to insert.
 */
void insertLogs(EventLog[] eventLogs) {
    Session session = getSession();

    FlushMode flushMode = session.getFlushMode();
    CacheMode cacheMode = session.getCacheMode();

    try {
        session.setFlushMode(FlushMode.MANUAL);

        // We do not want to update the 2nd level cache with these event
        // logs
        session.setCacheMode(CacheMode.IGNORE);

        for (int i = 0; i < eventLogs.length; i++) {
            create(eventLogs[i]);
        }

        session.flush();
        session.clear();
    } finally {
        session.setFlushMode(flushMode);
        session.setCacheMode(cacheMode);
    }
}

From source file:org.xchain.namespaces.hibernate.DebugSessionCommand.java

License:Apache License

private void logSession(Session session) {
    log.debug("Connected, Dirty, Open:  {}",
            new boolean[] { session.isConnected(), session.isDirty(), session.isOpen() });
    log.debug("CacheMode:  {}", session.getCacheMode().toString());
    log.debug("EntityMode: {}", session.getEntityMode().toString());
    log.debug("FlushMode:  {}", session.getFlushMode().toString());
}