List of usage examples for org.springframework.orm.jpa EntityManagerHolder getEntityManager
public EntityManager getEntityManager()
From source file:hsa.awp.common.util.EntityManagerUtil.java
/** * Finds the current {@link EntityManager} which is bound to the current Thread. If there is no {@link EntityManager} mapped, * <code>null</code> will be returned. * * @return the bound {@link EntityManager} or <code>null</code> if no one can be found. */// w w w . j a v a 2 s . c o m public static EntityManager getEntityManagerFromThread() { if (TransactionSynchronizationManager.hasResource(emf)) { EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.getResource(emf); return emHolder.getEntityManager(); } else { return null; } }
From source file:corner.orm.gae.impl.EntityManagerSourceImpl.java
/** * @see lichen.common.services.jpa.EntityManagerSource#getEntityManager() *//* ww w. j a v a 2 s .c o m*/ @Override public EntityManager getEntityManager() { EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager .getResource(entityManagerFactory); return emHolder.getEntityManager(); }
From source file:com.dcsbootcamp.maven.integrationtest.GreeterConfig.java
/** * Provides a proxy instance of the current transaction's entity manager * which is ready to inject via constructor injection and guarantees * immutability./*from w w w . jav a2 s. c om*/ * * @param emf the entity manager factory * @return a proxy of entity manager */ @Bean @Scope(SCOPE_PROTOTYPE) EntityManager entityManagerProvider(EntityManagerFactory emf) { EntityManagerHolder holder = (EntityManagerHolder) getResource(emf); if (holder == null) { return emf.createEntityManager(); } return holder.getEntityManager(); }
From source file:hsa.awp.common.util.OpenEntityManagerTimerTaskFactory.java
/** * Closes the previously created {@link EntityManager}. *///from w ww. j a va 2s. c o m private void closeEntityManager() { log.trace("Unbinding EntityManager from thread '{}'", Thread.currentThread().getName()); EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.unbindResource(emf); log.debug("Closing EntityManager"); EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager()); }
From source file:hsa.awp.common.test.OpenEntityManagerInTest.java
/** * Closes the previously created {@link EntityManager}. */// ww w. j av a 2 s . c o m @After public final void closeEntityManager() { if (create) { log.trace("Unbinding EntityManager from thread '{}'", Thread.currentThread().getName()); EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager .unbindResource(emf); log.debug("Closing EntityManager"); EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager()); } else { log.debug("Don't close EntityManager because of @Transactional annotation"); } }
From source file:corner.orm.gae.impl.EntityManagerSourceImpl.java
@Override public void threadDidCleanup() { if (!participate) { EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager .unbindResource(entityManagerFactory); logger.debug("Closing JPA EntityManager in EntityManagerSourceImpl"); EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager()); }//from w ww .ja v a 2 s .c o m }
From source file:ar.com.zauber.commons.repository.closures.OpenEntityManagerClosure.java
/** @see Closure#execute(Object) */ public final void execute(final T t) { if (dryrun) { //permite evitar que se hagan commit() en el ambiente de test target.execute(t);// w ww . j a va2 s. c o m } else { boolean participate = false; EntityTransaction transaction = null; if (TransactionSynchronizationManager.hasResource(emf)) { // Do not modify the EntityManager: just set the participate flag. participate = true; } else { try { final EntityManager em = emf.createEntityManager(); if (openTx) { if (!warningPrinted) { logger.warn( "The OpenEntityManagerClosure has Transactions enabled and is not recommended" + ". This behaviour will change in the future. Check setOpenTx(), "); } transaction = em.getTransaction(); transaction.begin(); } TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em)); } catch (PersistenceException ex) { throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex); } } if (transaction != null) { try { target.execute(t); if (transaction.getRollbackOnly()) { transaction.rollback(); } else { transaction.commit(); } } catch (final Throwable e) { if (transaction != null && transaction.isActive()) { transaction.rollback(); } throw new UnhandledException(e); } finally { if (!participate) { final EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager .unbindResource(emf); EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager()); } } } else { try { target.execute(t); } finally { if (!participate) { final EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager .unbindResource(emf); EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager()); } } } } }
From source file:com.dcsbootcamp.maven.fixture.PostgresTestConfig.java
/** * Provides a proxy instance of the current transaction's entity manager * which is ready to inject via constructor injection and guarantees * immutability./*from w w w.jav a 2s. co m*/ * * @param emf the entity manager factory * @return a proxy of entity manager */ @Bean @Scope(SCOPE_PROTOTYPE) EntityManager entityManagerProvider(EntityManagerFactory emf) { EntityManagerHolder holder = (EntityManagerHolder) getResource(emf); if (holder == null) { throw new IllegalStateException("Transaction not available. Is your service annotated with" + " @Transactional? Did you enable @EnableTransactionManagement?"); } return holder.getEntityManager(); }
From source file:org.drools.container.spring.beans.persistence.DroolsSpringJpaManager.java
public void beginCommandScopedEntityManager() { EntityManager cmdScopedEntityManager = (EntityManager) env.get(EnvironmentName.CMD_SCOPED_ENTITY_MANAGER); if (cmdScopedEntityManager == null || !cmdScopedEntityManager.isOpen()) { EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager .getResource("cmdEM"); EntityManager em = null;/*from w w w. jav a2 s . c om*/ if (emHolder == null) { em = this.emf.createEntityManager(); emHolder = new EntityManagerHolder(em); TransactionSynchronizationManager.bindResource("cmdEM", emHolder); } else { em = emHolder.getEntityManager(); } this.env.set(EnvironmentName.CMD_SCOPED_ENTITY_MANAGER, em); } this.getCommandScopedPersistenceContext().joinTransaction(); this.appScopedEntityManager.joinTransaction(); }
From source file:org.drools.container.spring.beans.persistence.DroolsSpringJpaManager.java
public PersistenceContext getApplicationScopedPersistenceContext() { if (this.appScopedEntityManager == null) { // Use the App scoped EntityManager if the user has provided it, and it is open. this.appScopedEntityManager = (EntityManager) this.env.get(EnvironmentName.APP_SCOPED_ENTITY_MANAGER); if (this.appScopedEntityManager != null && !this.appScopedEntityManager.isOpen()) { throw new RuntimeException("Provided APP_SCOPED_ENTITY_MANAGER is not open"); }//w w w .j av a 2 s.c o m if (this.appScopedEntityManager == null) { EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager .getResource(this.emf); if (emHolder == null) { this.appScopedEntityManager = this.emf.createEntityManager(); emHolder = new EntityManagerHolder(this.appScopedEntityManager); TransactionSynchronizationManager.bindResource(this.emf, emHolder); internalAppScopedEntityManager = true; } else { this.appScopedEntityManager = emHolder.getEntityManager(); } this.env.set(EnvironmentName.APP_SCOPED_ENTITY_MANAGER, emHolder.getEntityManager()); } } if (TransactionSynchronizationManager.isActualTransactionActive()) { this.appScopedEntityManager.joinTransaction(); } return new JpaPersistenceContext(this.appScopedEntityManager); }