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

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

Introduction

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

Prototype

public boolean isLocked() 

Source Link

Document

Queries if this lock is held by any thread.

Usage

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

/**
 * Abort database transaction/*from   w ww.java2s  . c  om*/
 * 
 * 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#DIDNT_START_RDB_TRANSACTION} if this thread didn't start the transaction
 * @throws AnzoException
 *             {@link ExceptionConstants.RDB#FAILED_ROLLBACK_RDB_TRANSACTION} if there was a problem rolling back the connection
 */
protected void abort(Connection connection, boolean needsWrite, boolean needsTransaction) throws AnzoException {
    long start = 0;
    if (stats.isEnabled()) {
        start = System.currentTimeMillis();
        stats.getAbortUse().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 {
                            if (!connection.isClosed()) {
                                try {
                                    connection.rollback();
                                    connection.setAutoCommit(true);
                                } catch (SQLException e) {
                                    log.error(LogUtils.RDB_MARKER, "Error rolling back transaction", e);
                                    exceptions = new ArrayList<AnzoException>();
                                    exceptions.add(new AnzoException(
                                            ExceptionConstants.RDB.FAILED_ROLLBACK_RDB_TRANSACTION, e));
                                }
                                try {
                                    unlockTable(connection, needsWrite);
                                } catch (AnzoException ae) {
                                    log.error(LogUtils.RDB_MARKER, "Error unlocking table", ae);
                                    if (exceptions == null) {
                                        exceptions = new ArrayList<AnzoException>();
                                    }
                                    exceptions.add(ae);
                                }
                            }
                        } catch (SQLException e) {
                            log.error(LogUtils.RDB_MARKER, "Error rollingback jdbc transaction", e);
                            exceptions = new ArrayList<AnzoException>();
                            exceptions.add(new AnzoException(
                                    ExceptionConstants.RDB.FAILED_ROLLBACK_RDB_TRANSACTION, e));
                        }

                        if (exceptions != null && exceptions.size() > 0) {
                            throw new CompoundAnzoException(exceptions,
                                    ExceptionConstants.RDB.FAILED_ROLLBACK_RDB_TRANSACTION);
                        }
                    }
                } finally {
                    lock.unlock();
                    nodeLayout.clearUncommittedCache();
                }
            } else {
                throw new AnzoException(ExceptionConstants.RDB.DIDNT_START_RDB_TRANSACTION);
            }
        }
    } finally {
        if (stats.isEnabled()) {
            stats.getAbortDuration().addTime((System.currentTimeMillis() - start));
        }
    }
}