Skip to content
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

fix: Noop in case there is no change in autocommit value for setAutocommit() method #2662

Merged
merged 5 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ implementation 'com.google.cloud:google-cloud-spanner'
If you are using Gradle without BOM, add this to your dependencies:

```Groovy
implementation 'com.google.cloud:google-cloud-spanner:6.49.0'
implementation 'com.google.cloud:google-cloud-spanner:6.50.0'
```

If you are using SBT, add this to your dependencies:

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.49.0"
libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.50.0"
```
<!-- {x-version-update-end} -->

Expand Down Expand Up @@ -432,7 +432,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-spanner/java11.html
[stability-image]: https://img.shields.io/badge/stability-stable-green
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-spanner.svg
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-spanner/6.49.0
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-spanner/6.50.0
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ public boolean isClosed() {
@Override
public void setAutocommit(boolean autocommit) {
ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
if (isAutocommit() == autocommit) {
olavloite marked this conversation as resolved.
Show resolved Hide resolved
return;
}
ConnectionPreconditions.checkState(!isBatchActive(), "Cannot set autocommit while in a batch");
ConnectionPreconditions.checkState(
!isTransactionStarted(), "Cannot set autocommit while a transaction is active");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,145 @@ public void testExecuteSetAutocommitOff() {
}
}

@Test
public void testSetAutocommitToTrue_inAutoCommitAndNotInTransaction_noop() {
try (ConnectionImpl subject =
createConnection(
ConnectionOptions.newBuilder()
.setCredentials(NoCredentials.getInstance())
.setUri(URI)
.build())) {
assertThat(subject.isAutocommit(), is(true));
olavloite marked this conversation as resolved.
Show resolved Hide resolved

subject.setAutocommit(true);

assertTrue(subject.isAutocommit());
}
}

@Test
public void testSetAutocommitToTrue_inAutoCommitAndInTransaction_noop() {
try (ConnectionImpl subject =
createConnection(
ConnectionOptions.newBuilder()
.setCredentials(NoCredentials.getInstance())
.setUri(URI)
.build())) {
assertThat(subject.isAutocommit(), is(true));
ankiaga marked this conversation as resolved.
Show resolved Hide resolved
subject.execute(Statement.of("begin transaction"));

subject.setAutocommit(true);

assertTrue(subject.isAutocommit());
}
}

@Test
public void testSetAutocommitToFalse_inAutoCommitAndNotInTransaction_autocommitModeChanged() {
try (ConnectionImpl subject =
createConnection(
ConnectionOptions.newBuilder()
.setCredentials(NoCredentials.getInstance())
.setUri(URI)
.build())) {
assertThat(subject.isAutocommit(), is(true));

subject.setAutocommit(false);

assertFalse(subject.isAutocommit());
}
}

@Test
public void testSetAutocommitToFalse_inAutoCommitAndInTransaction_throwsException() {
try (ConnectionImpl subject =
createConnection(
ConnectionOptions.newBuilder()
.setCredentials(NoCredentials.getInstance())
.setUri(URI)
.build())) {
assertThat(subject.isAutocommit(), is(true));
subject.execute(Statement.of("begin transaction"));

SpannerException exception =
assertThrows(SpannerException.class, () -> subject.setAutocommit(false));
assertEquals(ErrorCode.FAILED_PRECONDITION, exception.getErrorCode());
assertTrue(
exception
.getMessage()
.contains("Cannot set autocommit while in a temporary transaction"));
}
}

@Test
public void testSetAutocommitToFalse_notInAutoCommitAndTransactionNotStarted_noop() {
try (ConnectionImpl subject =
createConnection(
ConnectionOptions.newBuilder()
.setCredentials(NoCredentials.getInstance())
.setUri(URI + ";autocommit=false")
.build())) {
assertThat(subject.isAutocommit(), is(false));

subject.setAutocommit(false);

assertFalse(subject.isAutocommit());
}
}

@Test
public void testSetAutocommitToFalse_notInAutoCommitAndTransactionStarted_noop() {
try (ConnectionImpl subject =
createConnection(
ConnectionOptions.newBuilder()
.setCredentials(NoCredentials.getInstance())
.setUri(URI + ";autocommit=false")
.build())) {
assertThat(subject.isAutocommit(), is(false));
subject.executeQuery(Statement.of(SELECT));

subject.setAutocommit(false);

assertFalse(subject.isAutocommit());
}
}

@Test
public void
testSetAutocommitToTrue_notInAutoCommitAndTransactionNotStarted_autocommitModeChanged() {
try (ConnectionImpl subject =
createConnection(
ConnectionOptions.newBuilder()
.setCredentials(NoCredentials.getInstance())
.setUri(URI + ";autocommit=false")
.build())) {
assertThat(subject.isAutocommit(), is(false));

subject.setAutocommit(true);

assertTrue(subject.isAutocommit());
}
}

@Test
public void testSetAutocommitToTrue_notInAutoCommitAndTransactionStarted_throwsException() {
try (ConnectionImpl subject =
createConnection(
ConnectionOptions.newBuilder()
.setCredentials(NoCredentials.getInstance())
.setUri(URI + ";autocommit=false")
.build())) {
assertThat(subject.isAutocommit(), is(false));
subject.executeQuery(Statement.of(SELECT));

SpannerException exception =
assertThrows(SpannerException.class, () -> subject.setAutocommit(true));
assertEquals(ErrorCode.FAILED_PRECONDITION, exception.getErrorCode());
assertTrue(
exception.getMessage().contains("Cannot set autocommit while a transaction is active"));
}
}

@Test
public void testExecuteGetAutocommit() {
try (ConnectionImpl subject =
Expand Down
Loading