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

public abstract Class ExtendedEntityManagerCreator

extends Object
Class Inheritance
Imports
java.io.Serializable, java.lang.reflect.InvocationHandler, .InvocationTargetException, .Method, .Proxy, java.util.LinkedHashSet, .Map, .Set, jakarta.persistence.EntityManager, .EntityManagerFactory, .EntityTransaction, .TransactionRequiredException, jakarta.persistence.spi.PersistenceUnitInfo, .PersistenceUnitTransactionType, org.apache.commons.logging.Log, .LogFactory, org.springframework.core.Ordered, org.springframework.dao.DataAccessException, org.springframework.dao.support.PersistenceExceptionTranslator, org.springframework.lang.Nullable, org.springframework.transaction.support.ResourceHolderSynchronization, .TransactionSynchronizationManager, org.springframework.util.Assert, .ClassUtils, .CollectionUtils, .ConcurrentReferenceHashMap

Delegate for creating a variety of jakarta.persistence.EntityManager proxies that follow the JPA spec's semantics for "extended" EntityManagers.

Supports several variants of "extended" EntityManagers: in particular, an "application-managed extended EntityManager", as defined by jakarta.persistence.EntityManagerFactory#createEntityManager(), as well as a "container-managed extended EntityManager", as defined by jakarta.persistence.PersistenceContextType#EXTENDED.

The original difference between "application-managed" and "container-managed" was the need for explicit joining of an externally managed transaction through the EntityManager#joinTransaction() method in the "application" case versus the automatic joining on each user-level EntityManager operation in the "container" case. As of JPA 2.1, both join modes are available with both kinds of EntityManagers, so the difference between "application-" and "container-managed" is now primarily in the join mode default and in the restricted lifecycle of a container-managed EntityManager (i.e. tied to the object that it is injected into).

Authors
Juergen Hoeller, Rod Johnson, Mark Paluch
Since
2.0
See Also
jakarta.persistence.EntityManagerFactory#createEntityManager(), jakarta.persistence.PersistenceContextType#EXTENDED, jakarta.persistence.EntityManager#joinTransaction(), SharedEntityManagerCreator

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ExtendedEntityManagerCreator.ExtendedEntityManagerInvocationHandler

InvocationHandler for extended EntityManagers as defined in the JPA spec.

private static class
ExtendedEntityManagerCreator.ExtendedEntityManagerSynchronization

TransactionSynchronization enlisting an extended EntityManager with a current Spring transaction.

Field Summary

Modifier and TypeField and Description
private static final Map<Class<?>, Class<?>[]>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static EntityManager

Returns:

an application-managed EntityManager that can join transactions but does not participate in them automatically
createApplicationManagedEntityManager
(EntityManager
the raw EntityManager to decorate
rawEntityManager
,
EntityManagerFactoryInfo
the EntityManagerFactoryInfo to obtain the JpaDialect and PersistenceUnitInfo from
emfInfo
)

Create an application-managed extended EntityManager proxy.

public static EntityManager

Returns:

an application-managed EntityManager that can join transactions but does not participate in them automatically
createApplicationManagedEntityManager
(EntityManager
the raw EntityManager to decorate
rawEntityManager
,
EntityManagerFactoryInfo
the EntityManagerFactoryInfo to obtain the JpaDialect and PersistenceUnitInfo from
emfInfo
,
boolean
whether to automatically join ongoing transactions (according to the JPA 2.1 SynchronizationType rules)
synchronizedWithTransaction
)

Create an application-managed extended EntityManager proxy.

public static EntityManager

Returns:

a container-managed EntityManager that will automatically participate in any managed transaction
createContainerManagedEntityManager
(EntityManager
the raw EntityManager to decorate
rawEntityManager
,
EntityManagerFactoryInfo
the EntityManagerFactoryInfo to obtain the JpaDialect and PersistenceUnitInfo from
emfInfo
)

Create a container-managed extended EntityManager proxy.

public static EntityManager

Returns:

a container-managed EntityManager that will automatically participate in any managed transaction
createContainerManagedEntityManager
(EntityManagerFactory
the EntityManagerFactory to create the EntityManager with. If this implements the EntityManagerFactoryInfo interface, the corresponding JpaDialect and PersistenceUnitInfo will be detected accordingly.
emf
)

Create a container-managed extended EntityManager proxy.

public static EntityManager

Returns:

