/** * Obtain a JPA EntityManager from the given factory. Is aware of a corresponding * EntityManager bound to the current thread, e.g. when using JpaTransactionManager. * <p>Note: Will return {@code null} if no thread-bound EntityManager found! * @param emf the EntityManagerFactory to create the EntityManager with * @return the EntityManager, or {@code null} if none found * @throws DataAccessResourceFailureException if the EntityManager couldn't be obtained * @see JpaTransactionManager */ @Nullable public static EntityManager getTransactionalEntityManager(EntityManagerFactory emf) throws DataAccessResourceFailureException { return getTransactionalEntityManager(emf, null); }
@Override public void postProcessBeforeDestruction(Object bean, String beanName) { EntityManager emToClose = this.extendedEntityManagersToClose.remove(bean); EntityManagerFactoryUtils.closeEntityManager(emToClose); }
/** * Find an EntityManagerFactory with the given name in the current * Spring application context. * @param unitName the name of the persistence unit (never empty) * @param requestingBeanName the name of the requesting bean * @return the EntityManagerFactory * @throws NoSuchBeanDefinitionException if there is no such EntityManagerFactory in the context */ protected EntityManagerFactory findNamedEntityManagerFactory(String unitName, @Nullable String requestingBeanName) throws NoSuchBeanDefinitionException { Assert.state(this.beanFactory != null, "ListableBeanFactory required for EntityManagerFactory bean lookup"); EntityManagerFactory emf = EntityManagerFactoryUtils.findEntityManagerFactory(this.beanFactory, unitName); if (requestingBeanName != null && this.beanFactory instanceof ConfigurableBeanFactory) { ((ConfigurableBeanFactory) this.beanFactory).registerDependentBean(unitName, requestingBeanName); } return emf; }
EntityManager target = EntityManagerFactoryUtils.doGetTransactionalEntityManager( this.targetFactory, this.properties, this.synchronizedWithTransaction); EntityManagerFactoryUtils.applyTransactionTimeout(query, this.targetFactory); EntityManagerFactoryUtils.closeEntityManager(target);
protected EntityManager entityManager() { EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager( EntityManagerFactoryUtils.findEntityManagerFactory( ApplicationContextProvider.getBeanFactory(), AuthContextUtils.getDomain())); if (entityManager == null) { throw new IllegalStateException("Could not find EntityManager for domain " + AuthContextUtils.getDomain()); } return entityManager; }
throws DataAccessResourceFailureException { return getTransactionalEntityManager(emf, null); throws PersistenceException { return doGetTransactionalEntityManager(emf, properties, true);
/** * This implementation delegates to EntityManagerFactoryUtils. * @see EntityManagerFactoryUtils#convertJpaAccessExceptionIfPossible */ @Override @Nullable public DataAccessException translateExceptionIfPossible(RuntimeException ex) { return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex); }
Object transactionData = prepareTransaction(emHolder.getEntityManager(), emf); TransactionSynchronizationManager.registerSynchronization( new TransactionalEntityManagerSynchronization(emHolder, emf, transactionData, false)); Object transactionData = prepareTransaction(em, emf); TransactionSynchronizationManager.registerSynchronization( new TransactionalEntityManagerSynchronization(emHolder, emf, transactionData, true)); closeEntityManager(em); throw ex;
/** * Obtain a JPA EntityManager from the given factory. Is aware of a corresponding * EntityManager bound to the current thread, e.g. when using JpaTransactionManager. * <p>Same as {@code getEntityManager}, but throwing the original PersistenceException. * @param emf the EntityManagerFactory to create the EntityManager with * @param properties the properties to be passed into the {@code createEntityManager} * call (may be {@code null}) * @return the EntityManager, or {@code null} if none found * @throws javax.persistence.PersistenceException if the EntityManager couldn't be created * @see #getTransactionalEntityManager(javax.persistence.EntityManagerFactory) * @see JpaTransactionManager */ @Nullable public static EntityManager doGetTransactionalEntityManager(EntityManagerFactory emf, Map<?, ?> properties) throws PersistenceException { return doGetTransactionalEntityManager(emf, properties, true); }
EntityManager target = EntityManagerFactoryUtils.doGetTransactionalEntityManager( this.targetFactory, this.properties, this.synchronizedWithTransaction); EntityManagerFactoryUtils.applyTransactionTimeout(query, this.targetFactory); EntityManagerFactoryUtils.closeEntityManager(target);
private RuntimeException convertException(RuntimeException ex) { DataAccessException dae = (this.exceptionTranslator != null) ? this.exceptionTranslator.translateExceptionIfPossible(ex) : EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex); return (dae != null ? dae : ex); } }
/** * Obtain a JPA EntityManager from the given factory. Is aware of a corresponding * EntityManager bound to the current thread, e.g. when using JpaTransactionManager. * <p>Note: Will return {@code null} if no thread-bound EntityManager found! * @param emf the EntityManagerFactory to create the EntityManager with * @param properties the properties to be passed into the {@code createEntityManager} * call (may be {@code null}) * @return the EntityManager, or {@code null} if none found * @throws DataAccessResourceFailureException if the EntityManager couldn't be obtained * @see JpaTransactionManager */ @Nullable public static EntityManager getTransactionalEntityManager(EntityManagerFactory emf, @Nullable Map<?, ?> properties) throws DataAccessResourceFailureException { try { return doGetTransactionalEntityManager(emf, properties, true); } catch (PersistenceException ex) { throw new DataAccessResourceFailureException("Could not obtain JPA EntityManager", ex); } }
/** * Obtain a JPA EntityManager from the given factory. Is aware of a corresponding * EntityManager bound to the current thread, e.g. when using JpaTransactionManager. * <p>Note: Will return {@code null} if no thread-bound EntityManager found! * @param emf the EntityManagerFactory to create the EntityManager with * @return the EntityManager, or {@code null} if none found * @throws DataAccessResourceFailureException if the EntityManager couldn't be obtained * @see JpaTransactionManager */ @Nullable public static EntityManager getTransactionalEntityManager(EntityManagerFactory emf) throws DataAccessResourceFailureException { return getTransactionalEntityManager(emf, null); }
private void closeEntityManager() { if (this.timeoutInProgress || this.errorInProgress) { logger.debug("Closing JPA EntityManager after async request timeout/error"); EntityManagerFactoryUtils.closeEntityManager(this.emHolder.getEntityManager()); } }
EntityManager target = EntityManagerFactoryUtils.doGetTransactionalEntityManager( this.targetFactory, this.properties, this.synchronizedWithTransaction); EntityManagerFactoryUtils.applyTransactionTimeout(query, this.targetFactory); EntityManagerFactoryUtils.closeEntityManager(target);
/** * Retrieves an EntityManagerFactory by persistence unit name, if none set explicitly. * Falls back to a default EntityManagerFactory bean if no persistence unit specified. * @see #setPersistenceUnitName */ @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { if (getEntityManagerFactory() == null) { if (!(beanFactory instanceof ListableBeanFactory)) { throw new IllegalStateException("Cannot retrieve EntityManagerFactory by persistence unit name " + "in a non-listable BeanFactory: " + beanFactory); } ListableBeanFactory lbf = (ListableBeanFactory) beanFactory; setEntityManagerFactory(EntityManagerFactoryUtils.findEntityManagerFactory(lbf, getPersistenceUnitName())); } }
/** * This implementation delegates to EntityManagerFactoryUtils. * @see EntityManagerFactoryUtils#convertJpaAccessExceptionIfPossible */ @Override @Nullable public DataAccessException translateExceptionIfPossible(RuntimeException ex) { return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex); }
/** * Obtain a JPA EntityManager from the given factory. Is aware of a corresponding * EntityManager bound to the current thread, e.g. when using JpaTransactionManager. * <p>Same as {@code getEntityManager}, but throwing the original PersistenceException. * @param emf the EntityManagerFactory to create the EntityManager with * @param properties the properties to be passed into the {@code createEntityManager} * call (may be {@code null}) * @return the EntityManager, or {@code null} if none found * @throws javax.persistence.PersistenceException if the EntityManager couldn't be created * @see #getTransactionalEntityManager(javax.persistence.EntityManagerFactory) * @see JpaTransactionManager */ @Nullable public static EntityManager doGetTransactionalEntityManager(EntityManagerFactory emf, Map<?, ?> properties) throws PersistenceException { return doGetTransactionalEntityManager(emf, properties, true); }
/** * Obtain the transactional EntityManager for this accessor's EntityManagerFactory, if any. * @return the transactional EntityManager, or {@code null} if none * @throws IllegalStateException if this accessor is not configured with an EntityManagerFactory * @see EntityManagerFactoryUtils#getTransactionalEntityManager(javax.persistence.EntityManagerFactory) * @see EntityManagerFactoryUtils#getTransactionalEntityManager(javax.persistence.EntityManagerFactory, java.util.Map) */ @Nullable protected EntityManager getTransactionalEntityManager() throws IllegalStateException{ EntityManagerFactory emf = obtainEntityManagerFactory(); return EntityManagerFactoryUtils.getTransactionalEntityManager(emf, getJpaPropertyMap()); }
@Override protected void releaseResource(EntityManagerHolder resourceHolder, EntityManagerFactory resourceKey) { closeEntityManager(resourceHolder.getEntityManager()); } }