Skip to content

Commit cf865f8

Browse files
chore: loosen restrictions setRetryAbortsInternally (#2924)
* chore: loosen restrictions setRetryAbortsInternally Calling setRetryAbortsInternally(boolean) was unnecessarily strict, as there is no reason why it should be disallowed to change the value while in auto-commit mode and/or read-only mode. The fact that the flag does not have any direct impact on those modes, does not mean that it should be disallowed to set the value. The fact that it was impossible to set the value while in auto-commit mode, also means that it is impossible to disable it using a startup command for a PostgreSQL connection to PGAdapter, as PGAdapter creates connections in auto-commit mode. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 19b7976 commit cf865f8

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ConnectionImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -678,10 +678,6 @@ public void setStatementTag(String tag) {
678678
*/
679679
private void checkSetRetryAbortsInternallyAvailable() {
680680
ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
681-
ConnectionPreconditions.checkState(isInTransaction(), "This connection has no transaction");
682-
ConnectionPreconditions.checkState(
683-
getTransactionMode() == TransactionMode.READ_WRITE_TRANSACTION,
684-
"RetryAbortsInternally is only available for read-write transactions");
685681
ConnectionPreconditions.checkState(
686682
!isTransactionStarted(),
687683
"RetryAbortsInternally cannot be set after the transaction has started");

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/ConnectionImplTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,50 @@ public void testCheckResultTypeAllowed() {
18631863
checkResultTypeAllowed(parser.parse(Statement.of(start)), allowedResultTypes);
18641864
}
18651865

1866+
@Test
1867+
public void testSetRetryAbortsInternally() {
1868+
try (ConnectionImpl connection =
1869+
createConnection(
1870+
ConnectionOptions.newBuilder()
1871+
.setCredentials(NoCredentials.getInstance())
1872+
.setUri(URI)
1873+
.build())) {
1874+
assertFalse("Read-only should be disabled by default", connection.isReadOnly());
1875+
assertTrue("Autocommit should be enabled by default", connection.isAutocommit());
1876+
assertFalse(
1877+
"Retry aborts internally should be disabled by default on test connections",
1878+
connection.isRetryAbortsInternally());
1879+
1880+
// It should be possible to change this value also when in auto-commit mode.
1881+
connection.setRetryAbortsInternally(true);
1882+
assertTrue(connection.isRetryAbortsInternally());
1883+
1884+
// It should be possible to change this value also when in transactional mode, as long as
1885+
// there is no active transaction.
1886+
connection.setAutocommit(false);
1887+
connection.setRetryAbortsInternally(false);
1888+
assertFalse(connection.isRetryAbortsInternally());
1889+
1890+
// It should be possible to change the value when in read-only mode.
1891+
connection.setReadOnly(true);
1892+
connection.setRetryAbortsInternally(true);
1893+
assertTrue(connection.isRetryAbortsInternally());
1894+
1895+
// It should not be possible to change the value when there is an active transaction.
1896+
connection.setReadOnly(false);
1897+
connection.setAutocommit(false);
1898+
connection.execute(Statement.of(SELECT));
1899+
assertThrows(SpannerException.class, () -> connection.setRetryAbortsInternally(false));
1900+
// Verify that the value did not change.
1901+
assertTrue(connection.isRetryAbortsInternally());
1902+
1903+
// Rolling back the connection should allow us to set the property again.
1904+
connection.rollback();
1905+
connection.setRetryAbortsInternally(false);
1906+
assertFalse(connection.isRetryAbortsInternally());
1907+
}
1908+
}
1909+
18661910
private void assertThrowResultNotAllowed(
18671911
AbstractStatementParser parser, String sql, ImmutableSet<ResultType> allowedResultTypes) {
18681912
SpannerException exception =

0 commit comments

Comments
 (0)