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

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

Introduction

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

Prototype

public boolean isHeldByCurrentThread() 

Source Link

Document

Queries if this lock is held by the current thread.

Usage

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

/**
 * Commit database transaction//from www  .  ja v a  2 s  .  com
 * 
 * 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#NOT_IN_RDB_TRANSACTION} if connection isn't in a transaction
 * @throws AnzoException
 *             {@link ExceptionConstants.RDB#DIDNT_START_RDB_TRANSACTION} if this thread didn't start the transaction
 * @throws AnzoException
 *             {@link ExceptionConstants.RDB#FAILED_COMMIT_RDB_TRANSACTION} if there was a problem committing the connection
 */
public void commit(Connection connection, boolean needsWrite, boolean needsTransaction) throws AnzoException {
    long start = 0;
    if (stats.isEnabled()) {
        start = System.currentTimeMillis();
        stats.getCommitUse().increment();
    }
    try {
        ReentrantLock lock = connectionLocks.get(connection);
        if (lock == null) {
            lock = new ReentrantLock();
            connectionLocks.put(connection, lock);
        }
        if (lock.isLocked()) {
            if (lock.isHeldByCurrentThread()) {
                try {
                    if (needsTransaction) {
                        ArrayList<AnzoException> exceptions = null;
                        try {
                            connection.commit();
                            connection.setAutoCommit(true);
                        } catch (SQLException e) {
                            log.error(LogUtils.RDB_MARKER, "Error commmiting jdbc transaction", e);
                            exceptions = new ArrayList<AnzoException>();
                            exceptions.add(
                                    new AnzoException(ExceptionConstants.RDB.FAILED_COMMIT_RDB_TRANSACTION, e));
                        }
                        try {
                            unlockTable(connection, needsWrite);
                        } catch (AnzoException ae) {
                            log.error(LogUtils.RDB_MARKER, "Error unlocking tables", ae);
                            if (exceptions == null) {
                                exceptions = new ArrayList<AnzoException>();
                            }
                            exceptions.add(ae);
                        }
                        if (exceptions != null && exceptions.size() > 0) {
                            throw new CompoundAnzoException(exceptions,
                                    ExceptionConstants.RDB.FAILED_COMMIT_RDB_TRANSACTION);
                        }
                    }
                } finally {
                    lock.unlock();
                    nodeLayout.clearUncommittedCache();
                }
            } else {
                throw new AnzoException(ExceptionConstants.RDB.DIDNT_START_RDB_TRANSACTION);
            }
        } else {
            throw new AnzoException(ExceptionConstants.RDB.NOT_IN_RDB_TRANSACTION);
        }
    } finally {
        if (stats.isEnabled()) {
            stats.getCommitDuration().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./* w  w  w.  ja  va 2s  .  c o m*/
 *
 * @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./*ww  w .  jav  a 2  s  . c  om*/
 *
 * @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");
    }
}