Search in sources :

Example 1 with OptimisticLockException

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

the class DefaultJpaDialectTests method testTranslateException.

@Test
public void testTranslateException() {
    OptimisticLockException ex = new OptimisticLockException();
    assertThat(dialect.translateExceptionIfPossible(ex).getCause()).isEqualTo(EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex).getCause());
}
Also used : OptimisticLockException(jakarta.persistence.OptimisticLockException) Test(org.junit.jupiter.api.Test)

Example 2 with OptimisticLockException

use of jakarta.persistence.OptimisticLockException 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 3 with OptimisticLockException

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

the class LocalContainerEntityManagerFactoryBeanTests method testApplicationManagedEntityManagerWithTransactionAndCommitException.

@Test
public void testApplicationManagedEntityManagerWithTransactionAndCommitException() throws Exception {
    Object testEntity = new Object();
    EntityTransaction mockTx = mock(EntityTransaction.class);
    willThrow(new OptimisticLockException()).given(mockTx).commit();
    // This one's for the tx (shared)
    EntityManager sharedEm = mock(EntityManager.class);
    given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
    // This is the application-specific one
    EntityManager mockEm = mock(EntityManager.class);
    given(mockEm.getTransaction()).willReturn(mockTx);
    given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
    LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
    JpaTransactionManager jpatm = new JpaTransactionManager();
    jpatm.setEntityManagerFactory(cefb.getObject());
    TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
    EntityManagerFactory emf = cefb.getObject();
    assertThat(cefb.getObject()).as("EntityManagerFactory reference must be cached after init").isSameAs(emf);
    assertThat(emf).as("EMF must be proxied").isNotSameAs(mockEmf);
    EntityManager em = emf.createEntityManager();
    em.joinTransaction();
    assertThat(em.contains(testEntity)).isFalse();
    assertThatExceptionOfType(OptimisticLockingFailureException.class).isThrownBy(() -> jpatm.commit(txStatus));
    cefb.destroy();
    verify(mockTx).begin();
    verify(mockEm).contains(testEntity);
    verify(mockEmf).close();
}
Also used : EntityTransaction(jakarta.persistence.EntityTransaction) EntityManager(jakarta.persistence.EntityManager) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) DefaultTransactionAttribute(org.springframework.transaction.interceptor.DefaultTransactionAttribute) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) OptimisticLockException(jakarta.persistence.OptimisticLockException) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.jupiter.api.Test)

Aggregations

OptimisticLockException (jakarta.persistence.OptimisticLockException)3 Test (org.junit.jupiter.api.Test)3 EntityExistsException (jakarta.persistence.EntityExistsException)1 EntityManager (jakarta.persistence.EntityManager)1 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)1 EntityNotFoundException (jakarta.persistence.EntityNotFoundException)1 EntityTransaction (jakarta.persistence.EntityTransaction)1 NoResultException (jakarta.persistence.NoResultException)1 NonUniqueResultException (jakarta.persistence.NonUniqueResultException)1 PersistenceException (jakarta.persistence.PersistenceException)1 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)1 OptimisticLockingFailureException (org.springframework.dao.OptimisticLockingFailureException)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 DefaultTransactionAttribute (org.springframework.transaction.interceptor.DefaultTransactionAttribute)1