org.springframework.transaction.UnexpectedRollbackException Java Examples

The following examples show how to use org.springframework.transaction.UnexpectedRollbackException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProcessConfigHandlerImpl.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public ProcessChange createProcess(final ConfigurationElement element) throws IllegalAccessException {
  LOGGER.debug("Creating process with id " + element.getEntityId());
  if (processCache.hasKey(element.getEntityId())) {
    throw new ConfigurationException(ConfigurationException.ENTITY_EXISTS, "Attempting to create a process with an already existing id: "
        + element.getEntityId());
  }
  Process process = null;
  try {
    ProcessChange change = processConfigTransacted.doCreateProcess(element);
    process = processCache.get(element.getEntityId());
    jmsContainerManager.subscribe(process);
    processFacade.loadAndStartAliveTag(element.getEntityId());
    processCache.notifyListenersOfUpdate(element.getEntityId());
    return change;
  } catch (RuntimeException ex) {
    LOGGER.error("Exception caught while creating a new Process - rolling back DB changes and removing from cache.");
    processCache.remove(element.getEntityId());
    if (process != null){
      jmsContainerManager.unsubscribe(process);
    }
    throw new UnexpectedRollbackException("Unexpected error while creating a new Process.", ex);
  }

}
 
Example #2
Source File: RuleTagConfigHandlerImpl.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void updateRuleTag(Long id, Properties elementProperties) throws IllegalAccessException {
 try {
  ruleTagConfigTransacted.doUpdateRuleTag(id, elementProperties);
  ruleEvaluator.evaluateRule(id);
  if (log.isTraceEnabled()) {
	  log.trace("updateRuleTag - Notifying Configuration update listeners");
  }
  this.configurationUpdateImpl.notifyListeners(id);
 } catch (UnexpectedRollbackException e) {
  log.error("Rolling back Rule update in cache");
  ruleTagCache.remove(id);
  ruleTagCache.loadFromDb(id);
  throw e;
 }
}
 
Example #3
Source File: SubEquipmentConfigTransactedImpl.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
@Transactional(value = "cacheTransactionManager", propagation = Propagation.REQUIRES_NEW)
public List<ProcessChange> doRemoveSubEquipment(final SubEquipment subEquipment, final ConfigurationElementReport subEquipmentReport) {
  List<ProcessChange> processChanges = new ArrayList<ProcessChange>();

  try {
    subEquipmentDAO.deleteItem(subEquipment.getId());
    Long processId = this.subEquipmentFacade.getProcessIdForAbstractEquipment(subEquipment.getId());

    SubEquipmentUnitRemove subEquipmentUnitRemove = new SubEquipmentUnitRemove(0L, subEquipment.getId(), subEquipment.getParentId());
    processChanges.add(new ProcessChange(processId, subEquipmentUnitRemove));

  } catch (RuntimeException e) {
    subEquipmentReport.setFailure("Rolling back removal of sub-equipment " + subEquipment.getId());
    throw new UnexpectedRollbackException("Exception caught while removing Sub-equipment from DB: rolling back", e);
  }

  return processChanges;
}
 
Example #4
Source File: ProcessConfigTransactedImpl.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void removeEquipmentFromProcess(Long equipmentId, Long processId) {
  try {
    processCache.acquireWriteLockOnKey(processId);
    try {
      Process processCopy = processCache.getCopy(processId);
      log.debug("Removing Process Equipment {} for process {}", equipmentId, processCopy.getName());
      processCopy.getEquipmentIds().remove(equipmentId);
      processCache.putQuiet(processCopy);
    } finally {
      processCache.releaseWriteLockOnKey(processId);
    }
  } catch (RuntimeException e) {
    throw new UnexpectedRollbackException("Unable to remove equipment reference in process.", e);
  }
}
 
Example #5
Source File: DataTagConfigHandlerImpl.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public ProcessChange updateDataTag(Long id, Properties elementProperties) {
 try {
  ProcessChange processChange = dataTagConfigTransacted.doUpdateDataTag(id, elementProperties);
  if (LOGGER.isTraceEnabled()) {
    	LOGGER.trace("createDataTag - Notifying Configuration update listeners");
    }
  this.configurationUpdateImpl.notifyListeners(id);
  return processChange;
 } catch (UnexpectedRollbackException e) {
  LOGGER.error("Rolling back update in cache");
  dataTagCache.remove(id); //DB transaction is rolled back here: reload the tag
  dataTagCache.loadFromDb(id);
  throw e;
 }
}
 
Example #6
Source File: AbstractTransactionAspectTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Simulate failure of the underlying transaction infrastructure to commit.
 * Check that the target method was invoked, but that the transaction
 * infrastructure exception was thrown to the client
 */
