org.springframework.orm.jpa.EntityManagerHolder Java Examples

The following examples show how to use org.springframework.orm.jpa.EntityManagerHolder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JpaTransactionManagerHandlerTest.java    From opensharding-spi-impl with Apache License 2.0 5 votes vote down vote up
@Test
public void assertUnbindResource() {
    EntityManagerHolder holder = mock(EntityManagerHolder.class);
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    when(holder.getEntityManager()).thenReturn(entityManager);
    TransactionSynchronizationManager.bindResource(entityManagerFactory, holder);
    jpaTransactionManagerHandler.unbindResource();
    assertNull(TransactionSynchronizationManager.getResource(entityManagerFactory));
}
 
Example #2
Source File: MultiTenantJpaTransactionManager.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void doBegin(final Object transaction, final TransactionDefinition definition) {
    super.doBegin(transaction, definition);

    final String currentTenant = tenantAware.getCurrentTenant();
    if (currentTenant != null) {
        final EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager
                .getResource(getEntityManagerFactory());
        final EntityManager em = emHolder.getEntityManager();
        em.setProperty(PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT, currentTenant.toUpperCase());
    }
}
 
Example #3
Source File: JunitEntityManagerHolder.java    From dpCms with Apache License 2.0 5 votes vote down vote up
@Before
public void doBefore(){
	this.em = emf.createEntityManager();
	EntityManagerHolder emHolder = new EntityManagerHolder(em);
	TransactionSynchronizationManager.bindResource(emf,
			emHolder);
}
 
Example #4
Source File: PersistenceInjectionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testPropertiesForSharedEntityManager2() {
	Properties props = new Properties();
	props.put("foo", "bar");
	EntityManager em = mock(EntityManager.class);
	// only one call made  - the first EM definition wins (in this case the one w/o the properties)
	given(mockEmf.createEntityManager()).willReturn(em);
	given(em.getDelegate()).willReturn(new Object(), 2);
	given(em.isOpen()).willReturn(true);

	PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
	DefaultPrivatePersistenceContextFieldWithProperties transactionalFieldWithProperties =
			new DefaultPrivatePersistenceContextFieldWithProperties();
	DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();

	pabpp.postProcessPropertyValues(null, null, transactionalFieldWithProperties, "bean1");
	pabpp.postProcessPropertyValues(null, null, transactionalField, "bean2");

	assertNotNull(transactionalFieldWithProperties.em);
	assertNotNull(transactionalField.em);
	// the EM w/o properties will be created
	assertNotNull(transactionalField.em.getDelegate());
	// bind em to the thread now since it's created
	try {
		TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(em));
		assertNotNull(transactionalFieldWithProperties.em.getDelegate());
		verify(em).close();
	}
	finally {
		TransactionSynchronizationManager.unbindResource(mockEmf);
	}
}
 
Example #5
Source File: PersistenceInjectionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Binds an EMF to the thread and tests if EM with different properties
 * generate new EMs or not.
 */
@Test
public void testPropertiesForSharedEntityManager1() {
	Properties props = new Properties();
	props.put("foo", "bar");
	EntityManager em = mock(EntityManager.class);
	// only one call made  - the first EM definition wins (in this case the one w/ the properties)
	given(mockEmf.createEntityManager(props)).willReturn(em);
	given(em.getDelegate()).willReturn(new Object());
	given(em.isOpen()).willReturn(true);

	PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
	DefaultPrivatePersistenceContextFieldWithProperties transactionalFieldWithProperties =
			new DefaultPrivatePersistenceContextFieldWithProperties();
	DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();

	pabpp.postProcessPropertyValues(null, null, transactionalFieldWithProperties, "bean1");
	pabpp.postProcessPropertyValues(null, null, transactionalField, "bean2");

	assertNotNull(transactionalFieldWithProperties.em);
	assertNotNull(transactionalField.em);
	// the EM w/ properties will be created
	assertNotNull(transactionalFieldWithProperties.em.getDelegate());
	// bind em to the thread now since it's created
	try {
		TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(em));
		assertNotNull(transactionalField.em.getDelegate());
		verify(em).close();
	}
	finally {
		TransactionSynchronizationManager.unbindResource(mockEmf);
	}
}
 
Example #6
Source File: OpenEntityManagerInViewInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
	if (!decrementParticipateCount(request)) {
		EntityManagerHolder emHolder = (EntityManagerHolder)
				TransactionSynchronizationManager.unbindResource(getEntityManagerFactory());
		logger.debug("Closing JPA EntityManager in OpenEntityManagerInViewInterceptor");
		EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
	}
}
 
