use of javax.persistence.OptimisticLockException in project hibernate-orm by hibernate.
the class RemoveTest method testUpdatedAndRemove.
@Test
public void testUpdatedAndRemove() throws Exception {
Music music = new Music();
music.setName("Classical");
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist(music);
em.getTransaction().commit();
em.clear();
EntityManager em2 = entityManagerFactory().createEntityManager();
try {
em2.getTransaction().begin();
//read music from 2nd EM
music = em2.find(Music.class, music.getId());
} catch (Exception e) {
em2.getTransaction().rollback();
em2.close();
throw e;
}
//change music
em = getOrCreateEntityManager();
em.getTransaction().begin();
em.find(Music.class, music.getId()).setName("Rap");
em.getTransaction().commit();
try {
//remove changed music
em2.remove(music);
em2.flush();
Assert.fail("should have an optimistic lock exception");
} catch (OptimisticLockException e) {
log.debug("success");
} finally {
em2.getTransaction().rollback();
em2.close();
}
//clean
em.getTransaction().begin();
em.remove(em.find(Music.class, music.getId()));
em.getTransaction().commit();
em.close();
}
use of javax.persistence.OptimisticLockException in project hibernate-orm by hibernate.
the class LockTest method testLockWriteOnUnversioned.
@Test
public void testLockWriteOnUnversioned() throws Exception {
UnversionedLock lock = new UnversionedLock();
lock.setName("second");
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist(lock);
em.getTransaction().commit();
em.getTransaction().begin();
lock = em.getReference(UnversionedLock.class, lock.getId());
try {
// getting a READ (optimistic) lock on unversioned entity is not expected to work.
// To get the same functionality as prior release, change the LockModeType.READ lock to:
// em.lock(lock,LockModeType.PESSIMISTIC_READ);
em.lock(lock, LockModeType.READ);
fail("expected OptimisticLockException exception");
} catch (OptimisticLockException expected) {
}
em.getTransaction().rollback();
// the previous code block can be rewritten as follows (to get the previous behavior)
em.getTransaction().begin();
lock = em.getReference(UnversionedLock.class, lock.getId());
em.lock(lock, LockModeType.PESSIMISTIC_READ);
em.getTransaction().commit();
em.getTransaction().begin();
lock = em.getReference(UnversionedLock.class, lock.getId());
em.remove(lock);
em.getTransaction().commit();
em.close();
}
use of javax.persistence.OptimisticLockException in project hibernate-orm by hibernate.
the class FlushAndTransactionTest method testRollbackExceptionOnOptimisticLockException.
@Test
public void testRollbackExceptionOnOptimisticLockException() throws Exception {
Book book = new Book();
book.name = "Stolen keys";
//new Integer( 50 );
book.id = null;
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist(book);
em.flush();
em.clear();
book.setName("kitty kid");
em.merge(book);
em.flush();
em.clear();
//non updated version
book.setName("kitty kid2");
em.unwrap(Session.class).update(book);
try {
em.getTransaction().commit();
fail("Commit should be rollbacked");
} catch (RollbackException e) {
assertTrue("During flush a StateStateException is wrapped into a OptimisticLockException", e.getCause() instanceof OptimisticLockException);
} finally {
em.close();
}
}
use of javax.persistence.OptimisticLockException in project hibernate-orm by hibernate.
the class BasicHibernateAnnotationsTest method testVersioning.
@Test
@RequiresDialectFeature(DialectChecks.SupportsExpectedLobUsagePattern.class)
@SkipForDialect(value = TeradataDialect.class, comment = "One transaction hangs the other")
public void testVersioning() throws Exception {
Forest forest = new Forest();
forest.setName("Fontainebleau");
forest.setLength(33);
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
s.persist(forest);
tx.commit();
s.close();
Session parallelSession = openSession();
Transaction parallelTx = parallelSession.beginTransaction();
s = openSession();
tx = s.beginTransaction();
forest = (Forest) parallelSession.get(Forest.class, forest.getId());
Forest reloadedForest = (Forest) s.get(Forest.class, forest.getId());
reloadedForest.setLength(11);
assertNotSame(forest, reloadedForest);
tx.commit();
s.close();
forest.setLength(22);
try {
parallelTx.commit();
fail("All optimistic locking should have make it fail");
} catch (OptimisticLockException e) {
if (parallelTx != null)
parallelTx.rollback();
} finally {
parallelSession.close();
}
s = openSession();
tx = s.beginTransaction();
s.delete(s.get(Forest.class, forest.getId()));
tx.commit();
s.close();
}
use of javax.persistence.OptimisticLockException in project nhin-d by DirectProject.
the class AggregationDAOImpl_getAggregationCompletedTest method testGetAggregationCompleted_optomisticLockException_assertAggregationCompletedNotFound.
@Test
public void testGetAggregationCompleted_optomisticLockException_assertAggregationCompletedNotFound() throws Exception {
EntityManager mgr = mock(EntityManager.class);
doThrow(new OptimisticLockException()).when(mgr).find((Class<?>) any(), any());
final AggregationDAOImpl dao = new AggregationDAOImpl();
dao.setEntityManager(mgr);
final AggregationCompleted lockedAggr = dao.getAggregationCompleted("12345", true);
assertNull(lockedAggr);
}
Aggregations