public class JpaTransactionManager
extends org.springframework.transaction.support.AbstractPlatformTransactionManager
implements org.springframework.transaction.support.ResourceTransactionManager, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.InitializingBean
PlatformTransactionManager
implementation
for a single JPA 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
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
DataSourceTransactionManager
(i.e. DataSourceUtils.getConnection(javax.sql.DataSource)
or going through a
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(javax.sql.DataSource)
).
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 3.0 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(javax.persistence.EntityManagerFactory)
,
setDataSource(javax.sql.DataSource)
,
LocalEntityManagerFactoryBean
,
SharedEntityManagerBean
,
DataSourceUtils.getConnection(javax.sql.DataSource)
,
DataSourceUtils.releaseConnection(java.sql.Connection, javax.sql.DataSource)
,
org.springframework.jdbc.core.JdbcTemplate
,
DataSourceTransactionManager
,
JtaTransactionManager
,
Serialized FormConstructor and Description |
---|
JpaTransactionManager()
Create a new JpaTransactionManager instance.
|
JpaTransactionManager(EntityManagerFactory emf)
Create a new JpaTransactionManager instance.
|
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Eagerly initialize the JPA dialect, creating a default one
for the specified EntityManagerFactory if none set.
|
protected void |
closeEntityManagerAfterFailedBegin(org.springframework.orm.jpa.JpaTransactionManager.JpaTransactionObject txObject)
Close the current transaction's EntityManager.
|
protected EntityManager |
createEntityManagerForTransaction()
Create a JPA EntityManager to be used for a transaction.
|
protected void |
doBegin(Object transaction,
org.springframework.transaction.TransactionDefinition definition) |
protected void |
doCleanupAfterCompletion(Object transaction) |
protected void |
doCommit(org.springframework.transaction.support.DefaultTransactionStatus status) |
protected Object |
doGetTransaction() |
protected void |
doResume(Object transaction,
Object suspendedResources) |
protected void |
doRollback(org.springframework.transaction.support.DefaultTransactionStatus status) |
protected void |
doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus status) |
protected Object |
doSuspend(Object transaction) |
DataSource |
getDataSource()
Return the JDBC DataSource that this instance manages transactions for.
|
EntityManagerFactory |
getEntityManagerFactory()
Return the EntityManagerFactory that this instance should manage transactions for.
|
JpaDialect |
getJpaDialect()
Return the JPA dialect to use for this transaction manager.
|
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.
|
String |
getPersistenceUnitName()
Return the name of the persistence unit to manage transactions for, if any.
|
Object |
getResourceFactory() |
protected boolean |
isExistingTransaction(Object transaction) |
void |
setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
Retrieves an EntityManagerFactory by persistence unit name, if none set explicitly.
|
void |
setDataSource(DataSource dataSource)
Set the JDBC DataSource that this instance should manage transactions for.
|
void |
setEntityManagerFactory(EntityManagerFactory emf)
Set the EntityManagerFactory that this instance should manage transactions for.
|
void |
setJpaDialect(JpaDialect jpaDialect)
Set the JPA dialect to use for this transaction manager.
|
void |
setJpaProperties(Properties jpaProperties)
Specify JPA properties, to be passed into
EntityManagerFactory.createEntityManager(Map) (if any). |
void |
setJpaPropertyMap(Map<String,?> jpaProperties)
Specify JPA properties as a Map, to be passed into
EntityManagerFactory.createEntityManager(Map) (if any). |
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.
|
commit, determineTimeout, getDefaultTimeout, getTransaction, getTransactionSynchronization, invokeAfterCompletion, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, newTransactionStatus, prepareForCommit, prepareSynchronization, prepareTransactionStatus, registerAfterCompletionWithExistingTransaction, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionSynchronization, setTransactionSynchronizationName, setValidateExistingTransaction, suspend, triggerBeforeCommit, triggerBeforeCompletion, useSavepointForNestedTransaction
public JpaTransactionManager()
An EntityManagerFactory has to be set to be able to use it.
public JpaTransactionManager(EntityManagerFactory emf)
emf
- EntityManagerFactory to manage transactions forpublic void setEntityManagerFactory(EntityManagerFactory emf)
Alternatively, specify the persistence unit name of the target EntityManagerFactory. By default, a default EntityManagerFactory will be retrieved by finding a single unique bean of type EntityManagerFactory in the containing BeanFactory.
setPersistenceUnitName(java.lang.String)
public EntityManagerFactory getEntityManagerFactory()
public void setPersistenceUnitName(String persistenceUnitName)
This is an alternative to specifying the EntityManagerFactory by direct reference, resolving it by its persistence unit name instead. If no EntityManagerFactory and no persistence unit name have been specified, a default EntityManagerFactory will be retrieved by finding a single unique bean of type EntityManagerFactory.
public String getPersistenceUnitName()
public void setJpaProperties(Properties jpaProperties)
EntityManagerFactory.createEntityManager(Map)
(if any).
Can be populated with a String "value" (parsed via PropertiesEditor) or a "props" element in XML bean definitions.
public void setJpaPropertyMap(Map<String,?> jpaProperties)
EntityManagerFactory.createEntityManager(Map)
(if any).
Can be populated with a "map" or "props" element in XML bean definitions.
public Map<String,Object> getJpaPropertyMap()
Useful for specifying entries directly, for example via "jpaPropertyMap[myKey]".
public void setDataSource(DataSource dataSource)
If the EntityManagerFactory uses a known DataSource as its connection factory, the DataSource will be autodetected: You can still explicitly specify the DataSource, but you don't need to in this case.
A transactional JDBC Connection for this DataSource will be provided to application code accessing this DataSource directly via DataSourceUtils or JdbcTemplate. The Connection will be taken from the JPA EntityManager.
Note that you need to use a JPA dialect for a specific JPA implementation to allow for exposing JPA transactions as JDBC transactions.
The DataSource specified here should be the target DataSource to manage transactions for, not a TransactionAwareDataSourceProxy. Only data access code may work with TransactionAwareDataSourceProxy, while the transaction manager needs to work on the underlying target DataSource. If there's nevertheless a TransactionAwareDataSourceProxy passed in, it will be unwrapped to extract its target DataSource.
EntityManagerFactoryInfo.getDataSource()
,
setJpaDialect(org.springframework.orm.jpa.JpaDialect)
,
TransactionAwareDataSourceProxy
,
DataSourceUtils
,
org.springframework.jdbc.core.JdbcTemplate
public DataSource getDataSource()
public void setJpaDialect(JpaDialect jpaDialect)
If the EntityManagerFactory uses a known JpaDialect, it will be autodetected: You can still explicitly specify the DataSource, but you don't need to in this case.
The dialect object can be used to retrieve the underlying JDBC connection and thus allows for exposing JPA transactions as JDBC transactions.
public JpaDialect getJpaDialect()
public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) throws org.springframework.beans.BeansException
setBeanFactory
in interface org.springframework.beans.factory.BeanFactoryAware
org.springframework.beans.BeansException
setPersistenceUnitName(java.lang.String)
public void afterPropertiesSet()
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
public Object getResourceFactory()
getResourceFactory
in interface org.springframework.transaction.support.ResourceTransactionManager
protected Object doGetTransaction()
doGetTransaction
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected boolean isExistingTransaction(Object transaction)
isExistingTransaction
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doBegin(Object transaction, org.springframework.transaction.TransactionDefinition definition)
doBegin
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected EntityManager createEntityManagerForTransaction()
The default implementation checks whether the EntityManagerFactory is a Spring proxy and unwraps it first.
protected void closeEntityManagerAfterFailedBegin(org.springframework.orm.jpa.JpaTransactionManager.JpaTransactionObject txObject)
txObject
- the current transactionprotected Object doSuspend(Object transaction)
doSuspend
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doResume(Object transaction, Object suspendedResources)
doResume
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected boolean shouldCommitOnGlobalRollbackOnly()
shouldCommitOnGlobalRollbackOnly
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doCommit(org.springframework.transaction.support.DefaultTransactionStatus status)
doCommit
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doRollback(org.springframework.transaction.support.DefaultTransactionStatus status)
doRollback
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus status)
doSetRollbackOnly
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doCleanupAfterCompletion(Object transaction)
doCleanupAfterCompletion
in class org.springframework.transaction.support.AbstractPlatformTransactionManager