-
Notifications
You must be signed in to change notification settings - Fork 52
feat: support isolation level repeatable read #1973
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/com/google/cloud/spanner/jdbc/IsolationLevelConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.spanner.jdbc; | ||
|
|
||
| import com.google.spanner.v1.TransactionOptions.IsolationLevel; | ||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import java.sql.SQLFeatureNotSupportedException; | ||
|
|
||
| class IsolationLevelConverter { | ||
| static IsolationLevel convertToSpanner(int jdbcIsolationLevel) throws SQLException { | ||
| switch (jdbcIsolationLevel) { | ||
| case Connection.TRANSACTION_SERIALIZABLE: | ||
| return IsolationLevel.SERIALIZABLE; | ||
| case Connection.TRANSACTION_REPEATABLE_READ: | ||
| return IsolationLevel.REPEATABLE_READ; | ||
| case Connection.TRANSACTION_READ_COMMITTED: | ||
| case Connection.TRANSACTION_READ_UNCOMMITTED: | ||
| case Connection.TRANSACTION_NONE: | ||
| throw new SQLFeatureNotSupportedException( | ||
| "Unsupported JDBC isolation level: " + jdbcIsolationLevel); | ||
| default: | ||
| throw new IllegalArgumentException("Invalid JDBC isolation level: " + jdbcIsolationLevel); | ||
| } | ||
| } | ||
|
|
||
| static int convertToJdbc(IsolationLevel isolationLevel) { | ||
| switch (isolationLevel) { | ||
| // Translate UNSPECIFIED to SERIALIZABLE as that is the default isolation level. | ||
| case ISOLATION_LEVEL_UNSPECIFIED: | ||
| case SERIALIZABLE: | ||
| return Connection.TRANSACTION_SERIALIZABLE; | ||
| case REPEATABLE_READ: | ||
| return Connection.TRANSACTION_REPEATABLE_READ; | ||
| default: | ||
| throw new IllegalArgumentException( | ||
| "Unknown or unsupported isolation level: " + isolationLevel); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/test/java/com/google/cloud/spanner/jdbc/IsolationLevelConverterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.spanner.jdbc; | ||
|
|
||
| import static com.google.cloud.spanner.jdbc.IsolationLevelConverter.convertToJdbc; | ||
| import static com.google.cloud.spanner.jdbc.IsolationLevelConverter.convertToSpanner; | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertThrows; | ||
|
|
||
| import com.google.spanner.v1.TransactionOptions.IsolationLevel; | ||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import java.sql.SQLFeatureNotSupportedException; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class IsolationLevelConverterTest { | ||
|
|
||
| @Test | ||
| public void testConvertToSpanner() throws SQLException { | ||
| assertEquals( | ||
| IsolationLevel.SERIALIZABLE, convertToSpanner(Connection.TRANSACTION_SERIALIZABLE)); | ||
| assertEquals( | ||
| IsolationLevel.REPEATABLE_READ, convertToSpanner(Connection.TRANSACTION_REPEATABLE_READ)); | ||
|
|
||
| assertThrows( | ||
| SQLFeatureNotSupportedException.class, | ||
| () -> convertToSpanner(Connection.TRANSACTION_READ_COMMITTED)); | ||
| assertThrows( | ||
| SQLFeatureNotSupportedException.class, | ||
| () -> convertToSpanner(Connection.TRANSACTION_READ_UNCOMMITTED)); | ||
| assertThrows( | ||
| SQLFeatureNotSupportedException.class, () -> convertToSpanner(Connection.TRANSACTION_NONE)); | ||
|
|
||
| assertThrows(IllegalArgumentException.class, () -> convertToSpanner(-1)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testConvertToJdbc() { | ||
| // There is no 'unspecified' isolation level in JDBC, so we convert this to the default | ||
| // SERIALIZABLE isolation level in Spanner. | ||
| assertEquals( | ||
| Connection.TRANSACTION_SERIALIZABLE, | ||
| convertToJdbc(IsolationLevel.ISOLATION_LEVEL_UNSPECIFIED)); | ||
| assertEquals(Connection.TRANSACTION_SERIALIZABLE, convertToJdbc(IsolationLevel.SERIALIZABLE)); | ||
| assertEquals( | ||
| Connection.TRANSACTION_REPEATABLE_READ, convertToJdbc(IsolationLevel.REPEATABLE_READ)); | ||
|
|
||
| assertThrows(IllegalArgumentException.class, () -> convertToJdbc(IsolationLevel.UNRECOGNIZED)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a specific reason we're defaulting to the Serialization isolation level when the Spanner isolation level isn't specified?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the reason is the following:
serializable). JDBC does not have anything like an 'unspecified isolation level'.ISOLATION_LEVEL_UNSPECIFIEDand this is the default that we are using in the Java client library. It basically means 'Spanner should use its own default'. At the moment, as a user, you cannot configure a default. So this means the default is alwaysserializable.ISOLATION_LEVEL_UNSPECIFIEDand JDBC does not have an equivalent, we have to convert it to one of the actual isolation levels. And the only logical choice is then to returnserializable, as that is the isolation level that Spanner will be using when you useISOLATION_LEVEL_UNSPECIFIED.