Example usage for javax.persistence SynchronizationType SYNCHRONIZED

List of usage examples for javax.persistence SynchronizationType SYNCHRONIZED

Introduction

In this page you can find the example usage for javax.persistence SynchronizationType SYNCHRONIZED.

Prototype

SynchronizationType SYNCHRONIZED

To view the source code for javax.persistence SynchronizationType SYNCHRONIZED.

Click Source Link

Document

Persistence context is automatically synchronized with the current transaction

Usage

From source file:org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.java

/**
 * Delegate an incoming invocation from the proxy, dispatching to EntityManagerFactoryInfo
 * or the native EntityManagerFactory accordingly.
 *//*from  ww w  . j  av  a 2  s .  c o m*/
Object invokeProxyMethod(Method method, @Nullable Object[] args) throws Throwable {
    if (method.getDeclaringClass().isAssignableFrom(EntityManagerFactoryInfo.class)) {
        return method.invoke(this, args);
    } else if (method.getName().equals("createEntityManager") && args != null && args.length > 0
            && args[0] == SynchronizationType.SYNCHRONIZED) {
        // JPA 2.1's createEntityManager(SynchronizationType, Map)
        // Redirect to plain createEntityManager and add synchronization semantics through Spring proxy
        EntityManager rawEntityManager = (args.length > 1
                ? getNativeEntityManagerFactory().createEntityManager((Map<?, ?>) args[1])
                : getNativeEntityManagerFactory().createEntityManager());
        return ExtendedEntityManagerCreator.createApplicationManagedEntityManager(rawEntityManager, this, true);
    }

    // Look for Query arguments, primarily JPA 2.1's addNamedQuery(String, Query)
    if (args != null) {
        for (int i = 0; i < args.length; i++) {
            Object arg = args[i];
            if (arg instanceof Query && Proxy.isProxyClass(arg.getClass())) {
                // Assumably a Spring-generated proxy from SharedEntityManagerCreator:
                // since we're passing it back to the native EntityManagerFactory,
                // let's unwrap it to the original Query object from the provider.
                try {
                    args[i] = ((Query) arg).unwrap(null);
                } catch (RuntimeException ex) {
                    // Ignore - simply proceed with given Query object then
                }
            }
        }
    }

    // Standard delegation to the native factory, just post-processing EntityManager return values
    Object retVal = method.invoke(getNativeEntityManagerFactory(), args);
    if (retVal instanceof EntityManager) {
        // Any other createEntityManager variant - expecting non-synchronized semantics
        EntityManager rawEntityManager = (EntityManager) retVal;
        retVal = ExtendedEntityManagerCreator.createApplicationManagedEntityManager(rawEntityManager, this,
                false);
    }
    return retVal;
}