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

public abstract Class SharedEntityManagerCreator

extends Object
Class Inheritance
Imports
java.io.IOException, .ObjectInputStream, .Serializable, java.lang.reflect.InvocationHandler, .InvocationTargetException, .Method, .Proxy, java.util.LinkedHashMap, .Map, .Set, jakarta.persistence.EntityManager, .EntityManagerFactory, .ParameterMode, .Query, .StoredProcedureQuery, .TransactionRequiredException, org.apache.commons.logging.Log, .LogFactory, org.springframework.lang.Nullable, org.springframework.transaction.support.TransactionSynchronizationManager, org.springframework.util.ClassUtils, .CollectionUtils, .ConcurrentReferenceHashMap

Delegate for creating a shareable JPA jakarta.persistence.EntityManager reference for a given jakarta.persistence.EntityManagerFactory.

A shared EntityManager will behave just like an EntityManager fetched from an application server's JNDI environment, as defined by the JPA specification. It will delegate all calls to the current transactional EntityManager, if any; otherwise it will fall back to a newly created EntityManager per operation.

For a behavioral definition of such a shared transactional EntityManager, see jakarta.persistence.PersistenceContextType#TRANSACTION and its discussion in the JPA spec document. This is also the default being used for the annotation-based jakarta.persistence.PersistenceContext#type().

Authors
Juergen Hoeller, Rod Johnson, Oliver Gierke, Mark Paluch, Sam Brannen
Since
2.0
See Also
jakarta.persistence.PersistenceContext, jakarta.persistence.PersistenceContextType#TRANSACTION, org.springframework.orm.jpa.JpaTransactionManager, ExtendedEntityManagerCreator

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
SharedEntityManagerCreator.DeferredQueryInvocationHandler

Invocation handler that handles deferred Query objects created by non-transactional createQuery invocations on a shared EntityManager.

private static class
SharedEntityManagerCreator.SharedEntityManagerInvocationHandler

Invocation handler that delegates all calls to the current transactional EntityManager, if any; else, it will fall back to a newly created EntityManager per operation.

Field Summary

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

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static EntityManager

Returns:

a shareable transaction EntityManager proxy
createSharedEntityManager
(EntityManagerFactory
the EntityManagerFactory to delegate to.
emf
)

Create a transactional EntityManager proxy for the given EntityManagerFactory.

public static EntityManager

Returns:

a shareable transaction EntityManager proxy
createSharedEntityManager
(EntityManagerFactory
the EntityManagerFactory to delegate to.
emf
,
Map<?, ?>
the properties to be passed into the createEntityManager call (may be null)
properties
)

Create a transactional EntityManager proxy for the given EntityManagerFactory.

public static EntityManager

Returns:

a shareable transaction EntityManager proxy
createSharedEntityManager
(EntityManagerFactory
the EntityManagerFactory to delegate to.
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 transactional EntityManager proxy for the given EntityManagerFactory.

public static EntityManager

Returns:

a shareable transactional EntityManager proxy
createSharedEntityManager
(EntityManagerFactory
the EntityManagerFactory to obtain EntityManagers from as needed
emf
,
Map<?, ?>
the properties to be passed into the createEntityManager call (may be null)
properties
,
Class<?>...
the interfaces to be implemented by the EntityManager. Allows the addition or specification of proprietary interfaces.
entityManagerInterfaces
)

Create a transactional EntityManager proxy for the given EntityManagerFactory.

public static EntityManager

Returns:

a shareable transactional EntityManager proxy
createSharedEntityManager
(EntityManagerFactory
the EntityManagerFactory to obtain EntityManagers from as needed
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
,
Class<?>...
the interfaces to be implemented by the EntityManager. Allows the addition or specification of proprietary interfaces.
entityManagerInterfaces
)

Create a transactional EntityManager proxy for the given EntityManagerFactory.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cachedQueryInterfacesback to summary
private static final Map<Class<?>, Class<?>[]> cachedQueryInterfaces
NO_ENTITY_MANAGER_INTERFACESback to summary
private static final Class<?>[] NO_ENTITY_MANAGER_INTERFACES
queryTerminatingMethodsback to summary
private static final Set<String> queryTerminatingMethods
transactionRequiringMethodsback to summary
private static final Set<String> transactionRequiringMethods

Constructor Detail

SharedEntityManagerCreatorback to summary
public SharedEntityManagerCreator()

Method Detail

createSharedEntityManagerback to summary
public static EntityManager createSharedEntityManager(EntityManagerFactory emf)

Create a transactional EntityManager proxy for the given EntityManagerFactory.

Parameters
emf:EntityManagerFactory

the EntityManagerFactory to delegate to.

Returns:EntityManager

a shareable transaction EntityManager proxy

createSharedEntityManagerback to summary
public static EntityManager createSharedEntityManager(EntityManagerFactory emf, Map<?, ?> properties)

Create a transactional EntityManager proxy for the given EntityManagerFactory.

Parameters
emf:EntityManagerFactory

the EntityManagerFactory to delegate to.

properties:Map<?, ?>

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

Returns:EntityManager

a shareable transaction EntityManager proxy

createSharedEntityManagerback to summary
public static EntityManager createSharedEntityManager(EntityManagerFactory emf, Map<?, ?> properties, boolean synchronizedWithTransaction)

Create a transactional EntityManager proxy for the given EntityManagerFactory.

Parameters
emf:EntityManagerFactory

the EntityManagerFactory to delegate to.

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 shareable transaction EntityManager proxy

Since
4.0
createSharedEntityManagerback to summary
public static EntityManager createSharedEntityManager(EntityManagerFactory emf, Map<?, ?> properties, Class<?>... entityManagerInterfaces)

Create a transactional EntityManager proxy for the given EntityManagerFactory.

Parameters
emf:EntityManagerFactory

the EntityManagerFactory to obtain EntityManagers from as needed

properties:Map<?, ?>

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

entityManagerInterfaces:Class<?>[]

the interfaces to be implemented by the EntityManager. Allows the addition or specification of proprietary interfaces.

Returns:EntityManager

a shareable transactional EntityManager proxy

createSharedEntityManagerback to summary
public static EntityManager createSharedEntityManager(EntityManagerFactory emf, Map<?, ?> properties, boolean synchronizedWithTransaction, Class<?>... entityManagerInterfaces)

Create a transactional EntityManager proxy for the given EntityManagerFactory.

Parameters
emf:EntityManagerFactory

the EntityManagerFactory to obtain EntityManagers from as needed

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)