a container-managed EntityManager that will automatically participate in any managed transaction
createContainerManagedEntityManager
(EntityManagerFactory
the EntityManagerFactory to create the EntityManager with. If this implements the EntityManagerFactoryInfo interface, the corresponding JpaDialect and PersistenceUnitInfo will be detected accordingly.
emf
,
Map<?, ?>
the properties to be passed into the createEntityManager call (may be null)
properties
)

Create a container-managed extended EntityManager proxy.

public static EntityManager

Returns:

a container-managed EntityManager that expects container-driven lifecycle management but may opt out of automatic transaction synchronization
createContainerManagedEntityManager
(EntityManagerFactory
the EntityManagerFactory to create the EntityManager with. If this implements the EntityManagerFactoryInfo interface, the corresponding JpaDialect and PersistenceUnitInfo will be detected accordingly.
emf
,
Map<?, ?>
the properties to be passed into the createEntityManager call (may be null)
properties
,
boolean
whether to automatically join ongoing transactions (according to the JPA 2.1 SynchronizationType rules)
synchronizedWithTransaction
)

Create a container-managed extended EntityManager proxy.

private static EntityManager

Returns:

the EntityManager proxy
createProxy
(EntityManager
raw EntityManager
rawEntityManager
,
EntityManagerFactoryInfo
the EntityManagerFactoryInfo to obtain the JpaDialect and PersistenceUnitInfo from
emfInfo
,
boolean
whether to follow container-managed EntityManager or application-managed EntityManager semantics
containerManaged
,
boolean
whether to automatically join ongoing transactions (according to the JPA 2.1 SynchronizationType rules)
synchronizedWithTransaction
)

Actually create the EntityManager proxy.

private static EntityManager

Returns:

the EntityManager proxy
createProxy
(EntityManager
raw EntityManager
rawEm
,
Class<? extends EntityManager>
the (potentially vendor-specific) EntityManager interface to proxy, or null for default detection of all interfaces
emIfc
,
ClassLoader
the ClassLoader to use for proxy creation (maybe null)
cl
,
PersistenceExceptionTranslator
the PersistenceException translator to use
exceptionTranslator
,
Boolean
whether to create a JTA-aware EntityManager (or null if not known in advance)
jta
,
boolean
whether to follow container-managed EntityManager or application-managed EntityManager semantics
containerManaged
,
boolean
whether to automatically join ongoing transactions (according to the JPA 2.1 SynchronizationType rules)
synchronizedWithTransaction
)

Actually create the EntityManager proxy.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cachedEntityManagerInterfacesback to summary
private static final Map<Class<?>, Class<?>[]> cachedEntityManagerInterfaces

Constructor Detail

ExtendedEntityManagerCreatorback to summary
public ExtendedEntityManagerCreator()

Method Detail

createApplicationManagedEntityManagerback to summary
public static EntityManager createApplicationManagedEntityManager(EntityManager rawEntityManager, EntityManagerFactoryInfo emfInfo)

Create an application-managed extended EntityManager proxy.

Parameters
rawEntityManager:EntityManager

the raw EntityManager to decorate

emfInfo:EntityManagerFactoryInfo

the EntityManagerFactoryInfo to obtain the JpaDialect and PersistenceUnitInfo from

Returns:EntityManager

an application-managed EntityManager that can join transactions but does not participate in them automatically

createApplicationManagedEntityManagerback to summary
public static EntityManager createApplicationManagedEntityManager(EntityManager rawEntityManager, EntityManagerFactoryInfo emfInfo, boolean synchronizedWithTransaction)

Create an application-managed extended EntityManager proxy.

Parameters
rawEntityManager:EntityManager

the raw EntityManager to decorate

emfInfo:EntityManagerFactoryInfo

the EntityManagerFactoryInfo to obtain the JpaDialect and PersistenceUnitInfo from

synchronizedWithTransaction:boolean

whether to automatically join ongoing transactions (according to the JPA 2.1 SynchronizationType rules)

Returns:EntityManager

an application-managed EntityManager that can join transactions but does not participate in them automatically

Since
4.0
createContainerManagedEntityManagerback to summary
public static EntityManager createContainerManagedEntityManager(EntityManager rawEntityManager, EntityManagerFactoryInfo emfInfo)

Create a container-managed extended EntityManager proxy.

Parameters
rawEntityManager:EntityManager

the raw EntityManager to decorate

emfInfo:EntityManagerFactoryInfo

the EntityManagerFactoryInfo to obtain the JpaDialect and PersistenceUnitInfo from

