Example usage for org.hibernate LockOptions NO_WAIT

List of usage examples for org.hibernate LockOptions NO_WAIT

Introduction

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

Prototype

int NO_WAIT

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

Click Source Link

Document

Indicates that the database should not wait at all to acquire the pessimistic lock.

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./*w ww .j  a v  a 2  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;
    }
}

From source file:com.syndiceo.PostgreSQLDialect.java

License:Open Source License

public String getWriteLockString(int timeout) {
    if (timeout == LockOptions.NO_WAIT)
        return " for update nowait";
    else//w  w w. jav  a2s  .  com
        return " for update";
}

From source file:com.syndiceo.PostgreSQLDialect.java

License:Open Source License

public String getReadLockString(int timeout) {
    if (timeout == LockOptions.NO_WAIT)
        return " for share nowait";
    else//from ww w  . jav  a 2  s. c  o  m
        return " for share";
}