TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to Spring transactions (@Transactional)

๐Ÿ“ Spring transactions (@Transactional): cheat sheet

๐ŸŽจ The analogyA notary's clerk who opens a contract folder before the work and seals or shreds it at the end.
๐ŸŽ“ The regular explanationHow Spring wraps a bean in a proxy that begins, commits or rolls back a database transaction around each @Transactional call, and how propagation, rollback rules, isolation, read-only hints and after-commit listeners shape that behaviour.
@Transactional works through a proxyStep 6 of 6
Records roomo1key order 1 and its stock change: o10123๐Ÿง‘Customer๐Ÿง‘โ€๐Ÿ’ผClerk at the door๐Ÿง‘โ€โš–๏ธNotary๐Ÿ“ Contract foldershredded
  1. A customer brings a job. They talk to the clerk at the door, never to the notary directly.
  2. Before any work starts, the clerk opens a fresh contract folder for this job.
  3. The clerk lets the notary work. Every page the notary writes goes into the open folder.
  4. The notary finishes, so the clerk seals the folder and files it. Both pages count at once.
  5. The next job starts the same way, with a new folder. But this time the notary spills ink halfway.
  6. The clerk shreds the folder and tells the customer it went wrong. Nothing reaches the records room, as if the job never happened.
Self-invocation skips the proxyStep 5 of 5
Records room ยท half filedr1key row 1: r10r2key row 2: r2123๐Ÿง‘Customer๐Ÿง‘โ€๐Ÿ’ผClerk at the doorno folder๐Ÿง‘โ€โš–๏ธNotary๐Ÿ‘ฉโ€๐Ÿ’ผSecond clerkfolder open๐Ÿ‘จโ€โš–๏ธSecond notary
  1. A customer brings a big job that doesn't need a folder. The clerk sees that and waves it straight through.
  2. Halfway through, the notary does a part that should have a folder, but does it right there at the same desk. Nobody comes through the door.
  3. With no folder open, every page goes straight to the records room the moment it's written.
  4. The notary trips on the third page. There's no folder to shred, so the first two pages stay filed.
  5. The fix is to hand that part to a second notary in another room. Their clerk opens a folder, so it's all or nothing again.
Which exceptions roll backStep 4 of 4
ruleRecords roomhalkey half-finished transfer: hal0123๐Ÿง‘โ€๐Ÿ’ผClerk at the doorshred๐Ÿง‘โ€โš–๏ธNotary๐Ÿ“Œ Note to the clerk
  1. The notary spills ink, an accident nobody planned for. The clerk shreds the folder, and nothing is filed.
  2. Next time the notary raises a complaint they warned about in advance. The clerk seals the folder anyway, half-finished pages and all.
  3. To stop that, you pin a note on the clerk's desk saying "shred for complaints too".
  4. The same complaint comes up again. This time the clerk reads the note and shreds the folder, so nothing new is filed.
REQUIRED joins, REQUIRES_NEW starts its ownStep 6 of 6
joins๐Ÿง‘โ€โš–๏ธLead notaryspill๐Ÿ’ฐPayments notary๐Ÿ“’Logbook notary๐Ÿ“ Folder Ashredded๐Ÿ“‚ Folder Bsealed
  1. The lead notary's clerk opens folder A for a house sale, and the first page goes in.
  2. The payments notary's clerk sees folder A is already open and hands it straight in. The payment page lands in the same folder.
  3. The logbook notary's note says "always use your own folder". Their clerk puts folder A aside and opens folder B.
  4. The logbook entry is done, so folder B is sealed straight away and folder A is picked up again.
  5. Then the lead notary spills ink on the sale. Folder A is shared, so the payment pages are in it too.
  6. Folder A is shredded, sale and payment together. Folder B was already sealed, so the logbook entry stays.
Lazy loading after the transaction endsStep 6 of 6
๐Ÿง‘Customerall here๐Ÿง‘โ€๐Ÿ’ผClerk at the door๐Ÿง‘โ€โš–๏ธNotary๐Ÿ“ Contract foldersealed๐Ÿ—„๏ธ Records room
  1. The customer asks for the papers for order 7. The clerk opens a folder and the notary goes to the records room.
  2. The records room hands over the papers. To keep it light, the page about items just says "see the records room".
  3. The papers go back to the customer, and the clerk seals the folder behind it.
  4. Now the customer asks what the items were. The folder is sealed, so the records room won't hand anything over.
  5. The fix is to fetch everything the customer needs while the folder is still open, then hand it over complete.
  6. The complete papers travel back through the clerk, who seals the folder. The customer has everything it needs.
Work that waits for the commitStep 5 of 5
๐Ÿง‘โ€โš–๏ธNotary๐Ÿ“ Contract foldershredded๐ŸƒMessengerletter binned๐Ÿ“ฎPost box
  1. The notary writes the sale into the open folder, then hands the messenger a letter for the buyer. The messenger waits.
  2. The job finishes and the folder is sealed and filed.
  3. Only now does the messenger post the letter, so the buyer never hears about a sale that didn't happen.
  4. For the next sale, the notary again writes the page and hands over a letter, and the messenger waits.
  5. This time something goes wrong and the folder is shredded. The messenger throws the letter away, so no false news goes out.

๐Ÿงฉ Key questions

Halfway through a job, the notary does a part marked "needs a folder" right there at the same desk. What does the clerk do?
Nothing: nobody came through the door, so the clerk never sees it and no folder is opened. The clerk only handles jobs that come in through the door. Hand that part to a second notary with their own clerk.
The notary stops with a complaint they warned about in advance, and the clerk has no extra note. What happens to the folder?
It's sealed anyway, half-finished pages and all. By default only spills shred the folder. Leave the note "shred for complaints too" to change that.
The logbook notary always uses their own folder. Later the lead notary's folder A is shredded. What happens to the logbook entry?
It stays, because folder B was already sealed on its own. Folder A was put aside while folder B was written and sealed, so the two are separate.
The office doesn't keep folders open until the customer leaves. The customer gets the papers after their folder is sealed, then asks for the items the papers only point to. What happens?
Nobody can fetch them, because the records room only hands things over while the folder is open. Fetch everything the customer will need while the folder is still open.
The notary hands the messenger a letter for the buyer, then the folder is shredded. What does the messenger do?
Throws the letter away, so it's never posted. The messenger only posts letters for folders that were sealed and filed.