Java Code Examples for org.springframework.orm.jpa.EntityManagerFactoryUtils#closeEntityManager()

The following examples show how to use org.springframework.orm.jpa.EntityManagerFactoryUtils#closeEntityManager() . 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: 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 2
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 3
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 4
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 5
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 6
Source File: SimpleJpaQuery.java    From es with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link SimpleJpaQuery} that encapsulates a simple query string.
 */
SimpleJpaQuery(JpaQueryMethod method, EntityManager em, String queryString) {

    super(method, em);

    this.method = method;
    this.query = new StringQuery(queryString);
    this.countQuery = new StringQuery(method.getCountQuery() == null ? QueryUtils.createCountQueryFor(queryString)
            : method.getCountQuery());

    Parameters parameters = method.getParameters();
    boolean hasPagingOrSortingParameter = parameters.hasPageableParameter() || parameters.hasSortParameter();

    if (method.isNativeQuery() && hasPagingOrSortingParameter) {
        throw new IllegalStateException("Cannot use native queries with dynamic sorting and/or pagination!");
    }

    EntityManager target = null;
    // Try to create a Query object already to fail fast
    if (!method.isNativeQuery()) {
        try {
            target = em.getEntityManagerFactory().createEntityManager();
            target.createQuery(query.getQuery());
        } catch (RuntimeException e) {
            // Needed as there's ambiguities in how an invalid query string shall be expressed by the persistence provider
            // http://java.net/projects/jpa-spec/lists/jsr338-experts/archive/2012-07/message/17
            throw e instanceof IllegalArgumentException ? e : new IllegalArgumentException(e);
        } finally {
            EntityManagerFactoryUtils.closeEntityManager(target);
        }
    }
}
 
Example 7
Source File: PersistenceAnnotationBeanPostProcessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) {
	EntityManager emToClose = this.extendedEntityManagersToClose.remove(bean);
	EntityManagerFactoryUtils.closeEntityManager(emToClose);
}
 
Example 8
Source File: AsyncRequestInterceptor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void closeEntityManager() {
	if (this.timeoutInProgress || this.errorInProgress) {
		logger.debug("Closing JPA EntityManager after async request timeout/error");
		EntityManagerFactoryUtils.closeEntityManager(this.emHolder.getEntityManager());
	}
}
 
Example 9
Source File: PersistenceAnnotationBeanPostProcessor.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) {
	EntityManager emToClose = this.extendedEntityManagersToClose.remove(bean);
	EntityManagerFactoryUtils.closeEntityManager(emToClose);
}
 
Example 10
Source File: AsyncRequestInterceptor.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void closeEntityManager() {
	if (this.timeoutInProgress || this.errorInProgress) {
		logger.debug("Closing JPA EntityManager after async request timeout/error");
		EntityManagerFactoryUtils.closeEntityManager(this.emHolder.getEntityManager());
	}
}
 
Example 11
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 12
Source File: PersistenceAnnotationBeanPostProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
	EntityManager emToClose = this.extendedEntityManagersToClose.remove(bean);
	EntityManagerFactoryUtils.closeEntityManager(emToClose);
}
 
Example 13
Source File: AsyncRequestInterceptor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void closeAfterTimeout() {
	if (this.timeoutInProgress) {
		logger.debug("Closing JPA EntityManager after async request timeout");
		EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
	}
}
 
Example 14
Source File: PersistenceAnnotationBeanPostProcessor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
	EntityManager emToClose = this.extendedEntityManagersToClose.remove(bean);
	EntityManagerFactoryUtils.closeEntityManager(emToClose);
}
 
Example 15
Source File: AsyncRequestInterceptor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private void closeAfterTimeout() {
	if (this.timeoutInProgress) {
		logger.debug("Closing JPA EntityManager after async request timeout");
		EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
	}
}