This is an internal documentation. There is a good chance you’re looking for something else. See Disclaimer.
A session is associated with exactly one transaction at the same time. It is however possible to run several transactions consecutively in the same session. A transaction is only required for write operations.
There are several ways to start a transaction:
using the invoker obtained from
using the TransactionManager directly
using the PersistenceService of the new API
Independently of how the transaction was started, in the background an instance of TransactionControl will be created. No matter through which API the transaction is accessed, always the same transaction control instance is used per transaction.
The transaction control has the following responsibilities:
it encapsulates the actual Hibernate Transaction
commit or rollback of the transaction
firing of commit listeners
handling of created and deleted entities
mapping from JPA to our custom exception (for example
OptimisticLockExceptionis mapped to
OutdatedException. All exceptions that inherit from
BaseRuntimeExceptionwill be thrown again without wrapping them - this is used for example for
These listeners are meant to be used by the business code to run actions before or after a commit (or rollback).
The TransactionListener can be registered directly on the transaction control and is meant to be used by internal code and are used to clean up the transaction.
The EntityTransactionContext keeps track of all entities that are created or deleted during the transaction and executes these changes before the transaction is committed.
See Transaction context for more details.
Entities that have been created or modified during a transaction will be validated before the transaction is committed.
The validation for new entities is started in
EntityTransactionContext#executeEntityOperations() just before
Session#save() is called.
ValidationContext.Operation.INSERT is passed to the validation context for newly created entities.
All modified entities are validated by the
preFlush() event that is called for all entities which are in the Hibernate session
before the changes are flushed to the database. Only dirty entities will be validated.
ValidationContext.Operation.UPDATE is passed to the validation context for updated entities.
TransactionControl#lock() can be used to create an advisory lock (
pg_advisory_xact_lock()) which uses
the database to create an application-level lock (which is independent of any database tables) based on a
The lock is released automatically after the transaction is finished.
TransactionControl#getAttribute() can be used by the business
logic to store and retrieve transaction-scoped data.