Returns:EntityManager

a container-managed EntityManager that will automatically participate in any managed transaction

createContainerManagedEntityManagerback to summary
public static EntityManager createContainerManagedEntityManager(EntityManagerFactory emf)

Create a container-managed extended EntityManager proxy.

Parameters
emf:EntityManagerFactory

the EntityManagerFactory to create the EntityManager with. If this implements the EntityManagerFactoryInfo interface, the corresponding JpaDialect and PersistenceUnitInfo will be detected accordingly.

Returns:EntityManager

a container-managed EntityManager that will automatically participate in any managed transaction

See Also
jakarta.persistence.EntityManagerFactory#createEntityManager()
createContainerManagedEntityManagerback to summary
public static EntityManager createContainerManagedEntityManager(EntityManagerFactory emf, Map<?, ?> properties)

Create a container-managed extended EntityManager proxy.

Parameters
emf:EntityManagerFactory

the EntityManagerFactory to create the EntityManager with. If this implements the EntityManagerFactoryInfo interface, the corresponding JpaDialect and PersistenceUnitInfo will be detected accordingly.

properties:Map<?, ?>

the properties to be passed into the createEntityManager call (may be null)

Returns:EntityManager

a container-managed EntityManager that will automatically participate in any managed transaction

See Also
jakarta.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
createContainerManagedEntityManagerback to summary
public static EntityManager createContainerManagedEntityManager(EntityManagerFactory emf, Map<?, ?> properties, boolean synchronizedWithTransaction)

Create a container-managed extended EntityManager proxy.

Parameters
emf:EntityManagerFactory

the EntityManagerFactory to create the EntityManager with. If this implements the EntityManagerFactoryInfo interface, the corresponding JpaDialect and PersistenceUnitInfo will be detected accordingly.

properties:Map<?, ?>

the properties to be passed into the createEntityManager call (may be null)

synchronizedWithTransaction:boolean

whether to automatically join ongoing transactions (according to the JPA 2.1 SynchronizationType rules)

Returns:EntityManager

a container-managed EntityManager that expects container-driven lifecycle management but may opt out of automatic transaction synchronization

Since
4.0
See Also
jakarta.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
createProxyback to summary
private static EntityManager createProxy(EntityManager rawEntityManager, EntityManagerFactoryInfo emfInfo, boolean containerManaged, boolean synchronizedWithTransaction)

Actually create the EntityManager proxy.

Parameters
rawEntityManager:EntityManager

raw EntityManager

emfInfo:EntityManagerFactoryInfo

the EntityManagerFactoryInfo to obtain the JpaDialect and PersistenceUnitInfo from

containerManaged:boolean

whether to follow container-managed EntityManager or application-managed EntityManager semantics

synchronizedWithTransaction:boolean

whether to automatically join ongoing transactions (according to the JPA 2.1 SynchronizationType rules)

Returns:EntityManager

the EntityManager proxy

createProxyback to summary
private static EntityManager createProxy(EntityManager rawEm, Class<? extends EntityManager> emIfc, ClassLoader cl, PersistenceExceptionTranslator exceptionTranslator, Boolean jta, boolean containerManaged, boolean synchronizedWithTransaction)

Actually create the EntityManager proxy.

Parameters
rawEm:EntityManager

raw EntityManager

emIfc:Class<? extends EntityManager>

the (potentially vendor-specific) EntityManager interface to proxy, or null for default detection of all interfaces

cl:ClassLoader

the ClassLoader to use for proxy creation (maybe null)

exceptionTranslator:PersistenceExceptionTranslator

the PersistenceException translator to use

jta:Boolean

whether to create a JTA-aware EntityManager (or null if not known in advance)

containerManaged:boolean

whether to follow container-managed EntityManager or application-managed EntityManager semantics

synchronizedWithTransaction:boolean

whether to automatically join ongoing transactions (according to the JPA 2.1 SynchronizationType rules)

Returns:EntityManager

the EntityManager proxy

org.springframework.orm.jpa back to summary

private final Class ExtendedEntityManagerCreator.ExtendedEntityManagerInvocationHandler

extends Object
implements InvocationHandler, Serializable
Class Inheritance
  • java.lang.Object
  • org.springframework.orm.jpa.ExtendedEntityManagerCreator.ExtendedEntityManagerInvocationHandler
All Implemented Interfaces
java.io.Serializable, java.lang.reflect.InvocationHandler
Annotations
@SuppressWarnings:serial

