A database transaction groups operations into an atomic unit of work: they either succeed together or fail together. Its boundaries also help isolate concurrent units of work from one another. section 11.0 section 11.1.2
Completing an auction
Closing an auction may require finding the winning bid, charging the seller, and notifying both parties. If all actions must succeed or none may succeed, they form one unit of work. Because billing involves an external system, coordinating the whole operation spans more than a single database transaction. section 11.1 section 11.1.2
Transaction demarcation sets the start and end of a unit of work. Begin the transaction, perform database operations, and commit the changes; if an operation or commit fails, roll back and handle the error. Boundaries can be defined programmatically or declaratively. Keep database transactions short: open transactions consume resources and may hold exclusive locks that restrict concurrent access. section 11.1.2 section 10.2.1
Listing 10.1 A typical unit of work
Path: managing-data/src/test/java/com/manning/javapersistence/ch10 /SimpleTransitionsTest.java EntityManagerFactory emf = Persistence.createEntityManagerFactory("ch10"); // . . . EntityManager em = emf.createEntityManager(); \1 { em.getTransaction().begin(); // . . . em.getTransaction().commit(); } catch (Exception ex) { // Transaction rollback, exception handling // . . . } finally { if (em != null && em.isOpen()) em.close(); }
A programmatic Jakarta Persistence unit of work begins and commits through an EntityManager, with rollback and exception handling on failure and closure in a finally block. section 10.2.1
An EntityManager has a persistence context that tracks changes during a unit of work. Hibernate obtains a JDBC connection when SQL is needed and synchronizes tracked changes with the database at commit; flush() can request synchronization earlier within a transaction. The persistence context lasts until the EntityManager is closed, so its scope is chosen separately from the transaction boundary. section 10.1 section 10.2.1
Isolation is a tradeoff: weaker isolation can improve performance and scalability but permits concurrency problems. Examples include lost updates, dirty reads, unrepeatable reads, and phantom reads. Choose an isolation level with the anomalies the application must prevent in mind. section 11.2.1
Table 11.1 ANSI isolation levels and the problems that they address (view table figure)
Compare ANSI isolation levels by the concurrency problems they address. section 11.2.1
Spring supports programmatic transactions through TransactionTemplate and declarative transactions through annotations. By default, a Spring transaction rolls back for RuntimeException; @Transactional properties such as rollbackFor and noRollbackFor can change the rules. section 11.4 section 11.4.2
Transactional Spring tests roll back by default. Use @Commit or @Rollback(false) when a test should commit instead. A separately committed transaction can leave data behind even when the test transaction rolls back. section 20.5
Reading without an explicit transaction can use auto-commit, but an unsynchronized persistence context does not automatically flush changes. In that mode, attempting to flush or acquire a lock other than NONE raises TransactionRequiredException; refresh() can replace an in-memory change with the current database state. section 11.3.1














