In a Spring application that uses JPA, @Transactional defines the transaction boundary around the work done through an EntityManager. Spring opens a transaction and binds an EntityManager to it, JPA tracks every entity you load or change, and at commit it writes those changes to the database with one flush.
Without a transaction, JPA cannot persist changes made to managed entities, and write operations through EntityManager such as persist or remove fail with a TransactionRequiredException.
JPA works with a persistence context, a first-level cache of managed entities. Within one transaction, loading the same row twice returns the same Java object, and any change to its fields is detected automatically. This is called dirty checking.
When the transaction commits, Hibernate (the JPA provider in Spring Boot) flushes the pending changes as INSERT, UPDATE and DELETE statements. If the method fails, Spring rolls back and the changes are discarded. @Transactional ties the persistence context to this unit of work. In Spring Boot, JpaTransactionManager is configured automatically when spring-boot-starter-data-jpa is on the classpath.
The rename method never calls save. Because product is managed inside the transaction, Hibernate detects the change and issues an UPDATE when the method returns. Calling save on an already managed entity is harmless but unnecessary.
Spring Data JPA's SimpleJpaRepository is annotated so that read methods run with readOnly = true and write methods such as save and delete run in a normal transaction. A single repository call is therefore already transactional.
You need your own @Transactional on a service method when several calls must succeed or fail together:
If stock runs out for the second product, the order row and the first stock decrease are both rolled back.
Lazy associations such as @OneToMany are loaded only when you access them, and this needs an open persistence context. Outside a transaction you can get a LazyInitializationException.
readOnly = true tells Spring and Hibernate that no writes are expected. Hibernate can skip dirty checking for the entities, which saves time on large result sets. For responses, prefer returning DTOs or using a fetch join instead of leaking entities to the web layer. Spring Boot enables spring.jpa.open-in-view by default, which keeps the EntityManager open during the web request and hides lazy loading problems, but it can hold database connections longer than needed. Many teams set it to false.
@Transactional method from another method of the same class skips the proxy.RuntimeException and Error cause rollback. Use rollbackFor for checked exceptions.JOIN FETCH or an entity graph.@Version field for optimistic locking so two transactions cannot silently overwrite each other.No. Single repository calls are already transactional. Add it when a method does more than one write or relies on dirty checking and lazy loading.
Not necessarily. Hibernate delays SQL until commit, a query that needs the data, or an explicit flush(). This also means constraint violations may only appear at commit.
The Jakarta Transactions annotation jakarta.transaction.Transactional works in Jakarta EE containers. In Spring applications, use the Spring annotation.
It marks the transaction as read-only, so Hibernate skips dirty checking and may use a read-only database connection hint. It does not stop all writes at the database level.
In JPA, @Transactional scopes the persistence context and the commit or rollback of every change made through it. Apply it on service methods that group several operations, use readOnly = true for queries, keep transactions short, and remember the default rollback and self-invocation rules.