@Test
public void cannotCommitTransaction() throws Exception {
	TransactionAttribute txatt = new DefaultTransactionAttribute();

	Method m = setNameMethod;
	MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
	tas.register(m, txatt);
	// Method m2 = getNameMethod;
	// No attributes for m2

	PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);

	TransactionStatus status = mock(TransactionStatus.class);
	given(ptm.getTransaction(txatt)).willReturn(status);
	UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);
	willThrow(ex).given(ptm).commit(status);

	TestBean tb = new TestBean();
	ITestBean itb = (ITestBean) advised(tb, ptm, tas);

	String name = "new name";
	try {
		itb.setName(name);
		fail("Shouldn't have succeeded");
	}
	catch (UnexpectedRollbackException thrown) {
		assertTrue(thrown == ex);
	}

	// Should have invoked target and changed name
	assertTrue(itb.getName() == name);
}
 
Example #7
Source File: ControlTagConfigHandlerImpl.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public ProcessChange updateControlTag(Long id, Properties elementProperties) throws IllegalAccessException {
  acquireEquipmentWriteLockForElement(id, elementProperties);
  try {
    return controlTagConfigTransacted.doUpdateControlTag(id, elementProperties); 
  } catch (UnexpectedRollbackException e) {
    LOGGER.error("Rolling back ControlTag update in cache");
    controlTagCache.remove(id);
    controlTagCache.loadFromDb(id);
    throw e;
  } finally {
    releaseEquipmentWriteLockForElement(id, elementProperties);
  }
}
 
Example #8
Source File: DeviceClassConfigHandlerImpl.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public ProcessChange updateDeviceClass(Long id, Properties elementProperties) {
  try {
    ProcessChange processChange = deviceClassConfigTransacted.doUpdateDeviceClass(id, elementProperties);
    return processChange;

  } catch (UnexpectedRollbackException e) {
    LOGGER.error("Rolling back update in cache");
    // DB transaction is rolled back here: reload the tag
    deviceClassCache.remove(id);
    deviceClassCache.loadFromDb(id);
    throw e;
  }
}
 
Example #9
Source File: AbstractEquipmentConfigTransacted.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates the abstract equipment, puts it in the DB and then loads it into
 * the cache (in that order). The AliveTimer and CommFaultTag are then
 * generated in their respective caches.
 * 
 * @param element
 *          contains the creation detais
 * @return the generated AbstractEquipment object
 * @throws IllegalAccessException
 *           should not be thrown here (inherited at interface from Tag
 *           creation).
 */
protected T createAbstractEquipment(final ConfigurationElement element) throws IllegalAccessException {
  abstractEquipmentCache.acquireWriteLockOnKey(element.getEntityId());
  try {
    LOGGER.debug("Creating (Sub)Equipment " + element.getEntityId());
    T abstractEquipment = commonEquipmentFacade.createCacheObject(element.getEntityId(), element.getElementProperties());
    try {
      configurableDAO.insert(abstractEquipment);
      abstractEquipmentCache.putQuiet(abstractEquipment);
      
      // clear alive and commfault caches and refresh
      // (synch ok as locked equipment so no changes to these ids)
      if (abstractEquipment.getAliveTagId() != null) {
        commonEquipmentFacade.loadAndStartAliveTag(abstractEquipment.getId());
      }
      if (abstractEquipment.getCommFaultTagId() != null) {
        commFaultTagCache.remove(abstractEquipment.getCommFaultTagId());
        commFaultTagCache.loadFromDb(abstractEquipment.getCommFaultTagId());
      }
      
    } catch (Exception e) {
      if (abstractEquipment.getAliveTagId() != null) {
        aliveTimerCache.remove(abstractEquipment.getId());
      }
      if (abstractEquipment.getCommFaultTagId() != null) {
        commFaultTagCache.remove(abstractEquipment.getCommFaultTagId());
      }
      throw new UnexpectedRollbackException("Exception caught while creating equipment: rolling back changes", e);
    }
    // TODO necessary to use DB loading or not?? to check...
    // removed as now rely on automatic cache loading from DB: problem: also
    // used in checking if tag is alive or commfault, so added again
    // abstractEquipmentCache.putQuiet(abstractEquipment);
    // aliveTimerFacade.generateFromEquipment(abstractEquipment);
    // commFaultTagFacade.generateFromEquipment(abstractEquipment);
    return abstractEquipment;
  } finally {
    abstractEquipmentCache.releaseWriteLockOnKey(element.getEntityId());
  }
}
 
