spring-framework / org.springframework.orm.jpa / JpaDialect / beginTransaction

beginTransaction

@Nullable abstract fun beginTransaction(entityManager: EntityManager, definition: TransactionDefinition): Any

Begin the given JPA transaction, applying the semantics specified by the given Spring transaction definition (in particular, an isolation level and a timeout). Called by JpaTransactionManager on transaction begin.

An implementation can configure the JPA Transaction object and then invoke begin, or invoke a special begin method that takes, for example, an isolation level.

An implementation can apply the read-only flag as flush mode. In that case, a transaction data object can be returned that holds the previous flush mode (and possibly other data), to be reset in cleanupTransaction. It may also apply the read-only flag and isolation level to the underlying JDBC Connection before beginning the transaction.

Implementations can also use the Spring transaction name, as exposed by the passed-in TransactionDefinition, to optimize for specific data access use cases (effectively using the current transaction name as use case identifier).

This method also allows for exposing savepoint capabilities if supported by the persistence provider, through returning an Object that implements Spring's org.springframework.transaction.SavepointManager interface. JpaTransactionManager will use this capability if needed.

Parameters

entityManager - the EntityManager to begin a JPA transaction on

definition - the Spring transaction definition that defines semantics

Exceptions

javax.persistence.PersistenceException - if thrown by JPA methods

java.sql.SQLException - if thrown by JDBC methods

org.springframework.transaction.TransactionException - in case of invalid arguments

Return
an arbitrary object that holds transaction data, if any (to be passed into #cleanupTransaction). May implement the org.springframework.transaction.SavepointManager interface.

See Also
#cleanupTransactionjavax.persistence.EntityTransaction#beginorg.springframework.jdbc.datasource.DataSourceUtils#prepareConnectionForTransaction