Search in sources :

Example 6 with EntityManagerHolder

use of org.springframework.orm.jpa.EntityManagerHolder in project spring-framework by spring-projects.

the class SharedEntityManagerFactoryTests method testValidUsage.

@Test
public void testValidUsage() {
    Object o = new Object();
    EntityManager mockEm = mock(EntityManager.class);
    given(mockEm.isOpen()).willReturn(true);
    EntityManagerFactory mockEmf = mock(EntityManagerFactory.class);
    given(mockEmf.createEntityManager()).willReturn(mockEm);
    SharedEntityManagerBean proxyFactoryBean = new SharedEntityManagerBean();
    proxyFactoryBean.setEntityManagerFactory(mockEmf);
    proxyFactoryBean.afterPropertiesSet();
    assertTrue(EntityManager.class.isAssignableFrom(proxyFactoryBean.getObjectType()));
    assertTrue(proxyFactoryBean.isSingleton());
    EntityManager proxy = proxyFactoryBean.getObject();
    assertSame(proxy, proxyFactoryBean.getObject());
    assertFalse(proxy.contains(o));
    assertTrue(proxy instanceof EntityManagerProxy);
    EntityManagerProxy emProxy = (EntityManagerProxy) proxy;
    try {
        emProxy.getTargetEntityManager();
        fail("Should have thrown IllegalStateException outside of transaction");
    } catch (IllegalStateException ex) {
    // expected
    }
    TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(mockEm));
    try {
        assertSame(mockEm, emProxy.getTargetEntityManager());
    } finally {
        TransactionSynchronizationManager.unbindResource(mockEmf);
    }
    assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
    verify(mockEm).contains(o);
    verify(mockEm).close();
}
Also used : EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder) EntityManagerProxy(org.springframework.orm.jpa.EntityManagerProxy) Test(org.junit.Test)

Example 7 with EntityManagerHolder

use of org.springframework.orm.jpa.EntityManagerHolder in project uPortal by Jasig.

the class JpaPortletExecutionInterceptor method preExecution.

@Override
public void preExecution(HttpServletRequest request, HttpServletResponse response, IPortletExecutionContext context) {
    // Determine current EntityManager: either the transactional one
    // managed by the factory or a temporary one for the given invocation.
    EntityManager em = getTransactionalEntityManager();
    boolean isNewEm = false;
    if (em == null) {
        logger.debug("Creating new EntityManager for JpaInterceptor invocation");
        em = createEntityManager();
        isNewEm = true;
        TransactionSynchronizationManager.bindResource(getEntityManagerFactory(), new EntityManagerHolder(em));
        //For new EM store as attribute so it can be closed
        context.setExecutionAttribute(ENTITY_MANAGER_FACTORY, em);
    }
    context.setExecutionAttribute(IS_NEW, isNewEm);
}
Also used : EntityManager(javax.persistence.EntityManager) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder)

Example 8 with EntityManagerHolder

use of org.springframework.orm.jpa.EntityManagerHolder in project spring-boot by spring-projects.

the class TestEntityManagerTests method bindEntityManager.

private void bindEntityManager() {
    EntityManagerHolder holder = new EntityManagerHolder(this.entityManager);
    TransactionSynchronizationManager.bindResource(this.entityManagerFactory, holder);
}
Also used : EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder)

Example 9 with EntityManagerHolder

use of org.springframework.orm.jpa.EntityManagerHolder in project uPortal by Jasig.

the class OpenEntityManagerAspect method openEntityManager.

@Around("anyPublicMethod() && @annotation(openEntityManager)")
public Object openEntityManager(ProceedingJoinPoint pjp, OpenEntityManager openEntityManager) throws Throwable {
    final EntityManagerFactory emf = getEntityManagerFactory(openEntityManager);
    EntityManager em = getTransactionalEntityManager(emf);
    boolean isNewEm = false;
    if (em == null) {
        logger.debug("Opening JPA EntityManager in OpenEntityManagerAspect");
        em = createEntityManager(emf);
        isNewEm = true;
        TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em));
    } else {
        logger.debug("Using Existing JPA EntityManager in OpenEntityManagerAspect");
    }
    try {
        return pjp.proceed();
    } finally {
        if (isNewEm) {
            logger.debug("Closing JPA EntityManager in OpenEntityManagerAspect");
            TransactionSynchronizationManager.unbindResource(emf);
            EntityManagerFactoryUtils.closeEntityManager(em);
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder) Around(org.aspectj.lang.annotation.Around)

Aggregations

EntityManagerHolder (org.springframework.orm.jpa.EntityManagerHolder)9 EntityManager (javax.persistence.EntityManager)7 EntityManagerFactory (javax.persistence.EntityManagerFactory)3 Test (org.junit.Test)3 Properties (java.util.Properties)2 PersistenceException (javax.persistence.PersistenceException)2 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)2 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)2 Around (org.aspectj.lang.annotation.Around)1 EntityManagerProxy (org.springframework.orm.jpa.EntityManagerProxy)1