Example #7
Source File: OpenEntityManagerInViewInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String participateAttributeName = getParticipateAttributeName();

	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult()) {
		if (applyCallableInterceptor(asyncManager, participateAttributeName)) {
			return;
		}
	}

	if (TransactionSynchronizationManager.hasResource(getEntityManagerFactory())) {
		// Do not modify the EntityManager: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening JPA EntityManager in OpenEntityManagerInViewInterceptor");
		try {
			EntityManager em = createEntityManager();
			EntityManagerHolder emHolder = new EntityManagerHolder(em);
			TransactionSynchronizationManager.bindResource(getEntityManagerFactory(), emHolder);

			AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(getEntityManagerFactory(), emHolder);
			asyncManager.registerCallableInterceptor(participateAttributeName, interceptor);
			asyncManager.registerDeferredResultInterceptor(participateAttributeName, interceptor);
		}
		catch (PersistenceException ex) {
			throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
		}
	}
}
 
Example #8
Source File: EntityManagerUtil.java    From apollo with Apache License 2.0 5 votes vote down vote up
/**
 * close the entity manager.
 * Use it with caution! This is only intended for use with async request, which Spring won't
 * close the entity manager until the async request is finished.
 */
public void closeEntityManager() {
  EntityManagerHolder emHolder = (EntityManagerHolder)
      TransactionSynchronizationManager.getResource(getEntityManagerFactory());
  if (emHolder == null) {
    return;
  }
  logger.debug("Closing JPA EntityManager in EntityManagerUtil");
  EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
}
 
Example #9
Source File: OpenEntityManagerInViewInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
	if (!decrementParticipateCount(request)) {
		EntityManagerHolder emHolder = (EntityManagerHolder)
				TransactionSynchronizationManager.unbindResource(getEntityManagerFactory());
		logger.debug("Closing JPA EntityManager in OpenEntityManagerInViewInterceptor");
		EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
	}
}
 
Example #10
Source File: OpenEntityManagerInViewInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String participateAttributeName = getParticipateAttributeName();

	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult()) {
		if (applyCallableInterceptor(asyncManager, participateAttributeName)) {
			return;
		}
	}

	if (TransactionSynchronizationManager.hasResource(getEntityManagerFactory())) {
		// Do not modify the EntityManager: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening JPA EntityManager in OpenEntityManagerInViewInterceptor");
		try {
			EntityManager em = createEntityManager();
			EntityManagerHolder emHolder = new EntityManagerHolder(em);
			TransactionSynchronizationManager.bindResource(getEntityManagerFactory(), emHolder);

			AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(getEntityManagerFactory(), emHolder);
			asyncManager.registerCallableInterceptor(participateAttributeName, interceptor);
			asyncManager.registerDeferredResultInterceptor(participateAttributeName, interceptor);
		}
		catch (PersistenceException ex) {
			throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
		}
	}
}
 
Example #11
Source File: OpenEntityManagerInViewInterceptor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String key = getParticipateAttributeName();
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult() && applyEntityManagerBindingInterceptor(asyncManager, key)) {
		return;
	}

	EntityManagerFactory emf = obtainEntityManagerFactory();
	if (TransactionSynchronizationManager.hasResource(emf)) {
		// Do not modify the EntityManager: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(key, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening JPA EntityManager in OpenEntityManagerInViewInterceptor");
		try {
			EntityManager em = createEntityManager();
			EntityManagerHolder emHolder = new EntityManagerHolder(em);
			TransactionSynchronizationManager.bindResource(emf, emHolder);

			AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(emf, emHolder);
			asyncManager.registerCallableInterceptor(key, interceptor);
			asyncManager.registerDeferredResultInterceptor(key, interceptor);
		}
		catch (PersistenceException ex) {
			throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
		}
	}
}
 
Example #12
Source File: JpaTransactionManagerHandler.java    From opensharding-spi-impl with Apache License 2.0 5 votes vote down vote up
@Override
protected Connection getConnectionFromTransactionManager() {
    EntityManager entityManager = createEntityManager();
    Collection<JpaConnectionExtractor> jpaConnectionExtractors = NewInstanceServiceLoader.newServiceInstances(JpaConnectionExtractor.class);
    if (jpaConnectionExtractors.isEmpty()) {
        log.warn("Failed to get connection to proxy, caused by no JpaConnectionExtractor.");
        throw new ShardingException("No JpaConnectionExtractor loaded");
    }
    Connection result = jpaConnectionExtractors.iterator().next().getConnectionFromEntityManager(entityManager);
    TransactionSynchronizationManager.bindResource(transactionManager.getEntityManagerFactory(), new EntityManagerHolder(entityManager));
    return result;
}
 