InvocationHandler for extended EntityManagers as defined in the JPA spec.

Field Summary

Modifier and TypeField and Description
private final boolean
private final PersistenceExceptionTranslator
private final boolean
private static final Log
private final boolean
private final EntityManager

Constructor Summary

AccessConstructor and Description
private
ExtendedEntityManagerInvocationHandler(EntityManager target, PersistenceExceptionTranslator exceptionTranslator, Boolean jta, boolean containerManaged, boolean synchronizedWithTransaction)

Method Summary

Modifier and TypeMethod and Description
private void
doJoinTransaction(boolean
whether to enforce the transaction (i.e. whether failure to join is considered fatal)
enforce
)

Join an existing transaction, if not already joined.

private void
enlistInCurrentTransaction()

Enlist this application-managed EntityManager in the current transaction.

public Object
invoke(Object
the proxy instance that the method was invoked on
proxy
,
Method
the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.
method
,
Object[]
an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.
args
)

Implements java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result.

private boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

containerManagedback to summary
private final boolean containerManaged
exceptionTranslatorback to summary
private final PersistenceExceptionTranslator exceptionTranslator
Annotations
@Nullable
jtaback to summary
private final boolean jta
loggerback to summary
private static final Log logger
synchronizedWithTransactionback to summary
private final boolean synchronizedWithTransaction
targetback to summary
private final EntityManager target

Constructor Detail

ExtendedEntityManagerInvocationHandlerback to summary
private ExtendedEntityManagerInvocationHandler(EntityManager target, PersistenceExceptionTranslator exceptionTranslator, Boolean jta, boolean containerManaged, boolean synchronizedWithTransaction)

Method Detail

doJoinTransactionback to summary
private void doJoinTransaction(boolean enforce)

Join an existing transaction, if not already joined.

Parameters
enforce:boolean

whether to enforce the transaction (i.e. whether failure to join is considered fatal)

enlistInCurrentTransactionback to summary
private void enlistInCurrentTransaction()

Enlist this application-managed EntityManager in the current transaction.

invokeback to summary
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable

Implements java.lang.reflect.InvocationHandler.invoke.

Doc from java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result. This method will be invoked on an invocation handler when a method is invoked on a proxy instance that it is associated with.

Parameters
proxy:Object

the proxy instance that the method was invoked on

method:Method

the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.

args:Object[]

an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.

Returns:Object

the value to return from the method invocation on the proxy instance. If the declared return type of the interface method is a primitive type, then the value returned by this method must be an instance of the corresponding primitive wrapper class; otherwise, it must be a type assignable to the declared return type. If the value returned by this method is null and the interface method's return type is primitive, then a NullPointerException will be thrown by the method invocation on the proxy instance. If the value returned by this method is otherwise not compatible with the interface method's declared return type as described above, a ClassCastException will be thrown by the method invocation on the proxy instance.

Annotations
@Override
@Nullable
Exceptions
Throwable:
the exception to throw from the method invocation on the proxy instance. The exception's type must be assignable either to any of the exception types declared in the throws clause of the interface method or to the unchecked exception types java.lang.RuntimeException or java.lang.Error. If a checked exception is thrown by this method that is not assignable to any of the exception types declared in the throws clause of the interface method, then an UndeclaredThrowableException containing the exception that was thrown by this method will be thrown by the method invocation on the proxy instance.
isJtaEntityManagerback to summary
private boolean isJtaEntityManager()
org.springframework.orm.jpa back to summary

private Class ExtendedEntityManagerCreator.ExtendedEntityManagerSynchronization

extends ResourceHolderSynchronization<EntityManagerHolder, EntityManager>
implements Ordered
Class Inheritance
All Implemented Interfaces
org.springframework.core.Ordered

TransactionSynchronization enlisting an extended EntityManager with a current Spring transaction.

Field Summary

Modifier and TypeField and Description
public volatile boolean
private final EntityManager
private final PersistenceExceptionTranslator

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
public void
afterCompletion(int
completion status according to the STATUS_* constants
status
)

Overrides org.springframework.transaction.support.ResourceHolderSynchronization.afterCompletion.

Overrides default org.springframework.transaction.support.TransactionSynchronization.afterCompletion.

Invoked after transaction commit/rollback.

private RuntimeException
protected void
flushResource(EntityManagerHolder
the resource holder to flush
resourceHolder
)

