Skip to content

Commit

Permalink
Unify lifecycle of StatementLocksFactory implementations
Browse files Browse the repository at this point in the history
And improve setting description.
  • Loading branch information
lutovich committed Jul 29, 2016
1 parent 9a3e987 commit 6b83ba8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
Expand Up @@ -397,7 +397,7 @@ private static StatementLocksFactory createStatementLocksFactory( Locks locks, C
List<StatementLocksFactory> factories = Iterables.toList( Service.load( StatementLocksFactory.class ) );
if ( factories.isEmpty() )
{
statementLocksFactory = new SimpleStatementLocksFactory( locks );
statementLocksFactory = new SimpleStatementLocksFactory();

log.info( "No services implementing " + serviceName + " found. " +
"Using " + SimpleStatementLocksFactory.class.getSimpleName() );
Expand Down
Expand Up @@ -28,27 +28,37 @@
*/
public class SimpleStatementLocksFactory implements StatementLocksFactory
{
private final Locks locks;
private Locks locks;

public SimpleStatementLocksFactory( Locks locks )
public SimpleStatementLocksFactory()
{
this.locks = requireNonNull( locks );
}

/**
* Inherited from the {@link StatementLocksFactory} interface but does nothing for this factory.
* Creates a new factory initialized with given {@code locks}.
* <b>Note:</b> should be used for tests only.
*
* @param locks will not be used.
* @param config will not be used.
* @param locks the locks to use.
*/
public SimpleStatementLocksFactory( Locks locks )
{
initialize( locks, null );
}

@Override
public void initialize( Locks locks, Config config )
{
this.locks = requireNonNull( locks );
}

@Override
public StatementLocks newInstance()
{
if ( locks == null )
{
throw new IllegalStateException( "Factory has not been initialized" );
}

return new SimpleStatementLocks( locks.newClient() );
}
}
Expand Up @@ -21,6 +21,8 @@

import org.junit.Test;

import org.neo4j.kernel.configuration.Config;

import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
Expand Down Expand Up @@ -59,4 +61,34 @@ public void createSimpleStatementLocks()
assertSame( client, statementLocks.optimistic() );
assertSame( client, statementLocks.pessimistic() );
}

@Test
public void newInstanceThrowsWhenNotInitialized()
{
SimpleStatementLocksFactory factory = new SimpleStatementLocksFactory();
try
{
factory.newInstance();
fail( "Exception expected" );
}
catch ( Exception e )
{
assertThat( e, instanceOf( IllegalStateException.class ) );
}
}

@Test
public void initializeThrowsForNullLocks()
{
SimpleStatementLocksFactory factory = new SimpleStatementLocksFactory();
try
{
factory.initialize( null, new Config() );
fail( "Exception expected" );
}
catch ( Exception e )
{
assertThat( e, instanceOf( NullPointerException.class ) );
}
}
}
Expand Up @@ -35,9 +35,8 @@
public class DeferringStatementLocksFactory implements StatementLocksFactory
{
@Internal
@Description( "Enable deferring of locks to commit time. This feature is really dangerous and impacts the " +
"isolation level. It can result in both domain and storage level inconsistencies if used " +
"without an additional transaction management layer on top." )
@Description( "Enable deferring of locks to commit time. This feature weakens the isolation level. " +
"It can result in both domain and storage level inconsistencies." )
public static final Setting<Boolean> deferred_locks_enabled =
setting( "unsupported.dbms.deferred_locks.enabled", Settings.BOOLEAN, Settings.FALSE );

Expand Down

0 comments on commit 6b83ba8

Please sign in to comment.