entityManagerInterfaces:Class<?>[]

the interfaces to be implemented by the EntityManager. Allows the addition or specification of proprietary interfaces.

Returns:EntityManager

a shareable transactional EntityManager proxy

Since
4.0
org.springframework.orm.jpa back to summary

private Class SharedEntityManagerCreator.DeferredQueryInvocationHandler

extends Object
implements InvocationHandler
Class Inheritance
  • java.lang.Object
  • org.springframework.orm.jpa.SharedEntityManagerCreator.DeferredQueryInvocationHandler
All Implemented Interfaces
java.lang.reflect.InvocationHandler

Invocation handler that handles deferred Query objects created by non-transactional createQuery invocations on a shared EntityManager.

Includes deferred output parameter access for JPA 2.1 StoredProcedureQuery, retrieving the corresponding values for all registered parameters on query termination and returning the locally cached values for subsequent access.

Field Summary

Modifier and TypeField and Description
private EntityManager
private Map<Object, Object>
private final Query

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

entityManagerback to summary
private EntityManager entityManager
Annotations
@Nullable
outputParametersback to summary
private Map<Object, Object> outputParameters
Annotations
@Nullable
targetback to summary
private final Query target

Constructor Detail

DeferredQueryInvocationHandlerback to summary
public DeferredQueryInvocationHandler(Query target, EntityManager entityManager)

Method Detail

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
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.
org.springframework.orm.jpa back to summary

private Class SharedEntityManagerCreator.SharedEntityManagerInvocationHandler

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

Invocation handler that delegates all calls to the current transactional EntityManager, if any; else, it will fall back to a newly created EntityManager per operation.

Field Summary

Modifier and TypeField and Description
private final Log
private final Map<?, ?>
private transient volatile ClassLoader
private final boolean
private final EntityManagerFactory

Constructor Summary

AccessConstructor and Description
public
SharedEntityManagerInvocationHandler(EntityManagerFactory target, Map<?, ?> properties, boolean synchronizedWithTransaction)

Method Summary

Modifier and TypeMethod and Description
private void
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 void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

loggerback to summary
private final Log logger
propertiesback to summary
private final Map<?, ?> properties
Annotations
@Nullable
proxyClassLoaderback to summary
private transient volatile ClassLoader proxyClassLoader
Annotations
@Nullable
synchronizedWithTransactionback to summary
private final boolean synchronizedWithTransaction
targetFactoryback to summary
private final EntityManagerFactory targetFactory

Constructor Detail

SharedEntityManagerInvocationHandlerback to summary
public SharedEntityManagerInvocationHandler(EntityManagerFactory target, Map<?, ?> properties, boolean synchronizedWithTransaction)

Method Detail

initProxyClassLoaderback to summary
private void initProxyClassLoader()
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.
readObjectback to summary
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException