-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implementing Connection#beginTransaction(TransactionDefinition)…
… to support @transactional annotation (#738) Implemented the following classes and methods to conform to R2DBC SPI which enables supporting `@Transactional` annotation in [Spring Data Dialect](https://github.com/GoogleCloudPlatform/spring-cloud-gcp/tree/main/spring-cloud-spanner-spring-data-r2dbc). - `Connection#beginTransaction(TransactionDefinition)` method so that it can be used by a `TransactionManager` when `@Transactional` annotation is used. ✅ - `Connection#getTransactionIsolationLevel` returns SERIALIZABLE as it is the closest level to EXTERNAL_CONSISTENCY which spanner supports. ✅ - `Connection#setTransactionIsolationLevel(IsolationLevel)` accepts only `IsolationLevel.SERIALIZABLE` and throw `UnsupportedOperationException` otherwise. ✅ - Introduced `SpannerTransactionDefinition`, an implementation of `TransactionDefinition`(R2DBC spi) to configure the transaction attributes which will be used by `Connection#beginTransaction(TransactionDefinition)` method. ✅ Fixes: #238.
- Loading branch information
Showing
5 changed files
with
297 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
28 changes: 28 additions & 0 deletions
28
cloud-spanner-r2dbc/src/main/java/com/google/cloud/spanner/r2dbc/v2/SpannerConstants.java
This file contains 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,28 @@ | ||
/* | ||
* Copyright 2022-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 | ||
* | ||
* https://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.r2dbc.v2; | ||
|
||
import com.google.cloud.spanner.TimestampBound; | ||
import io.r2dbc.spi.Option; | ||
|
||
/** | ||
* Spanner Constants. | ||
*/ | ||
public class SpannerConstants { | ||
public static final Option<TimestampBound> TIMESTAMP_BOUND = Option.valueOf("timestampBound"); | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...r-r2dbc/src/main/java/com/google/cloud/spanner/r2dbc/v2/SpannerTransactionDefinition.java
This file contains 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,72 @@ | ||
/* | ||
* Copyright 2022-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 | ||
* | ||
* https://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.r2dbc.v2; | ||
|
||
import static com.google.cloud.spanner.r2dbc.v2.SpannerConstants.TIMESTAMP_BOUND; | ||
import static java.lang.Boolean.TRUE; | ||
|
||
import io.r2dbc.spi.Option; | ||
import io.r2dbc.spi.TransactionDefinition; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* An implementation of {@link TransactionDefinition} for Spanner Database. | ||
*/ | ||
public class SpannerTransactionDefinition implements TransactionDefinition { | ||
|
||
private final Map<Option<?>, Object> internalMap; | ||
|
||
SpannerTransactionDefinition(Map<Option<?>, Object> internalMap) { | ||
validate(internalMap); | ||
this.internalMap = internalMap; | ||
} | ||
|
||
private void validate(Map<Option<?>, Object> internalMap) { | ||
boolean isReadOnlyTransaction = TRUE.equals(internalMap.get(READ_ONLY)); | ||
if (!isReadOnlyTransaction && internalMap.containsKey(TIMESTAMP_BOUND)) { | ||
throw new IllegalArgumentException("TIMESTAMP_BOUND can only be configured for" | ||
+ " read only transactions."); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T getAttribute(Option<T> option) { | ||
return (T) this.internalMap.get(option); | ||
} | ||
|
||
|
||
/** | ||
* A builder class for {@link SpannerTransactionDefinition}. | ||
*/ | ||
public static class Builder { | ||
private final Map<Option<?>, Object> internalMap; | ||
|
||
public Builder() { | ||
this.internalMap = new HashMap<>(); | ||
} | ||
|
||
public <T> Builder with(Option<T> option, T value) { | ||
this.internalMap.put(option, value); | ||
return this; | ||
} | ||
|
||
public SpannerTransactionDefinition build() { | ||
return new SpannerTransactionDefinition(this.internalMap); | ||
} | ||
} | ||
} |
This file contains 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
44 changes: 44 additions & 0 deletions
44
...dbc/src/test/java/com/google/cloud/spanner/r2dbc/v2/SpannerTransactionDefinitionTest.java
This file contains 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,44 @@ | ||
/* | ||
* Copyright 2022-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 | ||
* | ||
* https://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.r2dbc.v2; | ||
|
||
import static com.google.cloud.spanner.r2dbc.v2.SpannerConstants.TIMESTAMP_BOUND; | ||
import static io.r2dbc.spi.TransactionDefinition.READ_ONLY; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import com.google.cloud.spanner.TimestampBound; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.jupiter.api.Test; | ||
|
||
|
||
|
||
class SpannerTransactionDefinitionTest { | ||
|
||
@Test | ||
void shouldThrowExceptionIfTimeStampBoundIsConfiguredWithReadWriteTransaction() { | ||
SpannerTransactionDefinition.Builder builder1 = new SpannerTransactionDefinition.Builder() | ||
.with(TIMESTAMP_BOUND, TimestampBound.ofExactStaleness(5, TimeUnit.SECONDS)) | ||
.with(READ_ONLY, false); | ||
|
||
// absence of READ_ONLY attribute indicates read write transaction | ||
SpannerTransactionDefinition.Builder builder2 = new SpannerTransactionDefinition.Builder() | ||
.with(TIMESTAMP_BOUND, TimestampBound.ofExactStaleness(5, TimeUnit.SECONDS)); | ||
|
||
assertThrows(IllegalArgumentException.class, builder1::build); | ||
assertThrows(IllegalArgumentException.class, builder2::build); | ||
} | ||
} |