use of org.apache.hop.pipeline.IExecutionFinishedListener in project hop by apache.
the class LocalPipelineEngine method prepareExecution.
@Override
public void prepareExecution() throws HopException {
if (!(pipelineRunConfiguration.getEngineRunConfiguration() instanceof LocalPipelineRunConfiguration)) {
throw new HopException("A local pipeline execution expects a local pipeline configuration, not an instance of class " + pipelineRunConfiguration.getEngineRunConfiguration().getClass().getName());
}
LocalPipelineRunConfiguration config = (LocalPipelineRunConfiguration) pipelineRunConfiguration.getEngineRunConfiguration();
int sizeRowsSet = Const.toInt(resolve(config.getRowSetSize()), Const.ROWS_IN_ROWSET);
setRowSetSize(sizeRowsSet);
setSafeModeEnabled(config.isSafeModeEnabled());
setSortingTransformsTopologically(config.isSortingTransformsTopologically());
setGatheringMetrics(config.isGatheringMetrics());
setFeedbackShown(config.isFeedbackShown());
setFeedbackSize(Const.toInt(resolve(config.getFeedbackSize()), Const.ROWS_UPDATE));
// See if we need to enable transactions...
//
IExtensionData parentExtensionData = getParentPipeline();
if (parentExtensionData == null) {
parentExtensionData = getParentWorkflow();
}
String connectionGroup = null;
if (parentExtensionData != null) {
connectionGroup = (String) parentExtensionData.getExtensionDataMap().get(Const.CONNECTION_GROUP);
}
//
if (config.isTransactional() && connectionGroup == null) {
// Store a value in the parent...
//
connectionGroup = getPipelineMeta().getName() + " - " + UUID.randomUUID();
// We also need to commit/rollback at the end of this pipeline...
// We only do this when we created a new group. Never in a child
//
addExecutionFinishedListener((IExecutionFinishedListener<IPipelineEngine>) pipeline -> {
String group = (String) pipeline.getExtensionDataMap().get(Const.CONNECTION_GROUP);
List<Database> databases = DatabaseConnectionMap.getInstance().getDatabases(group);
Result result = pipeline.getResult();
for (Database database : databases) {
try {
if (result.getResult() && !result.isStopped() && result.getNrErrors() == 0) {
try {
database.commit(true);
pipeline.getLogChannel().logBasic("All transactions of database connection '" + database.getDatabaseMeta().getName() + "' were committed at the end of the pipeline!");
} catch (HopDatabaseException e) {
throw new HopException("Error committing database connection " + database.getDatabaseMeta().getName(), e);
}
} else {
try {
database.rollback(true);
pipeline.getLogChannel().logBasic("All transactions of database connection '" + database.getDatabaseMeta().getName() + "' were rolled back at the end of the pipeline!");
} catch (HopDatabaseException e) {
throw new HopException("Error rolling back database connection " + database.getDatabaseMeta().getName(), e);
}
}
} finally {
try {
database.closeConnectionOnly();
pipeline.getLogChannel().logDebug("Database connection '" + database.getDatabaseMeta().getName() + "' closed successfully!");
} catch (HopDatabaseException hde) {
pipeline.getLogChannel().logError("Error disconnecting from database - closeConnectionOnly failed:" + Const.CR + hde.getMessage());
pipeline.getLogChannel().logError(Const.getStackTracker(hde));
}
}
DatabaseConnectionMap.getInstance().removeConnection(group, null, database);
}
});
}
//
if (connectionGroup != null && getExtensionDataMap() != null) {
// Set the connection group for this pipeline
//
getExtensionDataMap().put(Const.CONNECTION_GROUP, connectionGroup);
}
super.prepareExecution();
}
Aggregations