Concurrency Using STM

Transactions are nice, but what happens if two transactions try to change the same identity? Sorry, I didn’t mean to keep you on the edge of your seat this long. We’ll take a look at a couple of examples of that in this section.

A word of caution, though, before we walk through the examples. In production code, make sure that transactions are idempotent and don’t have any side effects, because they may be retried a number of times. That means no printing to the console, no logging, no sending out emails, and no making any irreversible operations within transactions. If we do any of these, we’re responsible for reversing the operations or dealing with the consequences. In general, it’s better to gather up these side-effect ...

Get Programming Concurrency on the JVM now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.