Search in sources :

Example 16 with OptimisticLockException

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

the class AggregationDAOImpl_addUpdateAggregationTest method testAddUpdateAggregationTest_optomisticLockException_assertAggregationVersionException.

@Test
public void testAddUpdateAggregationTest_optomisticLockException_assertAggregationVersionException() throws Exception {
    EntityManager mgr = mock(EntityManager.class);
    doThrow(new OptimisticLockException()).when(mgr).persist(any());
    final AggregationDAOImpl dao = new AggregationDAOImpl();
    dao.setEntityManager(mgr);
    boolean exceptionOccured = false;
    try {
        final Aggregation insert = new Aggregation();
        insert.setExchangeBlob(new byte[] { 0, 3, 2 });
        insert.setId("12345");
        insert.setVersion(0);
        dao.addUpdateAggregation(insert);
    } catch (AggregationVersionException e) {
        exceptionOccured = true;
    }
    assertTrue(exceptionOccured);
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationVersionException(org.nhindirect.monitor.dao.AggregationVersionException) EntityManager(javax.persistence.EntityManager) OptimisticLockException(javax.persistence.OptimisticLockException) AggregationDAOImpl(org.nhindirect.monitor.dao.impl.AggregationDAOImpl) Test(org.junit.Test)

Example 17 with OptimisticLockException

use of javax.persistence.OptimisticLockException in project uPortal by Jasig.

the class JpaClusterLockDao method updateLock.

@Override
public void updateLock(final String mutexName) {
    this.executeIgnoreRollback(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            final EntityManager entityManager = getEntityManager();
            final ClusterMutex clusterMutex = getClusterMutex(mutexName);
            validateLockedMutex(clusterMutex);
            clusterMutex.updateLock();
            entityManager.persist(clusterMutex);
            try {
                entityManager.flush();
                logger.trace("Updated {}", clusterMutex);
            } catch (OptimisticLockException e) {
                final IllegalMonitorStateException imse = new IllegalMonitorStateException("Failed to update " + mutexName + " due to another thread/server updating the mutex");
                imse.initCause(e);
                throw imse;
            }
        }
    });
}
Also used : EntityManager(javax.persistence.EntityManager) TransactionStatus(org.springframework.transaction.TransactionStatus) OptimisticLockException(javax.persistence.OptimisticLockException) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 18 with OptimisticLockException

use of javax.persistence.OptimisticLockException in project uPortal by Jasig.

the class JpaClusterLockDao method releaseLock.

@Override
public void releaseLock(final String mutexName) {
    this.executeIgnoreRollback(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            final EntityManager entityManager = getEntityManager();
            final ClusterMutex clusterMutex = getClusterMutex(mutexName);
            validateLockedMutex(clusterMutex);
            clusterMutex.unlock();
            entityManager.persist(clusterMutex);
            try {
                entityManager.flush();
                logger.trace("Released {}", clusterMutex);
            } catch (OptimisticLockException e) {
                final IllegalMonitorStateException imse = new IllegalMonitorStateException("Failed to unlock " + mutexName + " due to another thread/server updating the mutex");
                imse.initCause(e);
                throw imse;
            }
        }
    });
}
Also used : EntityManager(javax.persistence.EntityManager) TransactionStatus(org.springframework.transaction.TransactionStatus) OptimisticLockException(javax.persistence.OptimisticLockException) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

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