Search in sources :

Example 6 with PersistenceException

use of jakarta.persistence.PersistenceException in project spring-framework by spring-projects.

the class OpenEntityManagerInViewFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    EntityManagerFactory emf = lookupEntityManagerFactory(request);
    boolean participate = false;
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    String key = getAlreadyFilteredAttributeName();
    if (TransactionSynchronizationManager.hasResource(emf)) {
        // Do not modify the EntityManager: just set the participate flag.
        participate = true;
    } else {
        boolean isFirstRequest = !isAsyncDispatch(request);
        if (isFirstRequest || !applyEntityManagerBindingInterceptor(asyncManager, key)) {
            logger.debug("Opening JPA EntityManager in OpenEntityManagerInViewFilter");
            try {
                EntityManager em = createEntityManager(emf);
                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);
            }
        }
    }
    try {
        filterChain.doFilter(request, response);
    } finally {
        if (!participate) {
            EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.unbindResource(emf);
            if (!isAsyncStarted(request)) {
                logger.debug("Closing JPA EntityManager in OpenEntityManagerInViewFilter");
                EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
            }
        }
    }
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) EntityManager(jakarta.persistence.EntityManager) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) PersistenceException(jakarta.persistence.PersistenceException) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder)

Example 7 with PersistenceException

use of jakarta.persistence.PersistenceException in project spring-framework by spring-projects.

the class EntityManagerFactoryUtilsTests method testConvertJpaPersistenceException.

/*
	 * Test method for
	 * 'org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessException(PersistenceException)'
	 */
@Test
@SuppressWarnings("serial")
public void testConvertJpaPersistenceException() {
    EntityNotFoundException entityNotFound = new EntityNotFoundException();
    assertThat(EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(entityNotFound).getClass()).isSameAs(JpaObjectRetrievalFailureException.class);
    NoResultException noResult = new NoResultException();
    assertThat(EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(noResult).getClass()).isSameAs(EmptyResultDataAccessException.class);
    NonUniqueResultException nonUniqueResult = new NonUniqueResultException();
    assertThat(EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(nonUniqueResult).getClass()).isSameAs(IncorrectResultSizeDataAccessException.class);
    OptimisticLockException optimisticLock = new OptimisticLockException();
    assertThat(EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(optimisticLock).getClass()).isSameAs(JpaOptimisticLockingFailureException.class);
    EntityExistsException entityExists = new EntityExistsException("foo");
    assertThat(EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(entityExists).getClass()).isSameAs(DataIntegrityViolationException.class);
    TransactionRequiredException transactionRequired = new TransactionRequiredException("foo");
    assertThat(EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(transactionRequired).getClass()).isSameAs(InvalidDataAccessApiUsageException.class);
    PersistenceException unknown = new PersistenceException() {
    };
    assertThat(EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(unknown).getClass()).isSameAs(JpaSystemException.class);
}
Also used : NonUniqueResultException(jakarta.persistence.NonUniqueResultException) TransactionRequiredException(jakarta.persistence.TransactionRequiredException) PersistenceException(jakarta.persistence.PersistenceException) OptimisticLockException(jakarta.persistence.OptimisticLockException) EntityNotFoundException(jakarta.persistence.EntityNotFoundException) NoResultException(jakarta.persistence.NoResultException) EntityExistsException(jakarta.persistence.EntityExistsException) Test(org.junit.jupiter.api.Test)

Example 8 with PersistenceException

use of jakarta.persistence.PersistenceException in project spring-framework by spring-projects.

the class ContainerManagedEntityManagerIntegrationTests method doTestExceptionTranslationWithDialectFound.

protected void doTestExceptionTranslationWithDialectFound(PersistenceExceptionTranslator pet) throws Exception {
    RuntimeException in1 = new RuntimeException("in1");
    PersistenceException in2 = new PersistenceException();
    assertThat(pet.translateExceptionIfPossible(in1)).as("No translation here").isNull();
    DataAccessException dex = pet.translateExceptionIfPossible(in2);
    assertThat(dex).isNotNull();
    assertThat(dex.getCause()).isSameAs(in2);
}
Also used : PersistenceException(jakarta.persistence.PersistenceException) DataAccessException(org.springframework.dao.DataAccessException)

Example 9 with PersistenceException

use of jakarta.persistence.PersistenceException in project spring-framework by spring-projects.

the class LocalContainerEntityManagerFactoryBeanTests method testExceptionTranslationWithNoDialect.

@Test
public void testExceptionTranslationWithNoDialect() throws Exception {
    LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
    cefb.getObject();
    assertThat(cefb.getJpaDialect()).as("No dialect set").isNull();
    RuntimeException in1 = new RuntimeException("in1");
    PersistenceException in2 = new PersistenceException();
    assertThat(cefb.translateExceptionIfPossible(in1)).as("No translation here").isNull();
    DataAccessException dex = cefb.translateExceptionIfPossible(in2);
    assertThat(dex).isNotNull();
    assertThat(dex.getCause()).isSameAs(in2);
}
Also used : PersistenceException(jakarta.persistence.PersistenceException) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.jupiter.api.Test)

Aggregations

PersistenceException (jakarta.persistence.PersistenceException)9 HibernateException (org.hibernate.HibernateException)3 EntityManager (jakarta.persistence.EntityManager)2 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)2 Transaction (org.hibernate.Transaction)2 Test (org.junit.jupiter.api.Test)2 DataAccessException (org.springframework.dao.DataAccessException)2 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)2 EntityManagerHolder (org.springframework.orm.jpa.EntityManagerHolder)2 TransactionSystemException (org.springframework.transaction.TransactionSystemException)2 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)2 EntityExistsException (jakarta.persistence.EntityExistsException)1 EntityNotFoundException (jakarta.persistence.EntityNotFoundException)1 NoResultException (jakarta.persistence.NoResultException)1 NonUniqueResultException (jakarta.persistence.NonUniqueResultException)1 OptimisticLockException (jakarta.persistence.OptimisticLockException)1 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 URL (java.net.URL)1