Example #10
Source File: TransactionalMethodInterceptor.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
  DataSourceTransactionManager transactionManager =
      transactionManagerProvider.get();

  DefaultTransactionDefinition transactionDefinition =
      new DefaultTransactionDefinition();
  TransactionStatus transaction = transactionManager.getTransaction(
      transactionDefinition);

  try {
    Object result = invocation.proceed();

    try {
      if (transaction.isNewTransaction()) {
        transactionManager.commit(transaction);
      }
    } catch (UnexpectedRollbackException ignore) {
    }

    return result;
  } catch (Exception e) {
    if (transaction.isNewTransaction()) {
      transactionManager.rollback(transaction);
    }

    throw e;
  }
}
 
Example #11
Source File: EquipmentConfigTransactedImpl.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
@Transactional(value = "cacheTransactionManager", propagation=Propagation.REQUIRES_NEW)
public void doRemoveEquipment(final Equipment equipment, final ConfigurationElementReport equipmentReport) {
  LOGGER.debug("Removing Equipment " + equipment.getId() + " from DB");
  try {
    equipmentDAO.deleteItem(equipment.getId());                                     
  } catch (UnexpectedRollbackException ex) {
    equipmentReport.setFailure("Aborting removal of equipment " + equipment.getId() + " as unable to remove it from DB."); 
      throw new UnexpectedRollbackException("Interrupting removal of Equipment as failed to remove it from DB - " 
          + "control tags will not be removed.", ex);      
  }              
}
 
Example #12
Source File: AlarmConfigHandlerImpl.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void updateAlarm(Long alarmId, Properties properties) {
  try {
    alarmConfigTransacted.doUpdateAlarm(alarmId, properties);
    alarmFacade.evaluateAlarm(alarmId);
  } catch (UnexpectedRollbackException e) {
    log.error("Rolling back Alarm update in cache");
    alarmCache.remove(alarmId);
    alarmCache.loadFromDb(alarmId);
    throw e;
  }
}
 
Example #13
Source File: AbstractPlatformTransactionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * This implementation of commit handles participating in existing
 * transactions and programmatic rollback requests.
 * Delegates to {@code isRollbackOnly}, {@code doCommit}
 * and {@code rollback}.
 * @see org.springframework.transaction.TransactionStatus#isRollbackOnly()
 * @see #doCommit
 * @see #rollback
 */
@Override
public final void commit(TransactionStatus status) throws TransactionException {
	if (status.isCompleted()) {
		throw new IllegalTransactionStateException(
				"Transaction is already completed - do not call commit or rollback more than once per transaction");
	}

	DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status;
	if (defStatus.isLocalRollbackOnly()) {
		if (defStatus.isDebug()) {
			logger.debug("Transactional code has requested rollback");
		}
		processRollback(defStatus);
		return;
	}
	if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) {
		if (defStatus.isDebug()) {
			logger.debug("Global transaction is marked as rollback-only but transactional code requested commit");
		}
		processRollback(defStatus);
		// Throw UnexpectedRollbackException only at outermost transaction boundary
		// or if explicitly asked to.
		if (status.isNewTransaction() || isFailEarlyOnGlobalRollbackOnly()) {
			throw new UnexpectedRollbackException(
					"Transaction rolled back because it has been marked as rollback-only");
		}
		return;
	}

	processCommit(defStatus);
}
 
Example #14
Source File: SpannerTransactionManagerTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoCommitRollbackExceptions() {

	this.expectedEx.expect(UnexpectedRollbackException.class);
	this.expectedEx.expectMessage("Transaction Got Rolled Back; " +
			"nested exception is com.google.cloud.spanner.AbortedException");

	when(transactionManager.getState()).thenReturn(TransactionState.STARTED);
	Mockito.doThrow(AbortedException.class).when(transactionManager).commit();

	tx.transactionManager = transactionManager;

	manager.doCommit(status);
}
 
Example #15
Source File: AbstractPlatformTransactionManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This implementation of commit handles participating in existing
 * transactions and programmatic rollback requests.
 * Delegates to {@code isRollbackOnly}, {@code doCommit}
 * and {@code rollback}.
 * @see org.springframework.transaction.TransactionStatus#isRollbackOnly()
 * @see #doCommit
 * @see #rollback
 */
@Override
public final void commit(TransactionStatus status) throws TransactionException {
	if (status.isCompleted()) {
		throw new IllegalTransactionStateException(
				"Transaction is already completed - do not call commit or rollback more than once per transaction");
	}

	DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status;
	if (defStatus.isLocalRollbackOnly()) {
		if (defStatus.isDebug()) {
			logger.debug("Transactional code has requested rollback");
		}
		processRollback(defStatus);
		return;
	}
	if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) {
		if (defStatus.isDebug()) {
			logger.debug("Global transaction is marked as rollback-only but transactional code requested commit");
		}
		processRollback(defStatus);
		// Throw UnexpectedRollbackException only at outermost transaction boundary
		// or if explicitly asked to.
		if (status.isNewTransaction() || isFailEarlyOnGlobalRollbackOnly()) {
			throw new UnexpectedRollbackException(
					"Transaction rolled back because it has been marked as rollback-only");
		}
		return;
	}

	processCommit(defStatus);
}
 
