Skip to content

Commit

Permalink
fix: Propagate the readOnly property to the Bolt transaction. (#544)
Browse files Browse the repository at this point in the history
This update makes sure that the connection's `readOnly` property is used when beginning a new Bolt transaction.
  • Loading branch information
injectives authored Mar 7, 2024
1 parent 6caa1e6 commit 4926da2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion neo4j-jdbc/src/main/java/org/neo4j/jdbc/ConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ Neo4jTransaction getTransaction(boolean autoCommitCheck) throws SQLException {
&& Neo4jTransaction.State.FAILED.equals(this.transaction.getState()))
? this.boltConnection.reset(false) : CompletableFuture.completedStage(null);
var transactionType = this.autoCommit ? TransactionType.UNCONSTRAINED : TransactionType.DEFAULT;
var beginStage = this.boltConnection.beginTransaction(Collections.emptySet(), AccessMode.WRITE,
var beginStage = this.boltConnection.beginTransaction(Collections.emptySet(), getAccessMode(),
transactionType, false);
this.transaction = new DefaultTransactionImpl(this.boltConnection,
exception -> this.fatalException = exception, resetStage.thenCompose(ignored -> beginStage),
Expand All @@ -649,6 +649,10 @@ Neo4jTransaction getTransaction(boolean autoCommitCheck) throws SQLException {
return this.transaction;
}

private AccessMode getAccessMode() {
return this.readOnly ? AccessMode.READ : AccessMode.WRITE;
}

@Override
public void flushTranslationCache() {
synchronized (this) {
Expand Down
20 changes: 20 additions & 0 deletions neo4j-jdbc/src/test/java/org/neo4j/jdbc/ConnectionImplTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,26 @@ void shouldUpdateReadOnly() throws SQLException {
assertThat(connection.isReadOnly()).isTrue();
}

@ParameterizedTest
@ValueSource(booleans = { true, false })
void shouldPassAccessModeToBoltConnection(boolean readOnly) throws SQLException {
// given
var boltConnection = mock(BoltConnection.class);
var accessMode = readOnly ? AccessMode.READ : AccessMode.WRITE;
given(boltConnection.beginTransaction(Collections.emptySet(), accessMode, TransactionType.UNCONSTRAINED, false))
.willReturn(CompletableFuture.completedStage(null));
var connection = makeConnection(boltConnection);
connection.setReadOnly(readOnly);

// when
var transaction = connection.getTransaction();

// then
assertThat(transaction).isNotNull();
then(boltConnection).should()
.beginTransaction(Collections.emptySet(), accessMode, TransactionType.UNCONSTRAINED, false);
}

@Test
void shouldThrowOnUpdatingReadOnlyDuringTransaction() throws SQLException {
var boltConnection = mock(BoltConnection.class);
Expand Down

0 comments on commit 4926da2

Please sign in to comment.