Skip to content

Commit

Permalink
Differentiate between rollback and read-only for KTI id
Browse files Browse the repository at this point in the history
This commit makes KernelTransactionImplementation#closeTransaction() return
different values when transaction was rolled back or read only. It also
improves javadoc for KernelTransaction interface.
  • Loading branch information
lutovich committed Jul 8, 2016
1 parent 1c2c9b2 commit ba25119
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
Expand Up @@ -249,7 +249,7 @@ public void failure()
public long closeTransaction() throws TransactionFailureException
{
liveTransactions.decrementAndGet();
return NOT_COMMITTED;
return ROLLBACK;
}

@Override
Expand Down
Expand Up @@ -84,7 +84,8 @@ interface CloseListener
void notify( boolean success );
}

long NOT_COMMITTED = -1;
long ROLLBACK = -1;
long READ_ONLY = 0;

/**
* Acquires a new {@link Statement} for this transaction which allows for reading and writing data from and
Expand All @@ -108,16 +109,19 @@ interface CloseListener
void failure();

/**
* Closes this transaction, committing its changes iff {@link #success()} has been called and
* {@link #failure()} has NOT been called. Otherwise its changes will be rolled back.
* Closes this transaction, committing its changes if {@link #success()} has been called and neither
* {@link #failure()} nor {@link #markForTermination()} has been called.
* Otherwise its changes will be rolled back.
*
* @return id of the committed transaction or {@link #NOT_COMMITTED} if transaction was rolled back or read-only.
* @return id of the committed transaction or {@link #ROLLBACK} if transaction was rolled back or
* {@link #READ_ONLY} if transaction was read-only.
*/
long closeTransaction() throws TransactionFailureException;

/**
* Closes this transaction, committing its changes iff {@link #success()} has been called and
* {@link #failure()} has NOT been called. Otherwise its changes will be rolled back.
* Closes this transaction, committing its changes if {@link #success()} has been called and neither
* {@link #failure()} nor {@link #markForTermination()} has been called.
* Otherwise its changes will be rolled back.
*/
@Override
default void close() throws TransactionFailureException
Expand Down
Expand Up @@ -413,7 +413,7 @@ public long closeTransaction() throws TransactionFailureException
{
rollback();
failOnNonExplicitRollbackIfNeeded();
return NOT_COMMITTED;
return ROLLBACK;
}
else
{
Expand Down Expand Up @@ -538,7 +538,7 @@ private long commit() throws TransactionFailureException
}
}
success = true;
return NOT_COMMITTED;
return READ_ONLY;
}
catch ( ConstraintValidationKernelException | CreateConstraintFailureException e )
{
Expand Down
Expand Up @@ -177,7 +177,7 @@ public void failure()
@Override
public long closeTransaction() throws TransactionFailureException
{
return NOT_COMMITTED;
return ROLLBACK;
}

@Override
Expand Down
Expand Up @@ -553,7 +553,40 @@ public void txReturnsCorrectIdWhenRolledBack() throws Exception
}
tx.failure();

assertEquals( KernelTransaction.NOT_COMMITTED, tx.closeTransaction() );
assertEquals( KernelTransaction.ROLLBACK, tx.closeTransaction() );
assertFalse( tx.isOpen() );
}

@Test
public void txReturnsCorrectIdWhenMarkedForTermination() throws Exception
{
executeDummyTxs( db, 42 );

KernelTransaction tx = kernel.newTransaction( KernelTransaction.Type.implicit, AccessMode.Static.FULL );
try ( Statement statement = tx.acquireStatement() )
{
statement.dataWriteOperations().nodeCreate();
}
tx.markForTermination();

assertEquals( KernelTransaction.ROLLBACK, tx.closeTransaction() );
assertFalse( tx.isOpen() );
}

@Test
public void txReturnsCorrectIdWhenFailedlAndMarkedForTermination() throws Exception
{
executeDummyTxs( db, 42 );

KernelTransaction tx = kernel.newTransaction( KernelTransaction.Type.implicit, AccessMode.Static.FULL );
try ( Statement statement = tx.acquireStatement() )
{
statement.dataWriteOperations().nodeCreate();
}
tx.failure();
tx.markForTermination();

assertEquals( KernelTransaction.ROLLBACK, tx.closeTransaction() );
assertFalse( tx.isOpen() );
}

Expand All @@ -571,7 +604,7 @@ public void txReturnsCorrectIdWhenReadOnly() throws Exception
}
tx.success();

assertEquals( KernelTransaction.NOT_COMMITTED, tx.closeTransaction() );
assertEquals( KernelTransaction.READ_ONLY, tx.closeTransaction() );
assertFalse( tx.isOpen() );
}

Expand Down

0 comments on commit ba25119

Please sign in to comment.