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());
}
}
}
}
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);
}
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);
}
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);
}
Aggregations