Search in sources :

Example 6 with AggregationDAOException

use of org.nhindirect.monitor.dao.AggregationDAOException in project nhin-d by DirectProject.

the class AggregationDAOImpl method getAggregationCompletedKeys.

/**
	 * {@inheritDoc}
	 */
@SuppressWarnings("unchecked")
@Override
@Transactional(readOnly = true)
public List<String> getAggregationCompletedKeys() throws AggregationDAOException {
    final String query = "SELECT id from AggregationCompleted agg";
    Collection<String> rs;
    List<String> retVal = null;
    ;
    try {
        // get the list using a simple select
        final Query select = entityManager.createQuery(query);
        rs = (Collection<String>) select.getResultList();
        // no keys... return an empty set
        if (rs == null || rs.size() == 0)
            return Collections.emptyList();
        // put the list into a linked list
        retVal = new LinkedList<String>(rs);
    } catch (Exception e) {
        throw new AggregationDAOException("Failed to get aggregation completed keys", e);
    }
    return retVal;
}
Also used : Query(javax.persistence.Query) 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)

Example 7 with AggregationDAOException

use of org.nhindirect.monitor.dao.AggregationDAOException in project nhin-d by DirectProject.

the class AggregationDAOImpl method getAggregationCompleted.

/**
	 * {@inheritDoc}
	 */
@Override
@Transactional(readOnly = false, rollbackFor = { AggregationDAOException.class })
public AggregationCompleted getAggregationCompleted(String id, boolean lock) throws AggregationDAOException {
    validateState();
    try {
        if (!lock) {
            // it's not locked, so just find the completed aggregation and return it
            return entityManager.find(AggregationCompleted.class, id);
        } else {
            /* this is essentially a recovery of a failed completed exchange
				* camel runs this on start up and incremental intervals... it is
				* essential that two application or route instances do not try to play
				* back the same exchange at the same time, or else duplicate exchanges may be
				* routed.... the recovery needs to be locked
				*/
            // first check to see if the recovery is available for locking
            final AggregationCompleted entity = entityManager.find(AggregationCompleted.class, id);
            if (entity == null)
                return null;
            // if the recovery lock time has not passed, then we can't recover
            if (entity.getRecoveryLockedUntilDtTm() != null && entity.getRecoveryLockedUntilDtTm().after(Calendar.getInstance(Locale.getDefault())))
                return null;
            // now lock the row and update lock time and the count
            entityManager.lock(entity, LockModeType.WRITE);
            final Calendar newRecoveryLockTime = Calendar.getInstance(Locale.getDefault());
            newRecoveryLockTime.add(Calendar.SECOND, recoveredEntityLockInterval);
            entity.setRecoveryLockedUntilDtTm(newRecoveryLockTime);
            // persist the time new lock time and increment the update count
            entityManager.persist(entity);
            entityManager.flush();
            return entity;
        }
    } catch (OptimisticLockException ol) {
        // someone else locked the row... return null
        return null;
    } catch (Exception e) {
        throw new AggregationDAOException(e);
    }
}
Also used : Calendar(java.util.Calendar) AggregationCompleted(org.nhindirect.monitor.dao.entity.AggregationCompleted) 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)

Example 8 with AggregationDAOException

use of org.nhindirect.monitor.dao.AggregationDAOException in project nhin-d by DirectProject.

the class AggregationDAOImpl method removeAggregation.

/**
	 * {@inheritDoc}
	 */
