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