Example #16
Source File: AbstractTransactionAspectTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Simulate failure of the underlying transaction infrastructure to commit.
 * Check that the target method was invoked, but that the transaction
 * infrastructure exception was thrown to the client
 */
@Test
public void cannotCommitTransaction() throws Exception {
	TransactionAttribute txatt = new DefaultTransactionAttribute();

	Method m = setNameMethod;
	MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
	tas.register(m, txatt);
	// Method m2 = getNameMethod;
	// No attributes for m2

	PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);

	TransactionStatus status = mock(TransactionStatus.class);
	given(ptm.getTransaction(txatt)).willReturn(status);
	UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);
	willThrow(ex).given(ptm).commit(status);

	TestBean tb = new TestBean();
	ITestBean itb = (ITestBean) advised(tb, ptm, tas);

	String name = "new name";
	try {
		itb.setName(name);
		fail("Shouldn't have succeeded");
	}
	catch (UnexpectedRollbackException thrown) {
		assertTrue(thrown == ex);
	}

	// Should have invoked target and changed name
	assertTrue(itb.getName() == name);
}
 
Example #17
Source File: DeviceConfigHandlerImpl.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public ProcessChange updateDevice(Long id, Properties elementProperties) {
  try {
    ProcessChange processChange = deviceConfigTransacted.doUpdateDevice(id, elementProperties);
    return processChange;

  } catch (UnexpectedRollbackException e) {
    LOGGER.error("Rolling back update in cache");
    // DB transaction is rolled back here: reload the tag
    deviceCache.remove(id);
    deviceCache.loadFromDb(id);
    throw e;
  }
}
 
Example #18
Source File: JmsTransactionManagerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testParticipatingTransactionWithRollbackOnly() throws JMSException {
	ConnectionFactory cf = mock(ConnectionFactory.class);
	Connection con = mock(Connection.class);
	final Session session = mock(Session.class);

	given(cf.createConnection()).willReturn(con);
	given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);

	JmsTransactionManager tm = new JmsTransactionManager(cf);
	TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
	final JmsTemplate jt = new JmsTemplate(cf);
	jt.execute((SessionCallback<Void>) sess -> {
		assertSame(sess, session);
		return null;
	});
	TransactionTemplate tt = new TransactionTemplate(tm);
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			jt.execute((SessionCallback<Void>) sess -> {
				assertSame(sess, session);
				return null;
			});
			status.setRollbackOnly();
		}
	});
	try {
		tm.commit(ts);
		fail("Should have thrown UnexpectedRollbackException");
	}
	catch (UnexpectedRollbackException ex) {
		// expected
	}

	verify(session).rollback();
	verify(session).close();
	verify(con).close();
}
 
Example #19
Source File: AbstractTransactionAspectTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Simulate failure of the underlying transaction infrastructure to commit.
 * Check that the target method was invoked, but that the transaction
 * infrastructure exception was thrown to the client
 */
@Test
public void cannotCommitTransaction() throws Exception {
	TransactionAttribute txatt = new DefaultTransactionAttribute();

	Method m = setNameMethod;
	MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
	tas.register(m, txatt);
	// Method m2 = getNameMethod;
	// No attributes for m2

	PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);

	TransactionStatus status = mock(TransactionStatus.class);
	given(ptm.getTransaction(txatt)).willReturn(status);
	UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);
	willThrow(ex).given(ptm).commit(status);

	TestBean tb = new TestBean();
	ITestBean itb = (ITestBean) advised(tb, ptm, tas);

	String name = "new name";
	try {
		itb.setName(name);
		fail("Shouldn't have succeeded");
	}
	catch (UnexpectedRollbackException thrown) {
		assertTrue(thrown == ex);
	}

	// Should have invoked target and changed name
	assertTrue(itb.getName() == name);
}
 
Example #20
Source File: AbstractReactiveTransactionAspectTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Simulate failure of the underlying transaction infrastructure to commit.
 * Check that the target method was invoked, but that the transaction
 * infrastructure exception was thrown to the client
 */
