use of com.orientechnologies.common.concur.lock.OLockException in project orientdb by orientechnologies.
the class OResourcePool method getResource.
public V getResource(K key, final long maxWaitMillis, Object... additionalArgs) throws OLockException {
// First, get permission to take or create a resource
try {
if (!sem.tryAcquire(maxWaitMillis, TimeUnit.MILLISECONDS))
throw new OLockException("No more resources available in pool (max=" + maxResources + "). Requested resource: " + key);
} catch (InterruptedException e) {
throw new OInterruptedException("Acquiring of resources was interrupted");
}
V res;
do {
// POP A RESOURCE
res = resources.poll();
if (res != null) {
// TRY TO REUSE IT
if (listener.reuseResource(key, additionalArgs, res)) {
// OK: REUSE IT
break;
} else
res = null;
// UNABLE TO REUSE IT: THE RESOURE WILL BE DISCARDED AND TRY WITH THE NEXT ONE, IF ANY
}
} while (!resources.isEmpty());
// NO AVAILABLE RESOURCES: CREATE A NEW ONE
try {
if (res == null) {
res = listener.createNewResource(key, additionalArgs);
created.incrementAndGet();
if (OLogManager.instance().isDebugEnabled())
OLogManager.instance().debug(this, "pool:'%s' created new resource '%s', new resource count '%d'", this, res, created.get());
}
resourcesOut.add(res);
if (OLogManager.instance().isDebugEnabled())
OLogManager.instance().debug(this, "pool:'%s' acquired resource '%s' available %d out %d ", this, res, sem.availablePermits(), resourcesOut.size());
return res;
} catch (RuntimeException e) {
sem.release();
// PROPAGATE IT
throw e;
} catch (Exception e) {
sem.release();
throw OException.wrapException(new OLockException("Error on creation of the new resource in the pool"), e);
}
}
use of com.orientechnologies.common.concur.lock.OLockException in project orientdb by orientechnologies.
the class ODatabasePoolAbstract method release.
public void release(final DB iDatabase) {
// REMOVE ANY INTENT BEFORE. THIS RESTORE ANYTHING BEFORE THE CLOSE, LIKE THE USER NAME IN CASE OF MASSIVE INSERT
iDatabase.declareIntent(null);
final String dbPooledName = iDatabase.getUser().getName() + "@" + iDatabase.getURL();
final OReentrantResourcePool<String, DB> pool;
lock();
try {
pool = pools.get(dbPooledName);
} finally {
unlock();
}
if (pool == null)
throw new OLockException("Cannot release a database URL not acquired before. URL: " + iDatabase.getName());
if (pool.returnResource(iDatabase))
this.notifyEvictor(dbPooledName, iDatabase);
}
use of com.orientechnologies.common.concur.lock.OLockException in project orientdb by orientechnologies.
the class OTransactionAbstract method lockRecord.
@Override
public OTransaction lockRecord(final OIdentifiable iRecord, final OStorage.LOCKING_STRATEGY lockingStrategy) {
final OStorage stg = database.getStorage();
if (!(stg.getUnderlying() instanceof OAbstractPaginatedStorage))
throw new OLockException("Cannot lock record across remote connections");
final ORID rid = new ORecordId(iRecord.getIdentity());
LockedRecordMetadata lockedRecordMetadata = locks.get(rid);
boolean addItem = false;
if (lockedRecordMetadata == null) {
lockedRecordMetadata = new LockedRecordMetadata(lockingStrategy);
addItem = true;
} else if (lockedRecordMetadata.strategy != lockingStrategy) {
assert lockedRecordMetadata.locksCount == 0;
lockedRecordMetadata = new LockedRecordMetadata(lockingStrategy);
addItem = true;
}
if (lockingStrategy == OStorage.LOCKING_STRATEGY.EXCLUSIVE_LOCK)
((OAbstractPaginatedStorage) stg.getUnderlying()).acquireWriteLock(rid);
else if (lockingStrategy == OStorage.LOCKING_STRATEGY.SHARED_LOCK)
((OAbstractPaginatedStorage) stg.getUnderlying()).acquireReadLock(rid);
else
throw new IllegalStateException("Unsupported locking strategy " + lockingStrategy);
lockedRecordMetadata.locksCount++;
if (addItem) {
locks.put(rid, lockedRecordMetadata);
}
return this;
}
use of com.orientechnologies.common.concur.lock.OLockException in project orientdb by orientechnologies.
the class OTransactionAbstract method unlockRecord.
@Override
public OTransaction unlockRecord(final OIdentifiable iRecord) {
final OStorage stg = database.getStorage();
if (!(stg.getUnderlying() instanceof OAbstractPaginatedStorage))
throw new OLockException("Cannot lock record across remote connections");
final ORID rid = iRecord.getIdentity();
final LockedRecordMetadata lockedRecordMetadata = locks.get(rid);
if (lockedRecordMetadata == null || lockedRecordMetadata.locksCount == 0)
throw new OLockException("Cannot unlock a never acquired lock");
else if (lockedRecordMetadata.strategy == OStorage.LOCKING_STRATEGY.EXCLUSIVE_LOCK)
((OAbstractPaginatedStorage) stg.getUnderlying()).releaseWriteLock(rid);
else if (lockedRecordMetadata.strategy == OStorage.LOCKING_STRATEGY.SHARED_LOCK)
((OAbstractPaginatedStorage) stg.getUnderlying()).releaseReadLock(rid);
else
throw new IllegalStateException("Unsupported locking strategy " + lockedRecordMetadata.strategy);
lockedRecordMetadata.locksCount--;
if (lockedRecordMetadata.locksCount == 0)
locks.remove(rid);
return this;
}
use of com.orientechnologies.common.concur.lock.OLockException in project orientdb by orientechnologies.
the class OServerCommandAuthenticatedDbAbstract method authenticate.
protected boolean authenticate(final OHttpRequest iRequest, final OHttpResponse iResponse, final List<String> iAuthenticationParts, final String iDatabaseName) throws IOException {
ODatabaseDocument db = null;
try {
db = (ODatabaseDocument) server.openDatabase(iDatabaseName, iAuthenticationParts.get(0), iAuthenticationParts.get(1));
// if (db.getUser() == null)
// // MAYBE A PREVIOUS ROOT REALM? UN AUTHORIZE
// return false;
// Set user rid after authentication
iRequest.data.currentUserId = db.getUser() == null ? "<server user>" : db.getUser().getIdentity().toString();
// AUTHENTICATED: CREATE THE SESSION
iRequest.sessionId = OHttpSessionManager.getInstance().createSession(iDatabaseName, iAuthenticationParts.get(0), iAuthenticationParts.get(1));
iResponse.sessionId = iRequest.sessionId;
return true;
} catch (OSecurityAccessException e) {
// WRONG USER/PASSWD
} catch (OLockException e) {
OLogManager.instance().error(this, "Cannot access to the database '" + iDatabaseName + "'", ODatabaseException.class, e);
} finally {
if (db == null) {
// WRONG USER/PASSWD
sendAuthorizationRequest(iRequest, iResponse, iDatabaseName);
} else {
db.close();
}
}
return false;
}
Aggregations