Skip to content

Commit fab3f55

Browse files
committed
Add rejectNonTransactionalOperations to ReactiveFindByIdOperationSupport.
Closes #1494.
1 parent 9af74cf commit fab3f55

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/main/java/org/springframework/data/couchbase/core/ReactiveFindByIdOperationSupport.java

+16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import static com.couchbase.client.java.kv.GetAndTouchOptions.getAndTouchOptions;
1919
import static com.couchbase.client.java.transactions.internal.ConverterUtil.makeCollectionIdentifier;
2020

21+
import com.couchbase.client.core.msg.kv.DurabilityLevel;
22+
import com.couchbase.client.java.kv.PersistTo;
23+
import com.couchbase.client.java.kv.ReplicateTo;
2124
import reactor.core.publisher.Flux;
2225
import reactor.core.publisher.Mono;
2326

@@ -107,6 +110,7 @@ public Mono<T> one(final String id) {
107110
pArgs.getScope(), pArgs.getCollection(), null, null));
108111
}
109112
} else {
113+
rejectInvalidTransactionalOptions();
110114
return ctxOpt.get().getCore().get(makeCollectionIdentifier(rc.async()), id)
111115
.flatMap(result -> support.decodeEntity(id, new String(result.contentAsBytes(), StandardCharsets.UTF_8),
112116
result.cas(), domainType, pArgs.getScope(), pArgs.getCollection(),
@@ -129,6 +133,18 @@ public Mono<T> one(final String id) {
129133

130134
}
131135

136+
private void rejectInvalidTransactionalOptions() {
137+
if (this.expiry != null) {
138+
throw new IllegalArgumentException("withExpiry is not supported in a transaction");
139+
}
140+
if (this.options != null) {
141+
throw new IllegalArgumentException("withOptions is not supported in a transaction");
142+
}
143+
if (this.fields != null) {
144+
throw new IllegalArgumentException("project is not supported in a transaction");
145+
}
146+
}
147+
132148
@Override
133149
public Flux<? extends T> all(final Collection<String> ids) {
134150
return Flux.fromIterable(ids).flatMap(this::one);

src/test/java/org/springframework/data/couchbase/transactions/CouchbaseTransactionalUnsettableParametersIntegrationTests.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.function.Consumer;
2424
import java.util.function.Function;
2525

26-
import com.couchbase.client.core.msg.kv.DurabilityLevel;
2726
import org.junit.jupiter.api.BeforeAll;
2827
import org.junit.jupiter.api.BeforeEach;
2928
import org.junit.jupiter.api.DisplayName;
@@ -43,6 +42,8 @@
4342
import org.springframework.transaction.annotation.EnableTransactionManagement;
4443
import org.springframework.transaction.annotation.Transactional;
4544

45+
import com.couchbase.client.core.msg.kv.DurabilityLevel;
46+
import com.couchbase.client.java.kv.GetOptions;
4647
import com.couchbase.client.java.kv.InsertOptions;
4748
import com.couchbase.client.java.kv.PersistTo;
4849
import com.couchbase.client.java.kv.RemoveOptions;
@@ -177,6 +178,30 @@ public void removeWithOptions() {
177178
});
178179
}
179180

181+
@DisplayName("Using findById().withExpiry in a transaction is rejected at runtime")
182+
@Test
183+
public void findWithExpiry() {
184+
test((ops) -> {
185+
ops.replaceById(Person.class).withExpiry(Duration.ofSeconds(3)).one(WalterWhite);
186+
});
187+
}
188+
189+
@DisplayName("Using findById().project in a transaction is rejected at runtime")
190+
@Test
191+
public void findProject() {
192+
test((ops) -> {
193+
ops.findById(Person.class).project(new String[] { "someField" }).one(WalterWhite.id());
194+
});
195+
}
196+
197+
@DisplayName("Using findById().withOptions in a transaction is rejected at runtime")
198+
@Test
199+
public void findWithOptions() {
200+
test((ops) -> {
201+
ops.findById(Person.class).withOptions(GetOptions.getOptions()).one(WalterWhite.id());
202+
});
203+
}
204+
180205
@Service
181206
@Component
182207
@EnableTransactionManagement

0 commit comments

Comments
 (0)