-
Notifications
You must be signed in to change notification settings - Fork 50
feat: support Savepoint #1212
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
feat: support Savepoint #1212
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
import com.google.cloud.spanner.TimestampBound; | ||
import com.google.cloud.spanner.connection.AutocommitDmlMode; | ||
import com.google.cloud.spanner.connection.ConnectionOptions; | ||
import com.google.cloud.spanner.connection.SavepointSupport; | ||
import com.google.cloud.spanner.connection.TransactionMode; | ||
import com.google.common.collect.Iterators; | ||
import java.sql.Array; | ||
|
@@ -33,6 +34,7 @@ | |
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Savepoint; | ||
import java.sql.Statement; | ||
import java.sql.Timestamp; | ||
import java.util.HashMap; | ||
|
@@ -402,6 +404,70 @@ public String getSchema() throws SQLException { | |
return ""; | ||
} | ||
|
||
@Override | ||
public SavepointSupport getSavepointSupport() throws SQLException { | ||
checkClosed(); | ||
return getSpannerConnection().getSavepointSupport(); | ||
} | ||
|
||
@Override | ||
public void setSavepointSupport(SavepointSupport savepointSupport) throws SQLException { | ||
checkClosed(); | ||
try { | ||
getSpannerConnection().setSavepointSupport(savepointSupport); | ||
} catch (SpannerException e) { | ||
throw JdbcSqlExceptionFactory.of(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Savepoint setSavepoint() throws SQLException { | ||
checkClosed(); | ||
try { | ||
JdbcSavepoint savepoint = JdbcSavepoint.unnamed(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's your take on Usually with interfaces we don't expose the implementation class at time of dependency injection. Over here it's not really dependency injection, but would it not be better to use just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to use the |
||
getSpannerConnection().savepoint(savepoint.internalGetSavepointName()); | ||
return savepoint; | ||
} catch (SpannerException e) { | ||
throw JdbcSqlExceptionFactory.of(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Savepoint setSavepoint(String name) throws SQLException { | ||
checkClosed(); | ||
try { | ||
JdbcSavepoint savepoint = JdbcSavepoint.named(name); | ||
getSpannerConnection().savepoint(savepoint.internalGetSavepointName()); | ||
return savepoint; | ||
} catch (SpannerException e) { | ||
throw JdbcSqlExceptionFactory.of(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void rollback(Savepoint savepoint) throws SQLException { | ||
checkClosed(); | ||
JdbcPreconditions.checkArgument(savepoint instanceof JdbcSavepoint, savepoint); | ||
JdbcSavepoint jdbcSavepoint = (JdbcSavepoint) savepoint; | ||
try { | ||
getSpannerConnection().rollbackToSavepoint(jdbcSavepoint.internalGetSavepointName()); | ||
Comment on lines
+451
to
+453
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar comments on usage of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above: We need access to the |
||
} catch (SpannerException e) { | ||
throw JdbcSqlExceptionFactory.of(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void releaseSavepoint(Savepoint savepoint) throws SQLException { | ||
checkClosed(); | ||
JdbcPreconditions.checkArgument(savepoint instanceof JdbcSavepoint, savepoint); | ||
JdbcSavepoint jdbcSavepoint = (JdbcSavepoint) savepoint; | ||
try { | ||
getSpannerConnection().releaseSavepoint(jdbcSavepoint.internalGetSavepointName()); | ||
} catch (SpannerException e) { | ||
throw JdbcSqlExceptionFactory.of(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Timestamp getCommitTimestamp() throws SQLException { | ||
checkClosed(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2023 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 java.sql.SQLException; | ||
import java.sql.Savepoint; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
class JdbcSavepoint implements Savepoint { | ||
private static final AtomicInteger COUNTER = new AtomicInteger(); | ||
|
||
static JdbcSavepoint named(String name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason we are preferring static methods for instantiation instead of adding constructors for creating named and unnamed instances? Wouldn't the constructor be a more natural way for creating instance instead of static methods? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This gives more readable code where the savepoints are created, especially as the behavior of named and unnamed savepoints are slightly different. Internally, we always need to create savepoints with a name, as that is the only thing that Cloud Spanner supports. This way we make clear that one is a named savepoint, and the other is unnamed, even though both internally have a name. |
||
return new JdbcSavepoint(-1, name); | ||
} | ||
|
||
static JdbcSavepoint unnamed() { | ||
int id = COUNTER.incrementAndGet(); | ||
return new JdbcSavepoint(id, String.format("s_%d", id)); | ||
} | ||
|
||
private final int id; | ||
private final String name; | ||
|
||
private JdbcSavepoint(int id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public int getSavepointId() throws SQLException { | ||
JdbcPreconditions.checkState(this.id >= 0, "This is a named savepoint"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion : It might be more readable to track named vs un-named through an explicit boolean variable say There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't feel it's really necessary given the size of the class. Also; changing the type of ID from integer to anything else will not be possible, as the type is specified by the JDBC API (that is; |
||
return id; | ||
} | ||
|
||
@Override | ||
public String getSavepointName() throws SQLException { | ||
JdbcPreconditions.checkState(this.id < 0, "This is an unnamed savepoint"); | ||
return name; | ||
} | ||
|
||
String internalGetSavepointName() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a separate method from
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the explanation. |
||
return name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2023 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 org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThrows; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.sql.SQLException; | ||
import java.sql.Savepoint; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class JdbcSavepointTest { | ||
|
||
@Test | ||
public void testNamed() throws SQLException { | ||
Savepoint savepoint = JdbcSavepoint.named("test"); | ||
assertEquals("test", savepoint.getSavepointName()); | ||
assertThrows(SQLException.class, savepoint::getSavepointId); | ||
} | ||
|
||
@Test | ||
public void testUnnamed() throws SQLException { | ||
Savepoint savepoint = JdbcSavepoint.unnamed(); | ||
assertTrue( | ||
String.format("Savepoint id: %d", savepoint.getSavepointId()), | ||
savepoint.getSavepointId() > 0); | ||
assertThrows(SQLException.class, savepoint::getSavepointName); | ||
} | ||
} |
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.
Have we intentionally skipped
checkClosed();
validation in this method? If yes, I didn't get why we won't need the check here.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.
Not really intentionally. Added it to be consistent with the other methods.