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();
}
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);
}
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);
}
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);
}
}
}
Aggregations