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.) 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.
(i.e. org.
or going through a
org.).
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.
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
| Modifier and Type | Class and Description |
|---|---|
| private static class | JpaTransactionManager.
JPA-specific transaction definition to be passed to |
| private class | JpaTransactionManager.
JPA transaction object, representing a EntityManagerHolder. |
| private static class | JpaTransactionManager.
Holder for suspended resources. |
| Modifier and Type | Field and Description |
|---|---|
| private DataSource | |
| private EntityManagerFactory | |
| private Consumer | |
| private JpaDialect | |
| private final Map | |
| private String |
| Access | Constructor and Description |
|---|---|
| public | |
| public | JpaTransactionManager(EntityManagerFactory
the EntityManagerFactory to manage transactions for emf)Create a new JpaTransactionManager instance. |
| Modifier and Type | Method and Description |
|---|---|
| public void | afterPropertiesSet()
Implements org. Eagerly initialize the JPA dialect, creating a default one for the specified EntityManagerFactory if none set. |
| protected void | closeEntityManagerAfterFailedBegin(JpaTransactionManager.
the current transaction txObjectClose the current transaction's EntityManager. |
| protected EntityManager | |
| protected void | doBegin(Object
the transaction object returned by transaction, TransactionDefinition doGetTransactiona TransactionDefinition instance, describing propagation
behavior, isolation level, read-only flag, timeout, and transaction name definition)Implements abstract org. Begin a new transaction with semantics according to the given transaction definition. |
| protected void | doCleanupAfterCompletion(Object
the transaction object returned by transaction)doGetTransactionOverrides org. Cleanup resources after transaction completion. |
| protected void | doCommit(DefaultTransactionStatus
the status representation of the transaction status)Implements abstract org. Perform an actual commit of the given transaction. |
| protected Object | doGetTransaction()
Implements abstract org. Return a transaction object for the current transaction state. |
| protected void | doResume(Object
the transaction object returned by transaction, Object doGetTransactionthe object that holds suspended resources,
as returned by doSuspend suspendedResources)Overrides org. Resume the resources of the current transaction. |
| protected void | doRollback(DefaultTransactionStatus
the status representation of the transaction status)Implements abstract org. Perform an actual rollback of the given transaction. |
| protected void | doSetRollbackOnly(DefaultTransactionStatus
the status representation of the transaction status)Overrides org. Set the given transaction rollback-only. |
| protected Object | doSuspend(Object
the transaction object returned by transaction)doGetTransactionOverrides org. Suspend the resources of the current transaction. |
| public DataSource | |
| public EntityManagerFactory | getEntityManagerFactory()
Return the EntityManagerFactory that this instance should manage transactions for. |
| public JpaDialect | |
| public Map | getJpaPropertyMap()
Allow |
| public String | |
| public Object | getResourceFactory()
Implements org. 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. Check if the given transaction object indicates an existing transaction (that is, a transaction which has already started). |
| protected final EntityManagerFactory | Returns: the EntityManagerFactory (nevernull)Obtain the EntityManagerFactory for actual use. |
| public void | setBeanFactory(BeanFactory
owning BeanFactory (never beanFactory)null).
The bean can immediately call methods on the factory.Implements org. 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 |
| public void | |
| public void | setJpaProperties(Properties jpaProperties)
Specify JPA properties, to be passed into
|
| public void | setJpaPropertyMap(Map<String, ?> jpaProperties)
Specify JPA properties as a Map, to be passed into
|
| public void | setPersistenceUnitName(String persistenceUnitName)
Set the name of the persistence unit to manage transactions for. |
| protected boolean | shouldCommitOnGlobalRollbackOnly()
This implementation returns "true": a JPA commit will properly handle transactions that have been marked rollback-only at a global level. |