private EntityManager resolveExtendedEntityManager(Object target, String requestingBeanName) { // Obtain EntityManager reference from JNDI? EntityManager em = getPersistenceContext(this.unitName, true); if (em == null) { // No pre-built EntityManager found -> build one based on factory. // Obtain EntityManagerFactory from JNDI? EntityManagerFactory emf = getPersistenceUnit(this.unitName); if (emf == null) { // Need to search for EntityManagerFactory beans. emf = findEntityManagerFactory(this.unitName, requestingBeanName); } // Inject a container-managed extended EntityManager. em = ExtendedEntityManagerCreator.createContainerManagedEntityManager( emf, this.properties, this.synchronizedWithTransaction); } if (em instanceof EntityManagerProxy && beanFactory != null && !beanFactory.isPrototype(requestingBeanName)) { extendedEntityManagersToClose.put(target, ((EntityManagerProxy) em).getTargetEntityManager()); } return em; }
private EntityManager resolveExtendedEntityManager(Object target, String requestingBeanName) { // Obtain EntityManager reference from JNDI? EntityManager em = getPersistenceContext(this.unitName, true); if (em == null) { // No pre-built EntityManager found -> build one based on factory. // Obtain EntityManagerFactory from JNDI? EntityManagerFactory emf = getPersistenceUnit(this.unitName); if (emf == null) { // Need to search for EntityManagerFactory beans. emf = findEntityManagerFactory(this.unitName, requestingBeanName); } // Inject a container-managed extended EntityManager. em = ExtendedEntityManagerCreator.createContainerManagedEntityManager( emf, this.properties, this.synchronizedWithTransaction); } if (em instanceof EntityManagerProxy && beanFactory != null && beanFactory.containsBean(requestingBeanName) && !beanFactory.isPrototype(requestingBeanName)) { extendedEntityManagersToClose.put(target, ((EntityManagerProxy) em).getTargetEntityManager()); } return em; }
private EntityManager resolveExtendedEntityManager(Object target, String requestingBeanName) { // Obtain EntityManager reference from JNDI? EntityManager em = getPersistenceContext(this.unitName, true); if (em == null) { // No pre-built EntityManager found -> build one based on factory. // Obtain EntityManagerFactory from JNDI? EntityManagerFactory emf = getPersistenceUnit(this.unitName); if (emf == null) { // Need to search for EntityManagerFactory beans. emf = findEntityManagerFactory(this.unitName, requestingBeanName); } // Inject a container-managed extended EntityManager. em = ExtendedEntityManagerCreator.createContainerManagedEntityManager(emf, this.properties); } if (em instanceof EntityManagerProxy && beanFactory != null && !beanFactory.isPrototype(requestingBeanName)) { extendedEntityManagersToClose.put(target, ((EntityManagerProxy) em).getTargetEntityManager()); } return em; }
@PostConstruct public void initialize() { if(!(this.entityManager instanceof EntityManagerProxy)) throw new FatalBeanException("Entity manager " + this.entityManager + " was not a proxy"); this.entityManagerProxy = (EntityManagerProxy) this.entityManager; }
@PostConstruct public void init() { if (!(this.entityManager instanceof EntityManagerProxy)) { throw new FatalBeanException("Entity Manager" + this.entityManager + " is not a proxy"); } this.proxy = (EntityManagerProxy) this.entityManager; }
@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(); }