Example #13
Source File: PersistenceInjectionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPropertiesForSharedEntityManager2() {
	Properties props = new Properties();
	props.put("foo", "bar");
	EntityManager em = mock(EntityManager.class);
	// only one call made  - the first EM definition wins (in this case the one w/o the properties)
	given(mockEmf.createEntityManager()).willReturn(em);
	given(em.getDelegate()).willReturn(new Object(), 2);
	given(em.isOpen()).willReturn(true);

	PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
	DefaultPrivatePersistenceContextFieldWithProperties transactionalFieldWithProperties =
			new DefaultPrivatePersistenceContextFieldWithProperties();
	DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();

	pabpp.postProcessProperties(null, transactionalFieldWithProperties, "bean1");
	pabpp.postProcessProperties(null, transactionalField, "bean2");

	assertNotNull(transactionalFieldWithProperties.em);
	assertNotNull(transactionalField.em);
	// the EM w/o properties will be created
	assertNotNull(transactionalField.em.getDelegate());
	// bind em to the thread now since it's created
	try {
		TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(em));
		assertNotNull(transactionalFieldWithProperties.em.getDelegate());
		verify(em).close();
	}
	finally {
		TransactionSynchronizationManager.unbindResource(mockEmf);
	}
}
 
Example #14
Source File: PersistenceInjectionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Binds an EMF to the thread and tests if EM with different properties
 * generate new EMs or not.
 */
@Test
public void testPropertiesForSharedEntityManager1() {
	Properties props = new Properties();
	props.put("foo", "bar");
	EntityManager em = mock(EntityManager.class);
	// only one call made  - the first EM definition wins (in this case the one w/ the properties)
	given(mockEmf.createEntityManager(props)).willReturn(em);
	given(em.getDelegate()).willReturn(new Object());
	given(em.isOpen()).willReturn(true);

	PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
	DefaultPrivatePersistenceContextFieldWithProperties transactionalFieldWithProperties =
			new DefaultPrivatePersistenceContextFieldWithProperties();
	DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();

	pabpp.postProcessProperties(null, transactionalFieldWithProperties, "bean1");
	pabpp.postProcessProperties(null, transactionalField, "bean2");

	assertNotNull(transactionalFieldWithProperties.em);
	assertNotNull(transactionalField.em);
	// the EM w/ properties will be created
	assertNotNull(transactionalFieldWithProperties.em.getDelegate());
	// bind em to the thread now since it's created
	try {
		TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(em));
		assertNotNull(transactionalField.em.getDelegate());
		verify(em).close();
	}
	finally {
		TransactionSynchronizationManager.unbindResource(mockEmf);
	}
}
 
Example #15
Source File: OpenEntityManagerInViewInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void afterCompletion(WebRequest request, @Nullable Exception ex) throws DataAccessException {
	if (!decrementParticipateCount(request)) {
		EntityManagerHolder emHolder = (EntityManagerHolder)
				TransactionSynchronizationManager.unbindResource(obtainEntityManagerFactory());
		logger.debug("Closing JPA EntityManager in OpenEntityManagerInViewInterceptor");
		EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
	}
}
 
Example #16
Source File: OpenEntityManagerInViewInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void preHandle(WebRequest request) throws DataAccessException {
	String key = getParticipateAttributeName();
	WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
	if (asyncManager.hasConcurrentResult() && applyEntityManagerBindingInterceptor(asyncManager, key)) {
		return;
	}

	EntityManagerFactory emf = obtainEntityManagerFactory();
	if (TransactionSynchronizationManager.hasResource(emf)) {
		// Do not modify the EntityManager: just mark the request accordingly.
		Integer count = (Integer) request.getAttribute(key, WebRequest.SCOPE_REQUEST);
		int newCount = (count != null ? count + 1 : 1);
		request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
	}
	else {
		logger.debug("Opening JPA EntityManager in OpenEntityManagerInViewInterceptor");
		try {
			EntityManager em = createEntityManager();
			EntityManagerHolder emHolder = new EntityManagerHolder(em);
			TransactionSynchronizationManager.bindResource(emf, emHolder);

			AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(emf, emHolder);
			asyncManager.registerCallableInterceptor(key, interceptor);
			asyncManager.registerDeferredResultInterceptor(key, interceptor);
		}
		catch (PersistenceException ex) {
			throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
		}
	}
}
 
