1
0
Files
sdjpa4-demo/transactions/scripts/demo-propagation.sh
Ankur Mhatre 585eed4d57 Add the transactions module, and move the migration project under migration-behavior/
The repository now aggregates two independent modules. migration-behavior/ is the
original project, moved unchanged; it stays on Spring Boot 4.0.6 / JDK 21 because
that is what the four published migration articles were verified against, and
upgrading it would silently invalidate output they quote. The article-tagged trees
are untouched, so links into a tag are unaffected.

transactions/  Companion code for "@Transactional in Spring: Propagation, Isolation,
and the Six Ways It Silently Does Nothing". Spring Boot 4.1.1 / JDK 25.

Every row of the propagation matrix is produced by calling the method and asking the
transaction manager what it did. The transaction NAME is the exhibit: a scope that
joined reports its caller's name, a scope that started its own reports its own.

Three things the transcripts settle:

  - Propagation.NESTED cannot be used with JpaTransactionManager. It fails twice,
    with two different messages, the second of which blames your JPA provider. The
    savepoint manager comes from the object the JpaDialect returns when it begins the
    transaction, and Hibernate's does not implement one. It works on
    DataSourceTransactionManager, because a savepoint is a JDBC concept -- shown
    working there rather than only failing here.
  - Catching a REQUIRED inner failure does not save the transaction. The inner scope
    has already marked it rollback-only, so the commit throws
    UnexpectedRollbackException from a place with no connection to the cause.
  - A checked exception commits, and so does a swallowed one. Those two do not merely
    fail to start a transaction; they commit work the code was abandoning.

19 contract tests, six captured transcripts, all regenerated by scripts/run-all.sh.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gip4srpzMwjgoba6uEfbr5
2026-09-08 16:47:51 +00:00

61 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# All seven propagation values, each called with and without an outer transaction.
set -euo pipefail
set +m
cd "$(dirname "$0")/.."
source scripts/env.sh
{
echo "== the propagation matrix =="
echo
echo "Each inner method is called twice: once from a @Transactional caller and once from a"
echo "plain one. 'active' is TransactionSynchronizationManager.isActualTransactionActive();"
echo "'name' is the transaction's name, which is how you tell JOINING from STARTING -- a"
echo "joining method reports the OUTER method's name."
echo
start_app > /dev/null
curl -s "http://127.0.0.1:${APP_PORT}/tx/propagation" | python3 -c '
import json,sys
d=json.load(sys.stdin)
print(" %-14s %-24s %-8s %s" % ("PROPAGATION","CALLER","ACTIVE","TRANSACTION NAME / OUTCOME"))
print(" " + "-"*76)
for name,row in d.items():
for ctx,label in (("withOuterTransaction","inside @Transactional"),
("withoutOuterTransaction","no transaction")):
v=row[ctx]
if "exception" in v:
print(" %-14s %-24s %-8s %s" % (name, label, "--", v["exception"].split(".")[-1]))
else:
i=v["inner"]
print(" %-14s %-24s %-8s %s" % (name, label, i["actualTransactionActive"],
str(i["transactionName"]).split(".")[-1]))
print()'
echo
echo "Reading it:"
echo " REQUIRED inside a transaction the inner name is the OUTER method -- it joined."
echo " REQUIRES_NEW the inner name is its own method -- it started a second transaction."
echo " NESTED fails outright on JpaTransactionManager. See docs/output/03-nested.txt."
echo " SUPPORTS joins if there is one, runs with none if there is not. No transaction"
echo " is created, so the write below it lands on an auto-commit connection."
echo " NOT_SUPPORTED suspends the outer transaction: active=False even inside one."
echo " MANDATORY requires a caller's transaction; IllegalTransactionStateException if none."
echo " NEVER requires the absence of one; IllegalTransactionStateException if present."
echo
echo "== the exact exception messages =="
curl -s "http://127.0.0.1:${APP_PORT}/tx/propagation" | python3 -c '
import json,sys
for name,row in json.load(sys.stdin).items():
for ctx in ("withOuterTransaction","withoutOuterTransaction"):
v=row[ctx]
if "exception" in v:
print(" %s (%s)" % (name, ctx))
print(" %s" % v["exception"])
print(" %s" % v["message"])
print()'
echo "== what the transaction manager logged while doing it =="
echo
grep -E "Creating new transaction|Participating in existing|Suspending current|Initiating transaction|Not creating" "$LOG" \
| tidy | head -24
stop_app
} > docs/output/01-propagation.txt 2>&1
cat docs/output/01-propagation.txt