Top Description Inners Fields Constructors Methods
org.springframework.orm.jpa

public Class JpaTransactionManager

extends AbstractPlatformTransactionManager
implements ResourceTransactionManager, BeanFactoryAware, InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.Aware, org.springframework.transaction.support.ResourceTransactionManager, org.springframework.transaction.PlatformTransactionManager, org.springframework.transaction.TransactionManager
Annotations
@SuppressWarnings:serial
Imports
java.util.HashMap, .Map, .Properties, java.util.function.Consumer, javax.sql.DataSource, jakarta.persistence.EntityManager, .EntityManagerFactory, .EntityTransaction, .PersistenceException, .RollbackException, org.springframework.beans.BeansException, org.springframework.beans.factory.BeanFactory, .BeanFactoryAware, .InitializingBean, .ListableBeanFactory, org.springframework.dao.DataAccessException, org.springframework.dao.support.DataAccessUtils, org.springframework.jdbc.datasource.ConnectionHandle, .ConnectionHolder, .JdbcTransactionObjectSupport, .TransactionAwareDataSourceProxy, org.springframework.lang.Nullable, org.springframework.transaction.CannotCreateTransactionException, .IllegalTransactionStateException, .NestedTransactionNotSupportedException, .SavepointManager, .TransactionDefinition, .TransactionException, .TransactionSystemException, org.springframework.transaction.support.AbstractPlatformTransactionManager, .DefaultTransactionStatus, .DelegatingTransactionDefinition, .ResourceTransactionDefinition, .ResourceTransactionManager, .TransactionSynchronizationManager, org.springframework.util.Assert, .CollectionUtils

org.springframework.transaction.PlatformTransactionManager implementation for a single JPA jakarta.persistence.EntityManagerFactory. Binds a JPA EntityManager from the specified factory to the thread, potentially allowing for one thread-bound EntityManager per factory. SharedEntityManagerCreator and @PersistenceContext are aware of thread-bound entity managers and participate in such transactions automatically. Using either is required for JPA access code supporting this transaction management mechanism.

This transaction manager is appropriate for applications that use a single JPA EntityManagerFactory for transactional data access. JTA (usually through org.springframework.transaction.jta.JtaTransactionManager) is necessary for accessing multiple transactional resources within the same transaction. Note that you need to configure your JPA provider accordingly in order to make it participate in JTA transactions.

