Search in sources :

Example 1 with DatabaseTransactionListener

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);
                }
            }
        }
    }
}
Also used : DatabaseConnectionMap(org.pentaho.di.core.database.map.DatabaseConnectionMap) DatabaseTransactionListener(org.pentaho.di.core.database.DatabaseTransactionListener) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) Database(org.pentaho.di.core.database.Database) ArrayList(java.util.ArrayList) UnknownParamException(org.pentaho.di.core.parameters.UnknownParamException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleTransException(org.pentaho.di.core.exception.KettleTransException) DuplicateParamException(org.pentaho.di.core.parameters.DuplicateParamException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException)

Aggregations

UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Database (org.pentaho.di.core.database.Database)1 DatabaseTransactionListener (org.pentaho.di.core.database.DatabaseTransactionListener)1 DatabaseConnectionMap (org.pentaho.di.core.database.map.DatabaseConnectionMap)1 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)1 KettleException (org.pentaho.di.core.exception.KettleException)1 KettleFileException (org.pentaho.di.core.exception.KettleFileException)1 KettleTransException (org.pentaho.di.core.exception.KettleTransException)1 KettleValueException (org.pentaho.di.core.exception.KettleValueException)1 DuplicateParamException (org.pentaho.di.core.parameters.DuplicateParamException)1 UnknownParamException (org.pentaho.di.core.parameters.UnknownParamException)1