Skip to content

Commit b156904

Browse files
authored
moves transaction validation into the transaction (#26)
Co-authored-by: chedim <>
1 parent d30e401 commit b156904

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/main/java/org/couchbase/quickstart/controllers/ProfileController.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,6 @@ public ResponseEntity transferCredits(
176176
return ResponseEntity.status(500).body("Target profile not found");
177177
}
178178

179-
if (sourceProfile.getBalance() < amount) {
180-
return ResponseEntity.status(500).body("Insufficient balance");
181-
}
182-
183179
TransactionOptions to = TransactionOptions.transactionOptions();
184180
TransactionQueryOptions args = TransactionQueryOptions.queryOptions().parameters(
185181
JsonObject.create()
@@ -191,9 +187,12 @@ public ResponseEntity transferCredits(
191187
while(true) {
192188
try {
193189
cluster.transactions().run(ctx -> {
194-
195190
ctx.query("UPDATE `"+dbProperties.getBucketName()+"`.`_default`.`"+PROFILE+"` SET balance = balance - $amount WHERE pid = $source", args);
196191
ctx.query("UPDATE `"+dbProperties.getBucketName()+"`.`_default`.`"+PROFILE+"` SET balance = balance + $amount WHERE pid = $target", args);
192+
if (sourceProfile.getBalance() < amount) {
193+
throw new RuntimeException("Insufficient balance");
194+
}
195+
197196
}, to);
198197

199198
break;

0 commit comments

Comments
 (0)