This transaction manager also supports direct DataSource access within a transaction (i.e. plain JDBC code working with the same DataSource). This allows for mixing services which access JPA and services which use plain JDBC (without being aware of JPA)! Application code needs to stick to the same simple Connection lookup pattern as with org.springframework.jdbc.datasource.DataSourceTransactionManager (i.e. org.springframework.jdbc.datasource.DataSourceUtils#getConnection or going through a org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy). Note that this requires a vendor-specific JpaDialect to be configured.

Note

To be able to register a DataSource's Connection for plain JDBC code, this instance needs to be aware of the DataSource (setDataSource). The given DataSource should obviously match the one used by the given EntityManagerFactory. This transaction manager will autodetect the DataSource used as the connection factory of the EntityManagerFactory, so you usually don't need to explicitly specify the "dataSource" property.

This transaction manager supports nested transactions via JDBC Savepoints. The "nestedTransactionAllowed" flag defaults to false though, since nested transactions will just apply to the JDBC Connection, not to the JPA EntityManager and its cached entity objects and related context. You can manually set the flag to true if you want to use nested transactions for JDBC access code which participates in JPA transactions (provided that your JDBC driver supports Savepoints). Note that JPA itself does not support nested transactions! Hence, do not expect JPA access code to semantically participate in a nested transaction.

Author
Juergen Hoeller
Since
2.0
See Also
setEntityManagerFactory, setDataSource, LocalEntityManagerFactoryBean, org.springframework.orm.jpa.support.SharedEntityManagerBean, org.springframework.jdbc.datasource.DataSourceUtils#getConnection, org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection, org.springframework.jdbc.core.JdbcTemplate, org.springframework.jdbc.support.JdbcTransactionManager, org.springframework.transaction.jta.JtaTransactionManager

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
JpaTransactionManager.JpaTransactionDefinition

JPA-specific transaction definition to be passed to JpaDialect#beginTransaction.

private class
JpaTransactionManager.JpaTransactionObject

JPA transaction object, representing a EntityManagerHolder.

private static class
JpaTransactionManager.SuspendedResourcesHolder

Holder for suspended resources.

Field Summary

Modifier and TypeField and Description
private DataSource
private EntityManagerFactory
private Consumer<EntityManager>
private JpaDialect
private final Map<String, Object>
private String
Inherited from org.springframework.transaction.support.AbstractPlatformTransactionManager:
loggerSYNCHRONIZATION_ALWAYSSYNCHRONIZATION_NEVERSYNCHRONIZATION_ON_ACTUAL_TRANSACTION

Constructor Summary

AccessConstructor and Description
public
JpaTransactionManager()

Create a new JpaTransactionManager instance.

public
JpaTransactionManager(EntityManagerFactory
the EntityManagerFactory to manage transactions for
emf
)

Create a new JpaTransactionManager instance.

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Eagerly initialize the JPA dialect, creating a default one for the specified EntityManagerFactory if none set.

protected void
closeEntityManagerAfterFailedBegin(JpaTransactionManager.JpaTransactionObject
the current transaction
txObject
)

Close the current transaction's EntityManager.

protected EntityManager
createEntityManagerForTransaction()

Create a JPA EntityManager to be used for a transaction.

protected void
doBegin(Object
the transaction object returned by doGetTransaction
transaction
,
TransactionDefinition
a TransactionDefinition instance, describing propagation behavior, isolation level, read-only flag, timeout, and transaction name
definition
)

Implements abstract org.springframework.transaction.support.AbstractPlatformTransactionManager.doBegin.

Begin a new transaction with semantics according to the given transaction definition.

protected void
doCleanupAfterCompletion(Object
the transaction object returned by doGetTransaction
transaction
)

Overrides org.springframework.transaction.support.AbstractPlatformTransactionManager.doCleanupAfterCompletion.

Cleanup resources after transaction completion.

protected void
doCommit(DefaultTransactionStatus
the status representation of the transaction
status
)

Implements abstract org.springframework.transaction.support.AbstractPlatformTransactionManager.doCommit.

Perform an actual commit of the given transaction.

protected Object
doGetTransaction()

Implements abstract org.springframework.transaction.support.AbstractPlatformTransactionManager.doGetTransaction.

Return a transaction object for the current transaction state.

protected void
doResume(Object
the transaction object returned by doGetTransaction
transaction
,
Object
the object that holds suspended resources, as returned by doSuspend
suspendedResources
)

Overrides org.springframework.transaction.support.AbstractPlatformTransactionManager.doResume.

Resume the resources of the current transaction.

protected void
doRollback(DefaultTransactionStatus
the status representation of the transaction
status
)

Implements abstract org.springframework.transaction.support.AbstractPlatformTransactionManager.doRollback.

Perform an actual rollback of the given transaction.

protected void
doSetRollbackOnly(DefaultTransactionStatus
the status representation of the transaction
status
)

Overrides org.springframework.transaction.support.AbstractPlatformTransactionManager.doSetRollbackOnly.

Set the given transaction rollback-only.

protected Object
doSuspend(Object
the transaction object returned by doGetTransaction
transaction
)

Overrides org.springframework.transaction.support.AbstractPlatformTransactionManager.doSuspend.

Suspend the resources of the current transaction.

public DataSource
getDataSource()

Return the JDBC DataSource that this instance manages transactions for.

public EntityManagerFactory
getEntityManagerFactory()

Return the EntityManagerFactory that this instance should manage transactions for.

public JpaDialect
getJpaDialect()

Return the JPA dialect to use for this transaction manager.

public Map<String, Object>
getJpaPropertyMap()

Allow Map access to the JPA properties to be passed to the persistence provider, with the option to add or override specific entries.

public String
getPersistenceUnitName()

Return the name of the persistence unit to manage transactions for, if any.

public Object
getResourceFactory()

Implements org.springframework.transaction.support.ResourceTransactionManager.getResourceFactory.

Return the resource factory that this transaction manager operates on, e.g. a JDBC DataSource or a JMS ConnectionFactory.

protected boolean
isExistingTransaction(Object
the transaction object returned by doGetTransaction
transaction
)

Overrides org.springframework.transaction.support.AbstractPlatformTransactionManager.isExistingTransaction.

Check if the given transaction object indicates an existing transaction (that is, a transaction which has already started).

protected final EntityManagerFactory

Returns:

the EntityManagerFactory (never null)
obtainEntityManagerFactory
()

Obtain the EntityManagerFactory for actual use.

public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Retrieves an EntityManagerFactory by persistence unit name, if none set explicitly.

public void
setDataSource(DataSource dataSource)

Set the JDBC DataSource that this instance should manage transactions for.

public void
setEntityManagerFactory(EntityManagerFactory emf)

Set the EntityManagerFactory that this instance should manage transactions for.

public void
setEntityManagerInitializer(Consumer<EntityManager> entityManagerInitializer)

Specify a callback for customizing every EntityManager resource created for a new transaction managed by this JpaTransactionManager.

public void
setJpaDialect(JpaDialect jpaDialect)

Set the JPA dialect to use for this transaction manager.

public void
setJpaProperties(Properties jpaProperties)

Specify JPA properties, to be passed into EntityManagerFactory.createEntityManager(Map) (if any).

public void
setJpaPropertyMap(Map<String, ?> jpaProperties)

Specify JPA properties as a Map, to be passed into EntityManagerFactory.createEntityManager(Map) (if any).

public void
setPersistenceUnitName(String persistenceUnitName)

Set the name of the persistence unit to manage transactions for.

protected boolean
shouldCommitOnGlobalRollbackOnly()

Overrides org.springframework.transaction.support.AbstractPlatformTransactionManager.shouldCommitOnGlobalRollbackOnly.

This implementation returns "true": a JPA commit will properly handle transactions that have been marked rollback-only at a global level.

Inherited from org.springframework.transaction.support.AbstractPlatformTransactionManager:
commitdetermineTimeoutgetDefaultTimeoutgetTransactiongetTransactionExecutionListenersgetTransactionSynchronizationinvokeAfterCompletionisFailEarlyOnGlobalRollbackOnlyisGlobalRollbackOnParticipationFailureisNestedTransactionAllowedisRollbackOnCommitFailureisValidateExistingTransactionprepareForCommitprepareSynchronizationregisterAfterCompletionWithExistingTransactionresumerollbacksetDefaultTimeoutsetFailEarlyOnGlobalRollbackOnlysetGlobalRollbackOnParticipationFailuresetNestedTransactionAllowedsetRollbackOnCommitFailuresetTransactionExecutionListenerssetTransactionSynchronizationsetTransactionSynchronizationNamesetValidateExistingTransactionsuspendtriggerBeforeCommittriggerBeforeCompletionuseSavepointForNestedTransaction