Source:
<code>public void AddStoragePoolExistingTasks(storage_pool sp) { List<AsyncTaskCreationInfo> currPoolTasks = null; try { currPoolTasks = (ArrayList<AsyncTaskCreationInfo>) Backend.getInstance().getResourceManager() .RunVdsCommand(VDSCommandType.SPMGetAllTasksInfo, new IrsBaseVDSCommandParameters(sp.getId())) .getReturnValue(); } catch (RuntimeException e) { log.error( String.format( "AsyncTaskManager::AddStoragePoolExistingTasks: Getting existing tasks on Storage Pool %1$s failed.", sp.getname()), e); } if (currPoolTasks != null && currPoolTasks.size() > 0) { synchronized (this) { final List<SPMAsyncTask> newlyAddedTasks = new ArrayList<SPMAsyncTask>(); for (AsyncTaskCreationInfo creationInfo : currPoolTasks) { creationInfo.setStoragePoolID(sp.getId()); if (!_tasks.containsKey(creationInfo.getTaskID())) { try { SPMAsyncTask task = AsyncTaskFactory.Construct(creationInfo); addTaskToManager(task); newlyAddedTasks.add(task); } catch (Exception e) { log.errorFormat("Failed to load task of type {0} with id {1}, due to: {2}.", creationInfo.getTaskType(), creationInfo.getTaskID(), ExceptionUtils.getRootCauseMessage(e)); } } } TransactionSupport.executeInNewTransaction(new TransactionMethod<Void>() { @Override public Void runInTransaction() { for (SPMAsyncTask task : newlyAddedTasks) { <START> AsyncTaskUtils.saveAsyncTaskInDB(task); <END> } return null; } }); for (SPMAsyncTask task : newlyAddedTasks) { StartPollingTask(task.getTaskID()); } log.infoFormat( "AsyncTaskManager::AddStoragePoolExistingTasks: Discovered {0} tasks on Storage Pool '{1}', {2} added to manager.", currPoolTasks.size(), sp.getname(), newlyAddedTasks.size()); } } else { log.infoFormat("AsyncTaskManager::AddStoragePoolExistingTasks: Discovered no tasks on Storage Pool {0}", sp.getname()); } }</code><technical_language>If task in DB, save again? create sql exception? if engine restarted</technical_language>

Target:
public void AddStoragePoolExistingTasks(storage_pool sp) { List<AsyncTaskCreationInfo> currPoolTasks = null; try { currPoolTasks = (ArrayList<AsyncTaskCreationInfo>) Backend.getInstance().getResourceManager() .RunVdsCommand(VDSCommandType.SPMGetAllTasksInfo, new IrsBaseVDSCommandParameters(sp.getId())) .getReturnValue(); } catch (RuntimeException e) { log.error( String.format( "AsyncTaskManager::AddStoragePoolExistingTasks: Getting existing tasks on Storage Pool %1$s failed.", sp.getname()), e); } if (currPoolTasks != null && currPoolTasks.size() > 0) { synchronized (this) { final List<SPMAsyncTask> newlyAddedTasks = new ArrayList<SPMAsyncTask>(); for (AsyncTaskCreationInfo creationInfo : currPoolTasks) { creationInfo.setStoragePoolID(sp.getId()); if (!_tasks.containsKey(creationInfo.getTaskID())) { try { SPMAsyncTask task = AsyncTaskFactory.Construct(creationInfo); addTaskToManager(task); newlyAddedTasks.add(task); } catch (Exception e) { log.errorFormat("Failed to load task of type {0} with id {1}, due to: {2}.", creationInfo.getTaskType(), creationInfo.getTaskID(), ExceptionUtils.getRootCauseMessage(e)); } } } TransactionSupport.executeInNewTransaction(new TransactionMethod<Void>() { @Override public Void runInTransaction() { for (SPMAsyncTask task : newlyAddedTasks) { AsyncTaskUtils.addOrUpdateTaskInDB(task); } return null; } }); for (SPMAsyncTask task : newlyAddedTasks) { StartPollingTask(task.getTaskID()); } log.infoFormat( "AsyncTaskManager::AddStoragePoolExistingTasks: Discovered {0} tasks on Storage Pool '{1}', {2} added to manager.", currPoolTasks.size(), sp.getname(), newlyAddedTasks.size()); } } else { log.infoFormat("AsyncTaskManager::AddStoragePoolExistingTasks: Discovered no tasks on Storage Pool {0}", sp.getname()); } }