Skip to content

lock free driver #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 23 additions & 34 deletions driver/src/main/java/org/neo4j/driver/internal/BaseDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.neo4j.driver.internal;

import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.atomic.AtomicBoolean;

import org.neo4j.driver.internal.security.SecurityPlan;
import org.neo4j.driver.v1.AccessMode;
Expand All @@ -34,8 +34,7 @@ abstract class BaseDriver implements Driver
private final SecurityPlan securityPlan;
protected final Logger log;

private final ReentrantReadWriteLock closedLock = new ReentrantReadWriteLock();
private boolean closed;
private AtomicBoolean closed = new AtomicBoolean( false );

BaseDriver( SecurityPlan securityPlan, Logging logging )
{
Expand All @@ -46,16 +45,8 @@ abstract class BaseDriver implements Driver
@Override
public final boolean isEncrypted()
{
closedLock.readLock().lock();
try
{
assertOpen();
return securityPlan.requiresEncryption();
}
finally
{
closedLock.readLock().unlock();
}
assertOpen();
return securityPlan.requiresEncryption();
}

@Override
Expand All @@ -67,33 +58,26 @@ public final Session session()
@Override
public final Session session( AccessMode mode )
{
closedLock.readLock().lock();
try
{
assertOpen();
return newSessionWithMode( mode );
}
finally
assertOpen();
Session session = newSessionWithMode( mode );
if( closed.get() )
{
closedLock.readLock().unlock();
// the driver is already closed and we either 1. obtain this session from the old session pool
// or 2. we obtain this session from a new session pool
// For 1. this closeResources will take no effect as everything is already closed.
// For 2. this closeResources will close the new connection pool just created to ensure no resource leak.
closeResources();
throw driverCloseException();
}
return session;
}

@Override
public final void close()
{
closedLock.writeLock().lock();
try
if ( closed.compareAndSet(false, true) )
{
if ( !closed )
{
closeResources();
}
}
finally
{
closed = true;
closedLock.writeLock().unlock();
closeResources();
}
}

Expand All @@ -103,9 +87,14 @@ public final void close()

private void assertOpen()
{
if ( closed )
if ( closed.get() )
{
throw new IllegalStateException( "This driver instance has already been closed" );
throw driverCloseException();
}
}

private IllegalStateException driverCloseException()
{
return new IllegalStateException( "This driver instance has already been closed" );
}
}