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();
}
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);
}
}
}
}
}
Aggregations