Example usage for org.hibernate LockOptions WAIT_FOREVER

List of usage examples for org.hibernate LockOptions WAIT_FOREVER

Introduction

In this page you can find the example usage for org.hibernate LockOptions WAIT_FOREVER.

Prototype

int WAIT_FOREVER

To view the source code for org.hibernate LockOptions WAIT_FOREVER.

Click Source Link

Document

Indicates that there is no timeout for the acquisition.

Usage

From source file:com.sam.moca.dao.hibernate.AbstractUnknownKeyHibernateDAO.java

License:Open Source License

/**
 * This will lock the given and update at the same time.  Note that any
 * children objects may possibly be stale and if needed this object
 * should be reread after locking./*ww w. j  a v a2  s .c o  m*/
 * Depending on the database provider there is no guarantee as to whether
 * the wait argument is paid attention to.
 * @param obj The object to lock the row on
 * @param wait Whether to wait for the lock or timeout
 * @return whether or not the lock was obtained
 */
public boolean lockRow(T obj, boolean wait) {
    try {
        LockOptions opts = new LockOptions();
        if (wait) {
            opts.setLockMode(LockMode.PESSIMISTIC_WRITE);
            opts.setTimeOut(LockOptions.WAIT_FOREVER);
        } else {
            opts.setLockMode(LockMode.UPGRADE_NOWAIT);
            opts.setTimeOut(LockOptions.NO_WAIT);
        }
        HibernateTools.getSession().refresh(obj, opts);
        return true;
    } catch (LockAcquisitionException e) {
        return false;
    }
}