@Override
@Transactional(readOnly = false, rollbackFor = { AggregationDAOException.class })
public void removeAggregation(Aggregation agg, String exchangeId) throws AggregationDAOException {
    try {
        // find the aggregation
        final Aggregation existingAgg = this.getAggregation(agg.getId());
        if (existingAgg != null) {
            // check the version number for consistency
            if (existingAgg.getVersion() != agg.getVersion())
                throw new AggregationVersionException("Version number of aggreation does not match what is in the store.");
            // lock for removal
            entityManager.lock(existingAgg, LockModeType.WRITE);
            entityManager.remove(existingAgg);
        } else
            throw new AggregationDAOException("Aggregation does not exist is store.");
        // add to the completed repository
        final AggregationCompleted completed = new AggregationCompleted();
        completed.setExchangeBlob(existingAgg.getExchangeBlob());
        completed.setId(exchangeId);
        completed.setVersion(1);
        entityManager.persist(completed);
        // commit
        entityManager.flush();
    } catch (AggregationDAOException ae) {
        throw ae;
    }///CLOVER:OFF
     catch (OptimisticLockException ol) {
        throw new AggregationVersionException("Aggregation was removed by another thread or process before it could be committed.");
    } catch (Exception e) {
        throw new AggregationDAOException("Failed to remove aggregation.", e);
    }
///CLOVER:ON
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationVersionException(org.nhindirect.monitor.dao.AggregationVersionException) AggregationCompleted(org.nhindirect.monitor.dao.entity.AggregationCompleted) 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)

Example 9 with AggregationDAOException

use of org.nhindirect.monitor.dao.AggregationDAOException in project nhin-d by DirectProject.

the class AggregationDAOImpl_addUpdateAggregationTest method testAddUpdateAggregationTest_entityManagerException_assertNoAggregation.

@Test
public void testAddUpdateAggregationTest_entityManagerException_assertNoAggregation() throws Exception {
    EntityManager mgr = mock(EntityManager.class);
    doThrow(new RuntimeException()).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 (AggregationDAOException e) {
        exceptionOccured = true;
    }
    assertTrue(exceptionOccured);
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) EntityManager(javax.persistence.EntityManager) AggregationDAOException(org.nhindirect.monitor.dao.AggregationDAOException) AggregationDAOImpl(org.nhindirect.monitor.dao.impl.AggregationDAOImpl) Test(org.junit.Test)

Example 10 with AggregationDAOException

use of org.nhindirect.monitor.dao.AggregationDAOException in project nhin-d by DirectProject.

the class AggregationDAOImpl_addUpdateAggregationTest method testAddUpdateAggregationTest_updateIncorrectVersion_assertExceptionAndRollback.

@Test
public void testAddUpdateAggregationTest_updateIncorrectVersion_assertExceptionAndRollback() throws Exception {
    final Aggregation insert = new Aggregation();
    insert.setExchangeBlob(new byte[] { 0, 3, 2 });
    insert.setId("12345");
    insert.setVersion(0);
    notifDao.addUpdateAggregation(insert);
    final Aggregation insert2 = new Aggregation();
    insert2.setExchangeBlob(new byte[] { 0, 3, 2, 4, 1, 32 });
    insert2.setVersion(3);
    insert2.setId("12345");
    boolean exceptionOccured = false;
    try {
        notifDao.addUpdateAggregation(insert2);
    } catch (AggregationDAOException e) {
        exceptionOccured = true;
    }
    assertTrue(exceptionOccured);
    final Aggregation aggr = notifDao.getAggregation("12345");
    assertNotNull(aggr);
    assertEquals(insert, aggr);
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationDAOException(org.nhindirect.monitor.dao.AggregationDAOException) Test(org.junit.Test)

Aggregations

AggregationDAOException (org.nhindirect.monitor.dao.AggregationDAOException)17 Test (org.junit.Test)10 OptimisticLockException (javax.persistence.OptimisticLockException)7 AggregationVersionException (org.nhindirect.monitor.dao.AggregationVersionException)7 Aggregation (org.nhindirect.monitor.dao.entity.Aggregation)7 Transactional (org.springframework.transaction.annotation.Transactional)7 EntityManager (javax.persistence.EntityManager)6 AggregationDAOImpl (org.nhindirect.monitor.dao.impl.AggregationDAOImpl)6 Query (javax.persistence.Query)3 AggregationCompleted (org.nhindirect.monitor.dao.entity.AggregationCompleted)3 Calendar (java.util.Calendar)1