Search in sources :

Example 1 with OptimisticLockException

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();
}
Also used : EntityManager(javax.persistence.EntityManager) OptimisticLockException(javax.persistence.OptimisticLockException) OptimisticLockException(javax.persistence.OptimisticLockException) Test(org.junit.Test)

Example 2 with OptimisticLockException

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();
}
Also used : EntityManager(javax.persistence.EntityManager) OptimisticLockException(javax.persistence.OptimisticLockException) Test(org.junit.Test)

Example 3 with OptimisticLockException

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();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) OptimisticLockException(javax.persistence.OptimisticLockException) RollbackException(javax.persistence.RollbackException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with OptimisticLockException

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();
}
Also used : Transaction(org.hibernate.Transaction) OptimisticLockException(javax.persistence.OptimisticLockException) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature)

Example 5 with OptimisticLockException

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);
}
Also used : EntityManager(javax.persistence.EntityManager) OptimisticLockException(javax.persistence.OptimisticLockException) AggregationCompleted(org.nhindirect.monitor.dao.entity.AggregationCompleted) AggregationDAOImpl(org.nhindirect.monitor.dao.impl.AggregationDAOImpl) Test(org.junit.Test)

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