use of org.pentaho.di.core.database.DatabaseTransactionListener in project pentaho-kettle by pentaho.
the class Trans method closeUniqueDatabaseConnections.
/**
* Close unique database connections. If there are errors in the Result, perform a rollback
*
* @param result
* the result of the transformation execution
*/
private void closeUniqueDatabaseConnections(Result result) {
//
if (parentJob != null && transactionId != null && parentJob.getTransactionId() != null && transactionId.equals(parentJob.getTransactionId())) {
return;
}
//
if (parentTrans != null && parentTrans.getTransMeta().isUsingUniqueConnections() && transactionId != null && parentTrans.getTransactionId() != null && transactionId.equals(parentTrans.getTransactionId())) {
return;
}
// First we get all the database connections ...
//
DatabaseConnectionMap map = DatabaseConnectionMap.getInstance();
synchronized (map) {
List<Database> databaseList = new ArrayList<>(map.getMap().values());
for (Database database : databaseList) {
if (database.getConnectionGroup().equals(getTransactionId())) {
try {
//
if (result.getNrErrors() > 0) {
try {
database.rollback(true);
log.logBasic(BaseMessages.getString(PKG, "Trans.Exception.TransactionsRolledBackOnConnection", database.toString()));
} catch (Exception e) {
throw new KettleDatabaseException(BaseMessages.getString(PKG, "Trans.Exception.ErrorRollingBackUniqueConnection", database.toString()), e);
}
} else {
try {
database.commit(true);
log.logBasic(BaseMessages.getString(PKG, "Trans.Exception.TransactionsCommittedOnConnection", database.toString()));
} catch (Exception e) {
throw new KettleDatabaseException(BaseMessages.getString(PKG, "Trans.Exception.ErrorCommittingUniqueConnection", database.toString()), e);
}
}
} catch (Exception e) {
log.logError(BaseMessages.getString(PKG, "Trans.Exception.ErrorHandlingTransformationTransaction", database.toString()), e);
result.setNrErrors(result.getNrErrors() + 1);
} finally {
try {
// This database connection belongs to this transformation.
database.closeConnectionOnly();
} catch (Exception e) {
log.logError(BaseMessages.getString(PKG, "Trans.Exception.ErrorHandlingTransformationTransaction", database.toString()), e);
result.setNrErrors(result.getNrErrors() + 1);
} finally {
// Remove the database from the list...
//
map.removeConnection(database.getConnectionGroup(), database.getPartitionId(), database);
}
}
}
}
// Who else needs to be informed of the rollback or commit?
//
List<DatabaseTransactionListener> transactionListeners = map.getTransactionListeners(getTransactionId());
if (result.getNrErrors() > 0) {
for (DatabaseTransactionListener listener : transactionListeners) {
try {
listener.rollback();
} catch (Exception e) {
log.logError(BaseMessages.getString(PKG, "Trans.Exception.ErrorHandlingTransactionListenerRollback"), e);
result.setNrErrors(result.getNrErrors() + 1);
}
}
} else {
for (DatabaseTransactionListener listener : transactionListeners) {
try {
listener.commit();
} catch (Exception e) {
log.logError(BaseMessages.getString(PKG, "Trans.Exception.ErrorHandlingTransactionListenerCommit"), e);
result.setNrErrors(result.getNrErrors() + 1);
}
}
}
}
}
Aggregations