Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void setTransactionIsolation(int level) throws SQLException {
@Override
public int getTransactionIsolation() throws SQLException {
checkClosed();
IsolationLevel isolationLevel = spanner.getDefaultIsolationLevel();
IsolationLevel isolationLevel = spanner.getTransactionIsolationLevel();
if (isolationLevel == IsolationLevel.ISOLATION_LEVEL_UNSPECIFIED) {
return JdbcDatabaseMetaData.getDatabaseDefaultTransactionIsolation(getDatabaseClient());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ public void testUsesManualIsolationLevel() throws SQLException {
}
}

@Test
public void testReportsCurrentTransactionIsolationLevel() throws SQLException {
try (Connection connection = createJdbcConnection();
Statement statement = connection.createStatement()) {
connection.setAutoCommit(true);
connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
assertEquals(Connection.TRANSACTION_SERIALIZABLE, connection.getTransactionIsolation());

statement.execute("begin transaction isolation level repeatable read");
assertEquals(Connection.TRANSACTION_REPEATABLE_READ, connection.getTransactionIsolation());

statement.execute("rollback");
assertEquals(Connection.TRANSACTION_SERIALIZABLE, connection.getTransactionIsolation());
}
}

@Test
public void testUsesDefaultIsolationLevelInConnectionString() throws SQLException {
for (IsolationLevel isolationLevel :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ <T> T getConnectionPropertyValue(
/** Returns the default isolation level for read/write transactions for this connection. */
IsolationLevel getDefaultIsolationLevel();

/** Returns the isolation level of the current transaction, or the connection default. */
default IsolationLevel getTransactionIsolationLevel() {
return getDefaultIsolationLevel();
}

/** Sets the read lock mode for read/write transactions for this connection. */
void setReadLockMode(ReadLockMode readLockMode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -930,11 +930,9 @@ public void setTransactionMode(TransactionMode transactionMode) {
this.unitOfWorkType = UnitOfWorkType.of(transactionMode);
}

IsolationLevel getTransactionIsolationLevel() {
ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
ConnectionPreconditions.checkState(!isDdlBatchActive(), "This connection is in a DDL batch");
ConnectionPreconditions.checkState(isInTransaction(), "This connection has no transaction");
return this.transactionIsolationLevel;
@Override
public IsolationLevel getTransactionIsolationLevel() {
return isInTransaction() ? transactionIsolationLevel : getDefaultIsolationLevel();
}
Comment on lines +934 to 936

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To maintain consistency with other methods in ConnectionImpl and ensure that operations on a closed connection fail fast, we should preserve the check that the connection is not closed.

Suggested change
public IsolationLevel getTransactionIsolationLevel() {
return isInTransaction() ? transactionIsolationLevel : getDefaultIsolationLevel();
}
public IsolationLevel getTransactionIsolationLevel() {
ConnectionPreconditions.checkState(!isClosed(), CLOSED_ERROR_MSG);
return isInTransaction() ? transactionIsolationLevel : getDefaultIsolationLevel();
}


void setTransactionIsolationLevel(IsolationLevel isolationLevel) {
Expand Down
Loading