What is the PlatformTransactionManager interface in Spring, and what three methods does it define?
answer
- Central SPI in spring-tx
- getTransaction / commit / rollback
- TransactionDefinition in, TransactionStatus out
- @Transactional and TransactionTemplate drive it
- Swap impl for JDBC / JPA / JTA
basics
~10 sIt is Spring's central interface for managing transactions. It defines three methods: getTransaction (start/join a transaction), commit (make changes permanent), and rollback (undo changes).
solid answer
~30 sPlatformTransactionManager is the central strategy interface (SPI) of Spring's transaction infrastructure, living in the spring-tx module. It has three methods: TransactionStatus getTransaction(TransactionDefinition) starts a new transaction or joins the current one based on the requested propagation; void commit(TransactionStatus) commits it; void rollback(TransactionStatus) undoes it. Everything else in Spring transactions — @Transactional, TransactionTemplate — ultimately drives this interface. Because it is an abstraction, the same code works whether the backend is plain JDBC (DataSourceTransactionManager), JPA (JpaTransactionManager), or JTA (JtaTransactionManager); you just swap the implementation bean.
code
java · 16 linespublic interface PlatformTransactionManager extends TransactionManager {
TransactionStatus getTransaction(TransactionDefinition definition)
throws TransactionException;
void commit(TransactionStatus status) throws TransactionException;
void rollback(TransactionStatus status) throws TransactionException;
}
// Programmatic use (what @Transactional does under the hood):
TransactionStatus status = txManager.getTransaction(new DefaultTransactionDefinition());
try {
// ... business work ...
txManager.commit(status);
} catch (RuntimeException ex) {
txManager.rollback(status);
throw ex;
}go deeper
Know the interface name, the three methods, and that it is Spring's core transaction abstraction.
Connect it to @Transactional and TransactionTemplate, and know the argument/return types.
Explain the commit-becomes-rollback-only edge case and how propagation is decided inside getTransaction.
Discuss it as a stable SPI boundary enabling backend swap and reactive counterpart (ReactiveTransactionManager).
## What it is `PlatformTransactionManager` is the **central interface** (a Service Provider Interface, SPI) of Spring's transaction support, defined in the `org.springframework.transaction` package of the `spring-tx` module. A *transaction* is a unit of work that either fully succeeds (**commit**) or is fully undone (**rollback**) — it never leaves the system half-changed. Spring does not implement transactions itself; it defines this interface as a **strategy** and delegates the real work to a backend (a database driver, an ORM, or an application-server transaction coordinator). Your business code stays identical regardless of backend. ## The three methods ```java public interface PlatformTransactionManager extends TransactionManager { TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException; void commit(TransactionStatus status) throws TransactionException; void rollback(TransactionStatus status) throws TransactionException; } ``` - **`getTransaction(TransactionDefinition)`** — asks for a transaction. The `TransactionDefinition` argument describes *how* it should behave: **propagation** (start a new one vs. join an existing one), **isolation** level, **timeout**, and **read-only** hint. The method returns a `TransactionStatus` handle representing the (possibly newly created, possibly already-active) transaction. If you pass `null`, defaults are used. - **`commit(TransactionStatus)`** — attempts to commit the transaction represented by that status. If the status was marked rollback-only, commit will instead roll back and throw `UnexpectedRollbackException`. - **`rollback(TransactionStatus)`** — rolls back, undoing all changes. ## How it fits together You rarely call these methods yourself. In practice: - **Declarative** transactions: the `@Transactional` annotation is handled by an AOP proxy whose `TransactionInterceptor` calls `getTransaction` before your method, then `commit` or `rollback` after. - **Programmatic** transactions: `TransactionTemplate` wraps the same three calls around a callback. ## Key terms - **SPI** — a contract Spring defines and different providers implement. You (or Spring Boot) pick the implementation. - **`TransactionStatus`** — a runtime handle: tells you whether the transaction is new, lets you mark it rollback-only, manages savepoints. - **`TransactionDefinition`** — the read-only *specification* of the desired transaction (propagation, isolation, timeout, read-only, name). ## Why it matters Because the abstraction is uniform, moving from a single JDBC DataSource to JPA, or to distributed JTA transactions across two resources, is mostly a matter of registering a different `PlatformTransactionManager` bean — your annotated service code does not change.
- Do you usually call getTransaction/commit/rollback yourself?No. Normally @Transactional (via TransactionInterceptor) or TransactionTemplate makes those calls for you. Direct calls are rare and only for fine-grained programmatic control.
- What does the TransactionDefinition argument carry?The desired propagation behavior, isolation level, timeout, read-only flag, and an optional transaction name — the specification of how the transaction should behave.
saying these in an interview costs you the question
- Claiming PlatformTransactionManager itself talks SQL or manages a connection pool (it delegates to a backend).
- Thinking commit always commits — it rolls back and throws UnexpectedRollbackException if the transaction was marked rollback-only.
- Saying it has methods like begin() — the method is getTransaction().