Search in sources :

Example 11 with OptimisticLockException

use of javax.persistence.OptimisticLockException in project hibernate-orm by hibernate.

the class ExceptionConverterImpl method wrapLockException.

protected PersistenceException wrapLockException(HibernateException e, LockOptions lockOptions) {
    final PersistenceException pe;
    if (e instanceof OptimisticEntityLockException) {
        final OptimisticEntityLockException lockException = (OptimisticEntityLockException) e;
        pe = new OptimisticLockException(lockException.getMessage(), lockException, lockException.getEntity());
    } else if (e instanceof org.hibernate.exception.LockTimeoutException) {
        pe = new LockTimeoutException(e.getMessage(), e, null);
    } else if (e instanceof PessimisticEntityLockException) {
        final PessimisticEntityLockException lockException = (PessimisticEntityLockException) e;
        if (lockOptions != null && lockOptions.getTimeOut() > -1) {
            // assume lock timeout occurred if a timeout or NO WAIT was specified
            pe = new LockTimeoutException(lockException.getMessage(), lockException, lockException.getEntity());
        } else {
            pe = new PessimisticLockException(lockException.getMessage(), lockException, lockException.getEntity());
        }
    } else if (e instanceof org.hibernate.PessimisticLockException) {
        final org.hibernate.PessimisticLockException jdbcLockException = (org.hibernate.PessimisticLockException) e;
        if (lockOptions != null && lockOptions.getTimeOut() > -1) {
            // assume lock timeout occurred if a timeout or NO WAIT was specified
            pe = new LockTimeoutException(jdbcLockException.getMessage(), jdbcLockException, null);
        } else {
            pe = new PessimisticLockException(jdbcLockException.getMessage(), jdbcLockException, null);
        }
    } else {
        pe = new OptimisticLockException(e);
    }
    return pe;
}
Also used : PessimisticEntityLockException(org.hibernate.dialect.lock.PessimisticEntityLockException) PersistenceException(javax.persistence.PersistenceException) OptimisticLockException(javax.persistence.OptimisticLockException) LockTimeoutException(javax.persistence.LockTimeoutException) OptimisticEntityLockException(org.hibernate.dialect.lock.OptimisticEntityLockException) PessimisticLockException(javax.persistence.PessimisticLockException)

Example 12 with OptimisticLockException

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

the class DefaultJpaDialectTests method testTranslateException.

@Test
public void testTranslateException() {
    OptimisticLockException ex = new OptimisticLockException();
    assertEquals(EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex).getCause(), dialect.translateExceptionIfPossible(ex).getCause());
}
Also used : OptimisticLockException(javax.persistence.OptimisticLockException) Test(org.junit.Test)

Example 13 with OptimisticLockException

use of javax.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();
    assertSame(JpaObjectRetrievalFailureException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(entityNotFound).getClass());
    NoResultException noResult = new NoResultException();
    assertSame(EmptyResultDataAccessException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(noResult).getClass());
    NonUniqueResultException nonUniqueResult = new NonUniqueResultException();
    assertSame(IncorrectResultSizeDataAccessException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(nonUniqueResult).getClass());
    OptimisticLockException optimisticLock = new OptimisticLockException();
    assertSame(JpaOptimisticLockingFailureException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(optimisticLock).getClass());
    EntityExistsException entityExists = new EntityExistsException("foo");
    assertSame(DataIntegrityViolationException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(entityExists).getClass());
    TransactionRequiredException transactionRequired = new TransactionRequiredException("foo");
    assertSame(InvalidDataAccessApiUsageException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(transactionRequired).getClass());
    PersistenceException unknown = new PersistenceException() {
    };
    assertSame(JpaSystemException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(unknown).getClass());
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) TransactionRequiredException(javax.persistence.TransactionRequiredException) PersistenceException(javax.persistence.PersistenceException) OptimisticLockException(javax.persistence.OptimisticLockException) EntityNotFoundException(javax.persistence.EntityNotFoundException) NoResultException(javax.persistence.NoResultException) EntityExistsException(javax.persistence.EntityExistsException) Test(org.junit.Test)

