Example usage for org.hibernate LockMode FORCE

List of usage examples for org.hibernate LockMode FORCE

Introduction

In this page you can find the example usage for org.hibernate LockMode FORCE.

Prototype

LockMode FORCE

To view the source code for org.hibernate LockMode FORCE.

Click Source Link

Document

Similar to #UPGRADE except that, for versioned entities, it results in a forced version increment.

Usage

From source file:org.bpmscript.process.hibernate.HibernateInstanceManager.java

License:Apache License

/**
 * @see org.bpmscript.process.IInstanceManager#doWithInstance(java.lang.String,
 *      org.bpmscript.process.IInstanceCallback)
 *///www.j a  va  2s .com
public IExecutorResult doWithInstance(final String pid, final IInstanceCallback callback) throws Exception {
    return (IExecutorResult) getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(org.hibernate.Session session) throws HibernateException, SQLException {
            Transaction transaction = session.beginTransaction();
            try {
                HibernateInstance instance = (HibernateInstance) session.load(HibernateInstance.class, pid,
                        LockMode.FORCE);
                log.debug("locking " + pid);
                Object result = callback.execute(instance);
                log.debug("unlocking " + pid);
                transaction.commit();
                return result;
            } catch (Exception t) {
                transaction.rollback();
                throw new HibernateException(t);
            } catch (Throwable t) {
                transaction.rollback();
                throw new RuntimeException(t);
            }
        }

    });
}

From source file:org.jbpm.graph.node.Join.java

License:Open Source License

public void execute(ExecutionContext executionContext) {
    Token token = executionContext.getToken();

    boolean isAbleToReactivateParent = token.isAbleToReactivateParent();

    if (!token.hasEnded()) {
        token.end(false);/*from  w  w  w. jav a  2 s .  c  o m*/
    }

    // if this token is not able to reactivate the parent, 
    // we don't need to check anything
    if (isAbleToReactivateParent) {

        // the token arrived in the join and can only reactivate 
        // the parent once
        token.setAbleToReactivateParent(false);

        Token parentToken = token.getParent();

        if (parentToken != null) {

            JbpmContext jbpmContext = executionContext.getJbpmContext();
            Session session = (jbpmContext != null ? jbpmContext.getSession() : null);
            if (session != null) {
                LockMode lockMode = LockMode.FORCE;
                if (parentLockMode != null) {
                    lockMode = LockMode.parse(parentLockMode);
                }
                log.debug("forcing version increment on parent token " + parentToken);
                session.flush();
                session.lock(parentToken, lockMode);
            }

            boolean reactivateParent = true;

            // if this is a discriminator
            if (isDiscriminator) {
                // reactivate the parent when the first token arrives in the 
                // join.  this must be the first token arriving because otherwise
                // the isAbleToReactivateParent() of this token should have been false
                // above.
                reactivateParent = true;

                // if a fixed set of tokenNames is specified at design time...
            } else if (tokenNames != null) {
                // check reactivation on the basis of those tokenNames
                reactivateParent = mustParentBeReactivated(parentToken, tokenNames.iterator());

                // if a script is specified
            } else if (script != null) {

                // check if the script returns a collection or a boolean
                Object result = null;
                try {
                    result = script.eval(token);
                } catch (Exception e) {
                    this.raiseException(e, executionContext);
                }
                // if the result is a collection 
                if (result instanceof Collection) {
                    // it must be a collection of tokenNames 
                    Collection runtimeTokenNames = (Collection) result;
                    reactivateParent = mustParentBeReactivated(parentToken, runtimeTokenNames.iterator());

                    // if it's a boolean... 
                } else if (result instanceof Boolean) {
                    // the boolean specifies if the parent needs to be reactivated
                    reactivateParent = ((Boolean) result).booleanValue();
                }

                // if a nOutOfM is specified
            } else if (nOutOfM != -1) {

                int n = 0;
                // wheck how many tokens already arrived in the join
                Iterator iter = parentToken.getChildren().values().iterator();
                while (iter.hasNext()) {
                    Token concurrentToken = (Token) iter.next();
                    if (this.equals(concurrentToken.getNode())) {
                        n++;
                    }
                }
                if (n < nOutOfM) {
                    reactivateParent = false;
                }

                // if no configuration is specified..
            } else {
                // the default behaviour is to check all concurrent tokens and reactivate
                // the parent if the last token arrives in the join
                reactivateParent = mustParentBeReactivated(parentToken,
                        parentToken.getChildren().keySet().iterator());
            }

            // if the parent token needs to be reactivated from this join node
            if (reactivateParent) {

                // write to all child tokens that the parent is already reactivated
                Iterator iter = parentToken.getChildren().values().iterator();
                while (iter.hasNext()) {
                    ((Token) iter.next()).setAbleToReactivateParent(false);
                }

                // write to all child tokens that the parent is already reactivated
                ExecutionContext parentContext = new ExecutionContext(parentToken);
                leave(parentContext);
            }
        }
    }
}

From source file:org.jbpm.pvm.internal.hibernate.DbSessionImpl.java

License:Open Source License

public void forceVersionUpdate(Object entity) {
    session.lock(entity, LockMode.FORCE);
}