liveBook · Manning

Transactional annotation

As explained in Java Persistence with Spring Data and Hibernate epub

The Spring Transactional annotation declares that method or class execution participates in transaction management.

The Spring @Transactional annotation declares that method or class execution participates in transaction management. It defines boundaries and execution rules for atomic database operations. section 11.4.3 section 20.5

Configure transaction behavior with these properties:

  • isolation selects an ANSI isolation level, while propagation controls how existing transactions are handled. section 11.4.3
  • timeout specifies seconds before automatic rollback, and readOnly identifies read-only work that may allow optimizations. section 11.4.3
  • rollbackFor and rollbackForClassName add exception types that cause rollback; noRollbackFor and noRollbackForClassName exclude exception types. section 11.4.2

Apply the annotation to an interface, class, or method. Class- and interface-level declarations are inherited by their methods, while method annotations can override broader behavior. Method-level annotations on concrete classes provide the finest control. section 11.4.3

Transactional DAO

@Repository @Transactional public class ItemDaoImpl implements ItemDao { @PersistenceContext(type = PersistenceContextType.EXTENDED) private EntityManager em; public void insert(Item item) { em.persist(item); } public Item getById(long id) { return em.find(Item.class, id); } } Enable annotation-driven transaction management with @EnableTransactionManagement, or use the XML tx:annotation-driven element, and provide a transaction manager bean. section 14.2

For tests, placing @Transactional on the test class makes transactions active during each test method and its per-test setup and teardown, while they are inactive in suite-wide setup and teardown. Test transactions roll back by default; @Commit or @Rollback(false) changes that behavior. section 20.5

Table 11.4 @Transactional annotation properties (view table figure)

Property

Type

Behavior

isolation

Isolation enum

Declares the isolation levels, following the ANSI standard.

propagation

Propagation enum

Propagation settings following the values from table 11.2.

timeout

int (seconds)

Timeout after which the transaction will automatically roll back.

readOnly

boolean

Declares if the transaction is read-only or read-write. Read-only transactions allow optimizations that can make them faster.

Browse the annotation property table for isolation, propagation, timeout, and readOnly semantics. section 11.4.3

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains placement, inheritance, rollback rules, transaction properties, configuration, and test lifecycle without descending into transaction-manager internals.
Examples
Uses ItemDaoImpl and BidDaoImpl classes, DatabaseService initialization, and Spring transactional tests with UserService.
What you can do after
Operate/configure Spring persistence components and tests with explicit rollback, propagation, isolation, timeout, and read-only policies.
How it connects
Links rollback rules, isolation, propagation, timeout, read-only transactions, JPA, EntityManager, DAO pattern, and transaction managers.

Also explained in

Compare how 2 books explain Transactional annotation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.