Example usage for java.util.concurrent.locks ReentrantLock getHoldCount

List of usage examples for java.util.concurrent.locks ReentrantLock getHoldCount

Introduction

In this page you can find the example usage for java.util.concurrent.locks ReentrantLock getHoldCount.

Prototype

public int getHoldCount() 

Source Link

Document

Queries the number of holds on this lock by the current thread.

Usage

From source file:Main.java

private static String getLockInfo(ReentrantLock lock) {
    String lockid = "Lock@" + Integer.toHexString(lock.hashCode());
    return "lock " + lockid + " held=" + lock.getHoldCount() + " isHeldMe=" + lock.isHeldByCurrentThread()
            + " hasQueueThreads=" + lock.hasQueuedThreads();
}

From source file:eu.riscoss.server.DBConnector.java

private static void unlock(String domain) {

    ReentrantLock lock = locks.get(domain);

    if (lock == null)
        return;/*from  w ww. j a v  a 2s .c  o m*/

    if (lock.getHoldCount() > 0) {
        System.out.println("'" + domain + "' UNLock by " + Thread.currentThread());
        //         Thread.dumpStack();
        lock.unlock();
        //         locks.remove( domain );
    }
}

From source file:org.openanzo.datasource.nodecentric.internal.NodeCentricDatasource.java

/**
 * Begin database transaction//w  ww. j ava  2  s  .co  m
 * 
 * Note:Database already in transaction
 * 
 * @param connection
 *            {@link Connection} to underlying database
 * @param needsWrite
 *            if true, tables will be locked if needed
 * @param needsTransaction
 *            TODO
 * @throws AnzoException
 *             {@link ExceptionConstants.RDB#ALREADY_IN_RDB_TRANSACTION} if this connection is already with a transaction
 * @throws AnzoException
 *             {@link ExceptionConstants.RDB#FAILED_START_RDB_TRANSACTION} if there was a problem setting autoCommit to false
 */
public void begin(Connection connection, boolean needsWrite, boolean needsTransaction) throws AnzoException {
    long start = 0;
    if (stats.isEnabled()) {
        start = System.currentTimeMillis();
        stats.getBeginUse().increment();
    }
    try {
        ReentrantLock lock = connectionLocks.get(connection);
        if (lock == null) {
            lock = new ReentrantLock();
            connectionLocks.put(connection, lock);
        }
        if (lock.isLocked()) {
            throw new AnzoException(ExceptionConstants.RDB.ALREADY_IN_RDB_TRANSACTION);
        }
        lock.lock();
        if (lock.getHoldCount() == 1 && needsTransaction) {
            try {
                connection.setAutoCommit(false);
            } catch (SQLException e) {
                lock.unlock();
                log.error(LogUtils.RDB_MARKER, "Error starting jdbc transaction", e);
                throw new AnzoRuntimeException(ExceptionConstants.RDB.FAILED_START_RDB_TRANSACTION, e);
            }
            try {
                lockTable(connection, needsWrite);
            } catch (AnzoException e) {
                try {
                    connection.setAutoCommit(false);
                } catch (SQLException sqle) {
                    log.error(LogUtils.RDB_MARKER, "Error aborting jdbc transaction", sqle);
                }
                lock.unlock();
                throw e;
            }

        }
    } finally {
        if (stats.isEnabled()) {
            stats.getBeginDuration().addTime((System.currentTimeMillis() - start));
        }
    }
}

From source file:org.ut.biolab.medsavant.server.db.LockController.java

/**
 * Obtain a lock. If another thread already owns the lock, an exception will
 * be thrown./*from w  w w  .ja v  a  2  s.  c  om*/
 *
 * @param database The database with the project to be unlocked.
 * @param projectID The project ID to lock
 * @throws LockException
 */
public synchronized void requestLock(String database, int projectID) throws LockException {
    LOG.info("Server lock requested");
    Key key = new Key(database, projectID);
    ReentrantLock lock = locks.get(key);

    // create the lock if one doesn't exist for the project
    if (lock == null) {
        lock = new ReentrantLock();
        locks.put(key, lock);
    }

    // lock it down 
    // (if this thread owns the lock, call lock again which will increase the hold count)
    if (!lock.isLocked() || lock.isHeldByCurrentThread()) {
        lock.lock();
        LOG.info(String.format("Server locked - hold count %d", lock.getHoldCount()));
    } else {
        throw new LockException("Database is locked for changes");
    }
}

From source file:org.ut.biolab.medsavant.server.db.LockController.java

/**
 * Release the lock. If force is false, only the thread which requested the
 * lock may unlock. However, in the case where that thread no longer exists,
 * it is necessary to force the unlock./*  w ww  . jav a 2 s. com*/
 *
 * @param database The database containing the project to unlock.
 * @param projectID The project ID to unlock.
 * @param force Whether to force the unlock.
 * @throws org.ut.biolab.medsavant.shared.model.LockException
 */
public synchronized void releaseLock(String database, int projectID, boolean force) throws LockException {

    LOG.info("Server unlock requested");

    Key k = new Key(database, projectID);
    ReentrantLock lock = locks.get(k);

    // no lock exists for this project
    if (lock == null) {
        throw new LockException("No lock exists");
    }

    if (force) {
        while (lock.isLocked()) {
            lock.unlock();
        }
        LOG.info(String.format("Server forcibly unlocked - hold count %d", lock.getHoldCount()));

        // unlock it, or decrement the hold count
    } else if (lock.isHeldByCurrentThread()) {
        lock.unlock();
        LOG.info(String.format("Server unlocked - hold count %d", lock.getHoldCount()));
        // not allowed to lock
    } else {
        throw new LockException("Database could not be unlocked");
    }
}