@Test
public void cannotCommitTransaction() throws Exception {
	TransactionAttribute txatt = new DefaultTransactionAttribute();

	Method m = setNameMethod;
	MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
	tas.register(m, txatt);
	// Method m2 = getNameMethod;
	// No attributes for m2

	ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);

	ReactiveTransaction status = mock(ReactiveTransaction.class);
	given(rtm.getReactiveTransaction(txatt)).willReturn(Mono.just(status));
	UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);
	given(rtm.commit(status)).willReturn(Mono.error(ex));
	given(rtm.rollback(status)).willReturn(Mono.empty());

	DefaultTestBean tb = new DefaultTestBean();
	TestBean itb = (TestBean) advised(tb, rtm, tas);

	String name = "new name";

	Mono.from(itb.setName(name))
			.as(StepVerifier::create)
			.consumeErrorWith(throwable -> {
				assertEquals(RuntimeException.class, throwable.getClass());
				assertEquals(ex, throwable.getCause());
			})
			.verify();

	// Should have invoked target and changed name

	itb.getName()
			.as(StepVerifier::create)
			.expectNext(name)
			.verifyComplete();
}
 
Example #21
Source File: ChainedTransactionManager.java    From easyooo-framework with Apache License 2.0 5 votes vote down vote up
@Override
public void rollback(TransactionStatus status) throws TransactionException {
	if(logger.isWarnEnabled()){
		logger.warn("Rollback all connected transactions.");
	}
	try{
		TransactionSynchronizationUtils.triggerBeforeCompletion();
		Map<DataSource, ConnectionHolder> connSet = RoutingSynchronizationManager.getSynchronizations();
		Exception rollbackException = null;
		ConnectionHolder rollbackExceptionConnection = null;

        for (ConnectionHolder connection:connSet.values()) {
            try {
            	connection.rollback();
            	if (logger.isDebugEnabled()) {
        			logger.debug("Connection["+ connection +"] has been rolled back.");
        		}
            } catch (Exception ex) {
                if (rollbackException == null) {
                    rollbackException = ex;
                    rollbackExceptionConnection = connection;
                } else {
                    logger.warn("Rollback exception (" + rollbackExceptionConnection + ") " + ex.getMessage(), ex);
                }
            }
        }
        
        if (rollbackException != null) {
            throw new UnexpectedRollbackException("Rollback exception, originated at ("+rollbackExceptionConnection+") "+
              rollbackException.getMessage(), rollbackException);
        }
	}finally{
		RoutingSynchronizationManager
				.invokeAfterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
		RoutingSynchronizationManager.clearSynchronization();
	}
}
 
Example #22
Source File: JpaPersistenceProviderTest.java    From rice with Educational Community License v2.0 5 votes vote down vote up
@Test(expected=UnexpectedRollbackException.class)
public void testSaveUnlinkedSkipLinking() {
    Object a = createUnlinkedTestObject();

    provider.save(a);

    fail("save should have resulted in an exception as references have not been linked correctly");
}
 
Example #23
Source File: ControlTagConfigTransactedImpl.java    From c2mon with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
@Transactional(value = "cacheTransactionManager", propagation=Propagation.REQUIRES_NEW)
public ProcessChange doRemoveControlTag(Long id, ConfigurationElementReport tagReport) {
  LOGGER.trace("Removing ControlTag " + id);
  try {      
    Collection<Long> ruleIds = tagCache.get(id).getCopyRuleIds();
    if (!ruleIds.isEmpty()) {
      LOGGER.trace("Removing rules dependent on ControlTag " + id);
      for (Long ruleId : ruleIds) {
        ConfigurationElementReport newReport = new ConfigurationElementReport(Action.REMOVE, Entity.RULETAG, ruleId);
        tagReport.addSubReport(newReport);
        ruleTagConfigHandler.removeRuleTag(ruleId, newReport);
      }       
    }
    tagCache.acquireWriteLockOnKey(id);      
    try {                
      ControlTag controlTag = tagCache.get(id);
      if (!controlTag.getAlarmIds().isEmpty()) {
        LOGGER.trace("Removing Alarms dependent on ControlTag " + controlTag.getId());
        for (Long alarmId : new ArrayList<Long>(controlTag.getAlarmIds())) {
          ConfigurationElementReport alarmReport = new ConfigurationElementReport(Action.REMOVE, Entity.ALARM, alarmId);
          tagReport.addSubReport(alarmReport);
          alarmConfigHandler.removeAlarm(alarmId, alarmReport);
        } 
      }

      for (ConfigurationEventListener listener : configurationEventListeners) {
        listener.onConfigurationEvent(controlTag, Action.REMOVE);
      }

      //dataTagFacade.invalidate(controlTag, new DataTagQuality(DataTagQuality.REMOVED, "The ControlTag has been removed from the system and is no longer monitored."), new Timestamp(System.currentTimeMillis()));
      configurableDAO.deleteItem(controlTag.getId());        
      //if the ControlTag has no Address, do not send anything to the DAQ so return null
      if (((ControlTagFacade) commonTagFacade).isInProcessList(controlTag)) {
        tagCache.releaseWriteLockOnKey(id);
        //if the ControlTag is associated to some Equipment(or SubEquipment) inform the DAQ   
        DataTagRemove removeEvent = new DataTagRemove();
        removeEvent.setDataTagId(id);
        return getProcessChanges(removeEvent, id);        
      } else {
        return new ProcessChange();     
      }
    } catch (Exception ex) {
      //commonTagFacade.setStatus(controlTag, Status.RECONFIGURATION_ERROR);
      LOGGER.error("Exception caught while removing a control tag.", ex);
      tagReport.setFailure("Unable to remove ControlTag with id " + id); 
      throw new UnexpectedRollbackException("Unable to remove control tag " + id, ex);
    } finally {
      if (tagCache.isWriteLockedByCurrentThread(id)) {
        tagCache.releaseWriteLockOnKey(id);
      }      
    } 
  } catch (CacheElementNotFoundException e) {
    LOGGER.warn("Attempting to remove a non-existent ControlTag - no action taken.");
    tagReport.setWarning("Attempting to removed a non-existent ControlTag");
    return new ProcessChange();
  }          
}
 