Example #17
Source File: PersistenceInjectionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testPropertiesForSharedEntityManager2() {
	Properties props = new Properties();
	props.put("foo", "bar");
	EntityManager em = mock(EntityManager.class);
	// only one call made  - the first EM definition wins (in this case the one w/o the properties)
	given(mockEmf.createEntityManager()).willReturn(em);
	given(em.getDelegate()).willReturn(new Object(), 2);
	given(em.isOpen()).willReturn(true);

	PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
	DefaultPrivatePersistenceContextFieldWithProperties transactionalFieldWithProperties =
			new DefaultPrivatePersistenceContextFieldWithProperties();
	DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();

	pabpp.postProcessProperties(null, transactionalFieldWithProperties, "bean1");
	pabpp.postProcessProperties(null, transactionalField, "bean2");

	assertNotNull(transactionalFieldWithProperties.em);
	assertNotNull(transactionalField.em);
	// the EM w/o properties will be created
	assertNotNull(transactionalField.em.getDelegate());
	// bind em to the thread now since it's created
	try {
		TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(em));
		assertNotNull(transactionalFieldWithProperties.em.getDelegate());
		verify(em).close();
	}
	finally {
		TransactionSynchronizationManager.unbindResource(mockEmf);
	}
}
 
Example #18
Source File: PersistenceInjectionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Binds an EMF to the thread and tests if EM with different properties
 * generate new EMs or not.
 */
@Test
public void testPropertiesForSharedEntityManager1() {
	Properties props = new Properties();
	props.put("foo", "bar");
	EntityManager em = mock(EntityManager.class);
	// only one call made  - the first EM definition wins (in this case the one w/ the properties)
	given(mockEmf.createEntityManager(props)).willReturn(em);
	given(em.getDelegate()).willReturn(new Object());
	given(em.isOpen()).willReturn(true);

	PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor();
	DefaultPrivatePersistenceContextFieldWithProperties transactionalFieldWithProperties =
			new DefaultPrivatePersistenceContextFieldWithProperties();
	DefaultPrivatePersistenceContextField transactionalField = new DefaultPrivatePersistenceContextField();

	pabpp.postProcessProperties(null, transactionalFieldWithProperties, "bean1");
	pabpp.postProcessProperties(null, transactionalField, "bean2");

	assertNotNull(transactionalFieldWithProperties.em);
	assertNotNull(transactionalField.em);
	// the EM w/ properties will be created
	assertNotNull(transactionalFieldWithProperties.em.getDelegate());
	// bind em to the thread now since it's created
	try {
		TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(em));
		assertNotNull(transactionalField.em.getDelegate());
		verify(em).close();
	}
	finally {
		TransactionSynchronizationManager.unbindResource(mockEmf);
	}
}
 
Example #19
Source File: OpenEntityManagerInViewInterceptor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void afterCompletion(WebRequest request, @Nullable Exception ex) throws DataAccessException {
	if (!decrementParticipateCount(request)) {
		EntityManagerHolder emHolder = (EntityManagerHolder)
				TransactionSynchronizationManager.unbindResource(obtainEntityManagerFactory());
		logger.debug("Closing JPA EntityManager in OpenEntityManagerInViewInterceptor");
		EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
	}
}
 
Example #20
Source File: JpaTransactionManagerHandler.java    From opensharding-spi-impl with Apache License 2.0 4 votes vote down vote up
@Override
public void unbindResource() {
    EntityManagerHolder entityManagerHolder = (EntityManagerHolder) TransactionSynchronizationManager.unbindResourceIfPossible(transactionManager.getEntityManagerFactory());
    EntityManagerFactoryUtils.closeEntityManager(entityManagerHolder.getEntityManager());
}
 
Example #21
Source File: SharedEntityManagerFactoryTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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();
}
 
Example #22
Source File: AsyncRequestInterceptor.java    From java-technology-stack with MIT License 4 votes vote down vote up
public AsyncRequestInterceptor(EntityManagerFactory emFactory, EntityManagerHolder emHolder) {
	this.emFactory = emFactory;
	this.emHolder = emHolder;
}
 
Example #23
Source File: AsyncRequestInterceptor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public AsyncRequestInterceptor(EntityManagerFactory emFactory, EntityManagerHolder emHolder) {
	this.emFactory = emFactory;
	this.emHolder = emHolder;
}
 
Example #24
Source File: SharedEntityManagerFactoryTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@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();
}
 
Example #25
Source File: AsyncRequestInterceptor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public AsyncRequestInterceptor(EntityManagerFactory emFactory, EntityManagerHolder emHolder) {
	this.emFactory = emFactory;
	this.emHolder = emHolder;
}
 
Example #26
Source File: SharedEntityManagerFactoryTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@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();
}
 
Example #27
Source File: AsyncRequestInterceptor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public AsyncRequestInterceptor(EntityManagerFactory emFactory, EntityManagerHolder emHolder) {
	this.emFactory = emFactory;
	this.emHolder = emHolder;
}
 
Example #28
Source File: JunitEntityManagerHolder.java    From dpCms with Apache License 2.0 4 votes vote down vote up
@After
public void doAfter(){
	EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager
			.unbindResource(emf);
}