Example 14 with OptimisticLockException

use of javax.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();
    assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());
    assertNotSame("EMF must be proxied", mockEmf, emf);
    EntityManager em = emf.createEntityManager();
    em.joinTransaction();
    assertFalse(em.contains(testEntity));
    try {
        jpatm.commit(txStatus);
        fail("Should have thrown OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException ex) {
    // expected
    }
    cefb.destroy();
    verify(mockTx).begin();
    verify(mockEm).contains(testEntity);
    verify(mockEmf).close();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) DefaultTransactionAttribute(org.springframework.transaction.interceptor.DefaultTransactionAttribute) EntityManagerFactory(javax.persistence.EntityManagerFactory) OptimisticLockException(javax.persistence.OptimisticLockException) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 15 with OptimisticLockException

use of javax.persistence.OptimisticLockException in project nhin-d by DirectProject.

the class AggregationDAOImpl method addUpdateAggregation.

/**
	 * {@inheritDoc}
	 */
@Override
@Transactional(readOnly = false, rollbackFor = { AggregationDAOException.class })
public void addUpdateAggregation(Aggregation aggr) throws AggregationDAOException {
    try {
        // find the aggregation
        final Aggregation existingAggr = this.getAggregation(aggr.getId());
        // if its not there by the requested aggregation has a version > 1, then somethine is wrong
        if (existingAggr == null && aggr.getVersion() > 0)
            throw new AggregationVersionException("Aggregation not found but expected to exist due to non 0 version number");
        if (existingAggr != null) {
            // make sure the version on the existing aggregator matches ours
            if (existingAggr.getVersion() != aggr.getVersion())
                throw new AggregationVersionException("Version number of aggreation does not match what is in the store.");
            // lock the aggregation for update
            entityManager.lock(existingAggr, LockModeType.WRITE);
            existingAggr.setExchangeBlob(aggr.getExchangeBlob());
            entityManager.persist(existingAggr);
        } else {
            // initial add... set the version number to 1
            aggr.setVersion(aggr.getVersion() + 1);
            entityManager.persist(aggr);
        }
        // commit
        entityManager.flush();
    } catch (AggregationDAOException ae) {
        throw ae;
    } catch (OptimisticLockException ol) {
        throw new AggregationVersionException("Aggregation was updated by another thread or process before it could be committed.");
    } catch (Exception e) {
        throw new AggregationDAOException("Failed to add or update aggregation.", e);
    }
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationVersionException(org.nhindirect.monitor.dao.AggregationVersionException) OptimisticLockException(javax.persistence.OptimisticLockException) AggregationDAOException(org.nhindirect.monitor.dao.AggregationDAOException) OptimisticLockException(javax.persistence.OptimisticLockException) AggregationDAOException(org.nhindirect.monitor.dao.AggregationDAOException) AggregationVersionException(org.nhindirect.monitor.dao.AggregationVersionException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

OptimisticLockException (javax.persistence.OptimisticLockException)18 Test (org.junit.Test)11 EntityManager (javax.persistence.EntityManager)9 PersistenceException (javax.persistence.PersistenceException)4 AggregationVersionException (org.nhindirect.monitor.dao.AggregationVersionException)4 EntityNotFoundException (javax.persistence.EntityNotFoundException)3 Session (org.hibernate.Session)3 AggregationDAOException (org.nhindirect.monitor.dao.AggregationDAOException)3 Aggregation (org.nhindirect.monitor.dao.entity.Aggregation)3 AggregationCompleted (org.nhindirect.monitor.dao.entity.AggregationCompleted)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 Transactional (org.springframework.transaction.annotation.Transactional)3 Transaction (org.hibernate.Transaction)2 AggregationDAOImpl (org.nhindirect.monitor.dao.impl.AggregationDAOImpl)2 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)2 Serializable (java.io.Serializable)1 Calendar (java.util.Calendar)1 EntityExistsException (javax.persistence.EntityExistsException)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 EntityTransaction (javax.persistence.EntityTransaction)1