Example #24
Source File: ProcessConfigHandlerImpl.java    From c2mon with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public ProcessChange updateProcess(final Long processId,
                                  final Properties elementProperties) throws IllegalAccessException {

  if (elementProperties.containsKey("id")) {
    LOGGER.warn("Attempting to change the process id - this is not currently supported!");
    elementProperties.remove("id");
  }

  if (elementProperties.containsKey("name")) {
    LOGGER.warn("Attempting to change the process name - this is not currently supported!");
    elementProperties.remove("name");
  }

  boolean aliveConfigure = false;
  if (elementProperties.containsKey("aliveInterval") || elementProperties.containsKey("aliveTagId")) {
    aliveConfigure = true;
  }

  ProcessChange processChange = new ProcessChange(processId);
  try {
    Long oldAliveId = processCache.getCopy(processId).getAliveTagId();

    processConfigTransacted.doUpdateProcess(processId, elementProperties);

    //stop old, start new - transaction is committed here
    if (aliveConfigure) {
      processFacade.removeAliveDirectly(oldAliveId);
      processFacade.loadAndStartAliveTag(processId);
      processChange.requiresReboot();
    }

  } catch (CacheElementNotFoundException cacheEx) {
    LOGGER.warn("Unable to locate Process " + processId + " in cache so unable to update it.");
    throw cacheEx;
  } catch (RuntimeException e) {
    LOGGER.error("Exception caught while updating Process " + processId + " - rolling back DB and cache changes for this Process.");
    throw new UnexpectedRollbackException("Unexpected exception caught while updating Process " + processId, e);
  }

  return processChange;
}
 
Example #25
Source File: RuleTagConfigTransactedImpl.java    From c2mon with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
@Transactional(value = "cacheTransactionManager", propagation=Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED)
public void doRemoveRuleTag(final Long id, final ConfigurationElementReport elementReport) {
  LOGGER.trace("Removing RuleTag " + id);
  try {
    RuleTag ruleTag = tagCache.get(id);
    Collection<Long> ruleIds = ruleTag.getCopyRuleIds();  
    if (!ruleIds.isEmpty()) {
      LOGGER.debug("Removing rules dependent on RuleTag " + id);
      for (Long ruleId : ruleIds) { //concurrent modifcation as a rule is removed from the list during the remove call!
        if (tagLocationService.isInTagCache(ruleId)) { //may already have been removed if a previous rule in the list was used in this rule!
          ConfigurationElementReport newReport = new ConfigurationElementReport(Action.REMOVE, Entity.RULETAG, ruleId);
          elementReport.addSubReport(newReport);
          ruleTagConfigHandler.removeRuleTag(ruleId, newReport); //call config handler bean so transaction annotation is noticed
        }         
      }                
    }
    tagCache.acquireWriteLockOnKey(id);      
    Collection<Long> ruleInputTagIds = Collections.EMPTY_LIST;
    try {
      ruleInputTagIds = ruleTag.getCopyRuleInputTagIds();                
      Collection<Long> alarmIds = ruleTag.getCopyAlarmIds();                  
      if (!alarmIds.isEmpty()) {
        LOGGER.debug("Removing Alarms dependent on RuleTag " + id);
        for (Long alarmId : alarmIds) { //need copy as modified concurrently by remove alarm
          ConfigurationElementReport alarmReport = new ConfigurationElementReport(Action.REMOVE, Entity.ALARM, alarmId);
          elementReport.addSubReport(alarmReport);
          alarmConfigHandler.removeAlarm(alarmId, alarmReport);
        }        
      }
      for (Long inputTagId : ruleInputTagIds) {
        tagConfigGateway.removeRuleFromTag(inputTagId, id); //allowed to lock tag below the rule...
      }

      for (ConfigurationEventListener listener : configurationEventListeners) {
        listener.onConfigurationEvent(ruleTag, Action.REMOVE);
      }

      configurableDAO.deleteItem(ruleTag.getId());                                           
    }
    catch (RuntimeException rEx) {
      String errMessage = "Exception caught when removing rule tag with id " + id;
      LOGGER.error(errMessage, rEx); 
      throw new UnexpectedRollbackException(errMessage, rEx);   
    } finally {
      if (tagCache.isWriteLockedByCurrentThread(id)) {
        tagCache.releaseWriteLockOnKey(id);
      }        
    }
  } catch (CacheElementNotFoundException e) {
    LOGGER.debug("Attempting to remove a non-existent RuleTag - no action taken.");
    elementReport.setWarning("Attempting to removed a non-existent RuleTag");      
  }       
}
 