Overrides org.springframework.transaction.support.ResourceHolderSynchronization.flushResource.

Flush callback for the given resource holder.

public int
protected boolean
shouldReleaseBeforeCompletion()

Overrides org.springframework.transaction.support.ResourceHolderSynchronization.shouldReleaseBeforeCompletion.

Return whether this holder's resource should be released before transaction completion (true) or rather after transaction completion (false).

Inherited from org.springframework.transaction.support.ResourceHolderSynchronization:
beforeCommitbeforeCompletioncleanupResourceflushprocessResourceAfterCommitreleaseResourceresumeshouldReleaseAfterCompletionshouldUnbindAtCompletionsuspend

Field Detail

closeOnCompletionback to summary
public volatile boolean closeOnCompletion
entityManagerback to summary
private final EntityManager entityManager
exceptionTranslatorback to summary
private final PersistenceExceptionTranslator exceptionTranslator
Annotations
@Nullable

Constructor Detail

ExtendedEntityManagerSynchronizationback to summary
public ExtendedEntityManagerSynchronization(EntityManager em, PersistenceExceptionTranslator exceptionTranslator)

Method Detail

afterCommitback to summary
public void afterCommit()

Overrides org.springframework.transaction.support.ResourceHolderSynchronization.afterCommit.

Overrides default org.springframework.transaction.support.TransactionSynchronization.afterCommit.

Doc from org.springframework.transaction.support.TransactionSynchronization.afterCommit.

Invoked after transaction commit. Can perform further operations right after the main transaction has successfully committed.

Can e.g. commit further operations that are supposed to follow on a successful commit of the main transaction, like confirmation messages or emails.

Note

The transaction will have been committed already, but the transactional resources might still be active and accessible. As a consequence, any data access code triggered at this point will still "participate" in the original transaction, allowing to perform some cleanup (with no commit following anymore!), unless it explicitly declares that it needs to run in a separate transaction. Hence: Use PROPAGATION_REQUIRES_NEW for any transactional operation that is called from here.

Annotations
@Override
afterCompletionback to summary
public void afterCompletion(int status)

Overrides org.springframework.transaction.support.ResourceHolderSynchronization.afterCompletion.

Overrides default org.springframework.transaction.support.TransactionSynchronization.afterCompletion.

Doc from org.springframework.transaction.support.TransactionSynchronization.afterCompletion.

Invoked after transaction commit/rollback. Can perform resource cleanup after transaction completion.

Note

The transaction will have been committed or rolled back already, but the transactional resources might still be active and accessible. As a consequence, any data access code triggered at this point will still "participate" in the original transaction, allowing to perform some cleanup (with no commit following anymore!), unless it explicitly declares that it needs to run in a separate transaction. Hence: Use PROPAGATION_REQUIRES_NEW for any transactional operation that is called from here.

Parameters
status:int

completion status according to the STATUS_* constants

Annotations
@Override
convertExceptionback to summary
private RuntimeException convertException(RuntimeException ex)
flushResourceback to summary
protected void flushResource(EntityManagerHolder resourceHolder)

Overrides org.springframework.transaction.support.ResourceHolderSynchronization.flushResource.

Doc from org.springframework.transaction.support.ResourceHolderSynchronization.flushResource.

Flush callback for the given resource holder.

Parameters
resourceHolder:EntityManagerHolder

the resource holder to flush

Annotations
@Override
getOrderback to summary
public int getOrder()

Overrides default org.springframework.transaction.support.TransactionSynchronization.getOrder.

Implements org.springframework.core.Ordered.getOrder.

Doc from org.springframework.core.Ordered.getOrder.

Get the order value of this object.

Higher values are interpreted as lower priority. As a consequence, the object with the lowest value has the highest priority (somewhat analogous to Servlet load-on-startup values).

Same order values will result in arbitrary sort positions for the affected objects.

Returns:int

the order value

Annotations
@Override
shouldReleaseBeforeCompletionback to summary
protected boolean shouldReleaseBeforeCompletion()

Overrides org.springframework.transaction.support.ResourceHolderSynchronization.shouldReleaseBeforeCompletion.

Doc from org.springframework.transaction.support.ResourceHolderSynchronization.shouldReleaseBeforeCompletion.

Return whether this holder's resource should be released before transaction completion (true) or rather after transaction completion (false).

Note that resources will only be released when they are unbound from the thread (shouldUnbindAtCompletion()).

The default implementation returns true.

Annotations
@Override