# How can i use the operation of CAS in Java Client

**URL:** <https://forums.foundationdb.org/t/how-can-i-use-the-operation-of-cas-in-java-client/3543>\
**Category:** Using FoundationDB\
**Tags:** bindings\
**Created:** [September 7, 2022, 6:10am UTC](https://forums.foundationdb.org/t/how-can-i-use-the-operation-of-cas-in-java-client/3543 "2022-09-07T06:10:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![T-TRz879](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/t-trz879/32/1418_2.png) [@T-TRz879](https://forums.foundationdb.org/u/T-TRz879)\
**Post date:** [September 7, 2022, 6:10am UTC](https://forums.foundationdb.org/t/how-can-i-use-the-operation-of-cas-in-java-client/3543/1 "2022-09-07T06:10:46Z")

</div>

I can’t find this description in the tutorial

---

<div class="post-metadata">

**Author:** ![andrew.noyes](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/andrew.noyes/32/443_2.png) [@andrew.noyes](https://forums.foundationdb.org/u/andrew.noyes)\
**Post date:** [September 7, 2022, 5:32pm UTC](https://forums.foundationdb.org/t/how-can-i-use-the-operation-of-cas-in-java-client/3543/2 "2022-09-07T17:32:18Z")

</div>

_Probably_ what you want is to achieve a compare and swap with serializable reads and writes in a normal transaction.

There is a mutation called [COMPARE\_AND\_CLEAR](https://apple.github.io/foundationdb/javadoc/com/apple/foundationdb/MutationType.html#COMPARE_AND_CLEAR), which is kind of similar but the problem is that if the value of the key is _not_ what’s expected at commit time, the transaction still commits successfully and the COMPARE\_AND\_CLEAR mutation has no effect. Generally people seem to want the transaction to fail with `not_committed` in this case, but the only way to get a transaction to fail if the value of a key is unexpected at commit time is to read the key \[1\]. You can see some more explanation about why we can’t easily know the result of the atomic mutation here: [Efficient way to create if absent in FDB](https://forums.foundationdb.org/t/efficient-way-to-create-if-absent-in-fdb/2838).

Hope that helps. Let me know if I can clarify anything.

* * *

1. Depending on your data model, it’s possible that you can infer the value of a key at your transaction’s read version without actually reading it, and in that case you only need to add a read conflict on that key. Be sure to add the read conflict before modifying the key - see [Adding an explicit read conflict range skips the keys in write cache · Issue #126 · apple/foundationdb · GitHub](https://github.com/apple/foundationdb/issues/126) for a discussion of why.