Example #26
Source File: DataTagConfigTransactedImpl.java    From c2mon with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
@Transactional(value = "cacheTransactionManager", propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED)
public ProcessChange doRemoveDataTag(final Long id, final ConfigurationElementReport elementReport) {
  ProcessChange processChange = new ProcessChange();
  try {
    DataTag tagCopy = tagCache.getCopy(id);
    Collection<Long> ruleIds = tagCopy.getCopyRuleIds();
    if (!ruleIds.isEmpty()) {
      LOGGER.trace("Removing Rules dependent on DataTag " + id);
      for (Long ruleId : new ArrayList<>(ruleIds)) {
        if (tagLocationService.isInTagCache(ruleId)) { //may already have been removed if a previous rule in the list was used in this rule! {
          ConfigurationElementReport newReport = new ConfigurationElementReport(Action.REMOVE, Entity.RULETAG, ruleId);
          elementReport.addSubReport(newReport);
          ruleTagConfigHandler.removeRuleTag(ruleId, newReport);
        }
      }
    }
    tagCache.acquireWriteLockOnKey(id);
    try {
      Collection<Long> alarmIds = tagCopy.getCopyAlarmIds();
      if (!alarmIds.isEmpty()) {
        LOGGER.trace("Removing Alarms dependent on DataTag " + id);
        for (Long alarmId : new ArrayList<>(alarmIds)) {
          ConfigurationElementReport alarmReport = new ConfigurationElementReport(Action.REMOVE, Entity.ALARM, alarmId);
          elementReport.addSubReport(alarmReport);
          alarmConfigHandler.removeAlarm(alarmId, alarmReport);
        }
      }

      for (ConfigurationEventListener listener : configurationEventListeners) {
        listener.onConfigurationEvent(tagCopy, Action.REMOVE);
      }

      configurableDAO.deleteItem(tagCopy.getId());
    } catch (Exception ex) {
      //commonTagFacade.setStatus(dataTag, Status.RECONFIGURATION_ERROR);
      elementReport.setFailure("Exception caught while removing datatag", ex);
      LOGGER.error("Exception caught while removing datatag with id " + id + "; rolling back DB transaction.", ex);
      throw new UnexpectedRollbackException("Exception caught while removing datatag.", ex);
    } finally {
      if (tagCache.isWriteLockedByCurrentThread(id)) {
        tagCache.releaseWriteLockOnKey(id);
      }
    }

    // if successful so far add remove event for DAQ layer
    DataTagRemove removeEvent = new DataTagRemove();
    removeEvent.setDataTagId(id);

    if (tagCopy.getEquipmentId() != null) {
      removeEvent.setEquipmentId(tagCopy.getEquipmentId());
      processChange = new ProcessChange(equipmentFacade.getProcessIdForAbstractEquipment(tagCopy.getEquipmentId()), removeEvent);
    }
    // TIMS-951: Allow attachment of DataTags to SubEquipments
    else if (tagCopy.getSubEquipmentId() != null) {
      removeEvent.setEquipmentId(subEquipmentFacade.getEquipmentIdForSubEquipment(tagCopy.getSubEquipmentId()));
      processChange = new ProcessChange(subEquipmentFacade.getProcessIdForAbstractEquipment(tagCopy.getSubEquipmentId()), removeEvent);
    }
    else {
      LOGGER.warn("doRemoveDataTag() - data tag #" + tagCopy.getId() + " is not attached to any Equipment or Sub-Equipment. This should normally never happen.");
    }
  } catch (CacheElementNotFoundException e) {
    LOGGER.warn("doRemoveDataTag() - Attempting to remove a non-existent DataTag - no action taken.");
    throw new CacheElementNotFoundException("Attempting to remove a non-existent DataTag - no action taken", e);
  }
  return processChange;
}
 
