*/
public void lockPage(int pageId) throws CouldNotLockPageException
{
final Thread thread = Thread.currentThread();
final PageLock lock = new PageLock(pageId, thread);
final Time start = Time.now();
boolean locked = false;
final boolean isDebugEnabled = logger.isDebugEnabled();
PageLock previous = null;
while (!locked && start.elapsedSince().lessThan(timeout))
{
if (isDebugEnabled)
{
logger.debug("'{}' attempting to acquire lock to page with id '{}'",
thread.getName(), pageId);
}
previous = locks.get().putIfAbsent(pageId, lock);
if (previous == null || previous.thread == thread)
{
// first thread to acquire lock or lock is already owned by this thread
locked = true;
}
else
{
// wait for a lock to become available
long remaining = remaining(start, timeout);
if (remaining > 0)
{
previous.waitForRelease(remaining, isDebugEnabled);
}
}
}
if (locked)
{
if (isDebugEnabled)
{
logger.debug("{} acquired lock to page {}", thread.getName(), pageId);
}
}
else
{
if (logger.isWarnEnabled())
{
logger.warn(
"Thread '{}' failed to acquire lock to page with id '{}', attempted for {} out of allowed {}. The thread that holds the lock has name '{}'.",
new Object[] { thread.getName(), pageId, start.elapsedSince(), timeout,
previous.thread.getName() });
if (Application.exists())
{
ThreadDumpStrategy strategy = Application.get()
.getExceptionSettings()