1
0

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
This commit is contained in:
2026-09-08 16:36:32 +00:00
parent 879a06929a
commit 585eed4d57
53 changed files with 2278 additions and 68 deletions

View File

@@ -0,0 +1,157 @@
package com.ankurm.sdjpa4demo;
import com.ankurm.sdjpa4demo.domain.Author;
import com.ankurm.sdjpa4demo.domain.Book;
import com.ankurm.sdjpa4demo.domain.BookSummary;
import com.ankurm.sdjpa4demo.domain.Money;
import com.ankurm.sdjpa4demo.repo.AuthorRepository;
import com.ankurm.sdjpa4demo.repo.BookRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.JpaSort;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.List;
import static com.ankurm.sdjpa4demo.repo.AuthorSpecifications.byCountryDelete;
import static com.ankurm.sdjpa4demo.repo.AuthorSpecifications.hasCountry;
import static com.ankurm.sdjpa4demo.repo.AuthorSpecifications.relabelCountry;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@ActiveProfiles("test")
@Transactional
class MigrationBehaviorTests {
@Autowired AuthorRepository authors;
@Autowired BookRepository books;
private void seed() {
Author a1 = authors.save(new Author("Joshua Bloch", "US"));
authors.save(new Author("Anonymous", null));
books.save(new Book("Effective Java", new Money(new BigDecimal("45.00"), "USD"), a1));
books.save(new Book("Refactoring", new Money(new BigDecimal("45.00"), "USD"), a1));
}
@Test
void optionalMissingReturnsEmpty() {
seed();
assertTrue(authors.findByName("nope").isEmpty());
}
@Test
void nonNullMissingThrows() {
seed();
assertThrows(EmptyResultDataAccessException.class, () -> authors.getByName("nope"));
}
@Test
void nullMarkedRejectsNullArgument() {
seed();
assertThrows(IllegalArgumentException.class, () -> authors.getByName(null));
}
@Test
void nullableParamAcceptsNull() {
seed();
assertDoesNotThrow(() -> authors.findByCountry(null));
}
@Test
void nullsPrecedenceIsHonored() {
seed();
List<String> first = authors.findByNameStartingWith("",
Sort.by(Sort.Order.asc("country").nullsFirst()))
.stream().map(Author::getCountry).toList();
assertNull(first.get(0), "NULLS FIRST should put null country first");
}
@Test
void nonUniqueSingleResultThrows() {
seed();
assertThrows(IncorrectResultSizeDataAccessException.class,
() -> books.findByPriceAmount(new BigDecimal("45.00")));
}
@Test
void newJpqlFunctionsWork() {
Author a = authors.save(new Author("Erich Gamma", "CH"));
assertEquals("Erich_Gamma:CH", authors.badgeFor(a.getId()));
}
// --- Corner scenarios: refined Specification API (DeleteSpecification / UpdateSpecification / PredicateSpecification) ---
@Test
void predicateSpecificationReusedAcrossReadAndDelete() {
authors.save(new Author("Grady Booch", "US"));
authors.save(new Author("Robert Martin", "US"));
authors.save(new Author("Martin Fowler", "UK"));
assertEquals(2, authors.findAll(hasCountry("US")).size());
long removed = authors.delete(hasCountry("US"));
assertEquals(2, removed);
assertTrue(authors.findAll(hasCountry("US")).isEmpty());
}
@Test
void deleteSpecificationBulkDelete() {
authors.save(new Author("Kent Beck", "DE"));
authors.save(new Author("Erich Gamma", "CH"));
long removed = authors.delete(byCountryDelete("DE"));
assertEquals(1, removed);
assertTrue(authors.findByCountry("DE") == null);
}
@Test
void updateSpecificationBulkUpdate() {
authors.save(new Author("Martin Fowler", "UK"));
authors.save(new Author("Sam Newman", "UK"));
long updated = authors.update(relabelCountry("UK", "GB"));
assertEquals(2, updated);
assertEquals(2, authors.findAll(hasCountry("GB")).size());
assertTrue(authors.findAll(hasCountry("UK")).isEmpty());
}
@Test
void jpaSortUnsafeCaseExpressionOrdersResults() {
authors.save(new Author("Alpha Author", "DE"));
authors.save(new Author("Beta Author", "US"));
Sort usFirst = JpaSort.unsafe(Sort.Direction.ASC, "CASE WHEN country = 'US' THEN 0 ELSE 1 END");
List<Author> ordered = authors.findByNameStartingWith("", usFirst);
assertEquals("US", ordered.get(0).getCountry());
}
// --- Corner scenarios: Money embeddable value object ---
@Test
void embeddedValueObjectPathTraversalWorks() {
seed();
List<Book> found = books.findByPriceAmountGreaterThanEqual(new BigDecimal("45.00"));
assertEquals(2, found.size());
}
@Test
void recordProjectionPopulatesFromEmbeddedPath() {
Author a = authors.save(new Author("Kathy Sierra", "US"));
books.save(new Book("Head First Java", new Money(new BigDecimal("39.99"), "USD"), a));
List<BookSummary> summaries = books.findByPriceAmountLessThanEqual(new BigDecimal("40.00"));
assertEquals(1, summaries.size());
assertEquals("Head First Java", summaries.get(0).title());
assertEquals(new BigDecimal("39.99"), summaries.get(0).amount());
}
}