Search in sources :

Example 1 with DatabaseConnectionMap

use of org.pentaho.di.core.database.map.DatabaseConnectionMap in project pdi-dataservice-server-plugin by pentaho.

the class TableInputParameterGenerationTest method setUp.

@Before
public void setUp() throws Exception {
    // Setup Mock Step and Data
    data = new TableInputData();
    when(stepInterface.getLogLevel()).thenReturn(LogLevel.NOTHING);
    data.db = new Database(stepInterface, databaseMeta);
    // Add mock connection to connection map, prevent an actual connection attempt
    data.db.setConnection(mock(Connection.class));
    data.db.setConnectionGroup(MOCK_CONNECTION_GROUP);
    data.db.setPartitionId(MOCK_PARTITION_ID);
    when(stepInterface.getStepDataInterface()).thenReturn(data);
    service.dbMeta = databaseMeta;
    when(databaseMeta.quoteField(anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return (String) invocation.getArguments()[0];
        }
    });
    DatabaseConnectionMap connectionMap = DatabaseConnectionMap.getInstance();
    connectionMap.getMap().clear();
    connectionMap.storeDatabase(MOCK_CONNECTION_GROUP, MOCK_PARTITION_ID, data.db);
    setupValueMetaResolverMock();
}
Also used : DatabaseConnectionMap(org.pentaho.di.core.database.map.DatabaseConnectionMap) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Database(org.pentaho.di.core.database.Database) Connection(java.sql.Connection) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) TableInputData(org.pentaho.di.trans.steps.tableinput.TableInputData) Before(org.junit.Before)

Example 2 with DatabaseConnectionMap

use of org.pentaho.di.core.database.map.DatabaseConnectionMap 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

Database (org.pentaho.di.core.database.Database)2 DatabaseConnectionMap (org.pentaho.di.core.database.map.DatabaseConnectionMap)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Connection (java.sql.Connection)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1 JUnitMatchers.containsString (org.junit.matchers.JUnitMatchers.containsString)1 Matchers.anyString (org.mockito.Matchers.anyString)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 DatabaseTransactionListener (org.pentaho.di.core.database.DatabaseTransactionListener)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 TableInputData (org.pentaho.di.trans.steps.tableinput.TableInputData)1