Example #27
Source File: ProgramaticUserServiceTest.java    From Spring with Apache License 2.0 4 votes vote down vote up
@Test(expected = UnexpectedRollbackException.class)
public void testNotFindById() {
    User user = userService.findById(99L);
    assertNotNull(user);
}
 
Example #28
Source File: AbstractPlatformTransactionManager.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Process an actual rollback.
 * The completed flag has already been checked.
 * @param status object representing the transaction
 * @throws TransactionException in case of rollback failure
 */
private void processRollback(DefaultTransactionStatus status, boolean unexpected) {
	try {
		boolean unexpectedRollback = unexpected;

		try {
			triggerBeforeCompletion(status);

			if (status.hasSavepoint()) {
				if (status.isDebug()) {
					logger.debug("Rolling back transaction to savepoint");
				}
				status.rollbackToHeldSavepoint();
			}
			else if (status.isNewTransaction()) {
				if (status.isDebug()) {
					logger.debug("Initiating transaction rollback");
				}
				doRollback(status);
			}
			else {
				// Participating in larger transaction
				if (status.hasTransaction()) {
					if (status.isLocalRollbackOnly() || isGlobalRollbackOnParticipationFailure()) {
						if (status.isDebug()) {
							logger.debug("Participating transaction failed - marking existing transaction as rollback-only");
						}
						doSetRollbackOnly(status);
					}
					else {
						if (status.isDebug()) {
							logger.debug("Participating transaction failed - letting transaction originator decide on rollback");
						}
					}
				}
				else {
					logger.debug("Should roll back transaction but cannot - no transaction available");
				}
				// Unexpected rollback only matters here if we're asked to fail early
				if (!isFailEarlyOnGlobalRollbackOnly()) {
					unexpectedRollback = false;
				}
			}
		}
		catch (RuntimeException | Error ex) {
			triggerAfterCompletion(status, TransactionSynchronization.STATUS_UNKNOWN);
			throw ex;
		}

		triggerAfterCompletion(status, TransactionSynchronization.STATUS_ROLLED_BACK);

		// Raise UnexpectedRollbackException if we had a global rollback-only marker
		if (unexpectedRollback) {
			throw new UnexpectedRollbackException(
					"Transaction rolled back because it has been marked as rollback-only");
		}
	}
	finally {
		cleanupAfterCompletion(status);
	}
}
 
Example #29
Source File: AbstractPlatformTransactionManager.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Process an actual rollback.
 * The completed flag has already been checked.
 * @param status object representing the transaction
 * @throws TransactionException in case of rollback failure
 */
private void processRollback(DefaultTransactionStatus status, boolean unexpected) {
	try {
		boolean unexpectedRollback = unexpected;

		try {
			triggerBeforeCompletion(status);

			if (status.hasSavepoint()) {
				if (status.isDebug()) {
					logger.debug("Rolling back transaction to savepoint");
				}
				status.rollbackToHeldSavepoint();
			}
			else if (status.isNewTransaction()) {
				if (status.isDebug()) {
					logger.debug("Initiating transaction rollback");
				}
				doRollback(status);
			}
			else {
				// Participating in larger transaction
				if (status.hasTransaction()) {
					if (status.isLocalRollbackOnly() || isGlobalRollbackOnParticipationFailure()) {
						if (status.isDebug()) {
							logger.debug("Participating transaction failed - marking existing transaction as rollback-only");
						}
						doSetRollbackOnly(status);
					}
					else {
						if (status.isDebug()) {
							logger.debug("Participating transaction failed - letting transaction originator decide on rollback");
						}
					}
				}
				else {
					logger.debug("Should roll back transaction but cannot - no transaction available");
				}
				// Unexpected rollback only matters here if we're asked to fail early
				if (!isFailEarlyOnGlobalRollbackOnly()) {
					unexpectedRollback = false;
				}
			}
		}
		catch (RuntimeException | Error ex) {
			triggerAfterCompletion(status, TransactionSynchronization.STATUS_UNKNOWN);
			throw ex;
		}

		triggerAfterCompletion(status, TransactionSynchronization.STATUS_ROLLED_BACK);

		// Raise UnexpectedRollbackException if we had a global rollback-only marker
		if (unexpectedRollback) {
			throw new UnexpectedRollbackException(
					"Transaction rolled back because it has been marked as rollback-only");
		}
	}
	finally {
		cleanupAfterCompletion(status);
	}
}
 
Example #30
Source File: AbstractReactiveTransactionManager.java    From spring-analysis-note with MIT License 2 votes vote down vote up
@Override
public boolean test(Throwable throwable) {
	return throwable instanceof UnexpectedRollbackException;
}