Search in sources :

Example 1 with JdbcStatement

use of org.finra.herd.model.api.xml.JdbcStatement in project herd by FINRAOS.

the class ExecuteJdbc method executeSync.

/**
 * Executes the task synchronously. overrideActivitiId is provided to customize the name of the variable to set during the execution of the task. This is
 * important when the task is running asynchronously because execution.getCurrentActivitiId() will report the ID the task that the workflow is currently in,
 * and not the ID of the task that was originally executed.
 *
 * @param execution The execution.
 * @param overrideActivitiId Optionally overrides the task id which to use to generate variable names.
 * @param jdbcExecutionRequest The request.
 *
 * @throws Exception When any exception occurs during the execution of the task.
 */
private void executeSync(DelegateExecution execution, String overrideActivitiId, JdbcExecutionRequest jdbcExecutionRequest) throws Exception {
    String executionId = execution.getId();
    String activitiId = execution.getCurrentActivityId();
    if (overrideActivitiId != null) {
        activitiId = overrideActivitiId;
    }
    // Execute the request. May throw exception here in case of validation or system errors.
    // Thrown exceptions should be caught by parent implementation.
    JdbcExecutionResponse jdbcExecutionResponse = jdbcService.executeJdbc(jdbcExecutionRequest);
    // Set the response
    setJsonResponseAsWorkflowVariable(jdbcExecutionResponse, executionId, activitiId);
    /*
         * Special handling of error condition.
         * Any one of the requested SQL statements could've failed without throwing an exception.
         * If any of the statements are in ERROR, throw an exception.
         * This ensures that the workflow response variable is still available to the users.
         */
    for (JdbcStatement jdbcStatement : jdbcExecutionResponse.getStatements()) {
        if (JdbcStatementStatus.ERROR.equals(jdbcStatement.getStatus())) {
            throw new IllegalArgumentException("There are failed executions. See JSON response for details.");
        }
    }
}
Also used : JdbcStatement(org.finra.herd.model.api.xml.JdbcStatement) JdbcExecutionResponse(org.finra.herd.model.api.xml.JdbcExecutionResponse)

Example 2 with JdbcStatement

use of org.finra.herd.model.api.xml.JdbcStatement in project herd by FINRAOS.

the class JdbcServiceImpl method executeJdbcImpl.

/**
 * This implementation uses a {@link DriverManagerDataSource}. Uses existing Spring ORM transaction.
 *
 * @param jdbcExecutionRequest JDBC execution request
 *
 * @return {@link JdbcExecutionResponse}
 */
protected JdbcExecutionResponse executeJdbcImpl(JdbcExecutionRequest jdbcExecutionRequest) {
    validateJdbcExecutionRequest(jdbcExecutionRequest);
    // Optionally, get properties from S3
    S3PropertiesLocation s3PropertiesLocation = jdbcExecutionRequest.getS3PropertiesLocation();
    Map<String, Object> variables = getVariablesFromS3(s3PropertiesLocation);
    // Create data source
    DataSource dataSource = createDataSource(jdbcExecutionRequest.getConnection(), variables);
    // Execute the requested statements
    List<JdbcStatement> requestJdbcStatements = jdbcExecutionRequest.getStatements();
    List<JdbcStatement> responseJdbcStatements = executeStatements(requestJdbcStatements, dataSource, variables);
    // Create and return the execution result
    return new JdbcExecutionResponse(null, responseJdbcStatements);
}
Also used : S3PropertiesLocation(org.finra.herd.model.api.xml.S3PropertiesLocation) JdbcStatement(org.finra.herd.model.api.xml.JdbcStatement) JdbcExecutionResponse(org.finra.herd.model.api.xml.JdbcExecutionResponse) DataSource(javax.sql.DataSource) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource)

Example 3 with JdbcStatement

use of org.finra.herd.model.api.xml.JdbcStatement in project herd by FINRAOS.

the class JdbcServiceImpl method createDefaultResponseJdbcStatement.

/**
 * Creates and returns a {@link JdbcStatement} at a state which has not yet been executed based on the given request.
 * <p/>
 * The status will be set to {@link JdbcStatementStatus#SKIPPED} and result null.
 *
 * @param requestJdbcStatement the requested JDBC statement
 *
 * @return a new {@link JdbcStatement}
 */
private JdbcStatement createDefaultResponseJdbcStatement(JdbcStatement requestJdbcStatement) {
    JdbcStatement responseJdbcStatement = new JdbcStatement();
    responseJdbcStatement.setType(requestJdbcStatement.getType());
    responseJdbcStatement.setSql(requestJdbcStatement.getSql());
    responseJdbcStatement.setContinueOnError(requestJdbcStatement.isContinueOnError());
    responseJdbcStatement.setStatus(JdbcStatementStatus.SKIPPED);
    return responseJdbcStatement;
}
Also used : JdbcStatement(org.finra.herd.model.api.xml.JdbcStatement)

Example 4 with JdbcStatement

use of org.finra.herd.model.api.xml.JdbcStatement in project herd by FINRAOS.

the class JdbcServiceImpl method executeStatements.

/**
 * Executes the requested statements in order. Returns the result of the execution.
 *
 * @param requestJdbcStatements the list of statements to execute, in order
 * @param dataSource the data source
 * @param variables the mapping of variables
 *
 * @return List of response {@link JdbcStatement}
 */
private List<JdbcStatement> executeStatements(List<JdbcStatement> requestJdbcStatements, DataSource dataSource, Map<String, Object> variables) {
    List<JdbcStatement> responseJdbcStatements = new ArrayList<>();
    /*
         * Create a copy of all the request statements.
         * The copied statements are the response statements. The response statements are defaulted to SKIPPED.
         */
    for (JdbcStatement requestJdbcStatement : requestJdbcStatements) {
        JdbcStatement responseJdbcStatement = createDefaultResponseJdbcStatement(requestJdbcStatement);
        responseJdbcStatements.add(responseJdbcStatement);
    }
    // We will reuse this template for all executions
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    /*
         * Execute each statement.
         * If there were any errors, and continueOnError is not TRUE, then the execution will stop.
         * The un-executed response statements will remain in their SKIPPED status.
         */
    for (int i = 0; i < responseJdbcStatements.size(); i++) {
        JdbcStatement jdbcStatement = responseJdbcStatements.get(i);
        executeStatement(jdbcTemplate, jdbcStatement, variables, i);
        if (JdbcStatementStatus.ERROR.equals(jdbcStatement.getStatus()) && !Boolean.TRUE.equals(jdbcStatement.isContinueOnError())) {
            break;
        }
    }
    return responseJdbcStatements;
}
Also used : JdbcStatement(org.finra.herd.model.api.xml.JdbcStatement) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 5 with JdbcStatement

use of org.finra.herd.model.api.xml.JdbcStatement in project herd by FINRAOS.

the class JdbcServiceTest method testExecuteJdbcStatementError.

/**
 * Use case where 3 statements are requested to be executed, but the 2nd is erroneous. The first statement should result in SUCCESS. The second should
 * result in ERROR, with appropriate result message. The third should result in SKIPPED with no result.
 */
@Test
public void testExecuteJdbcStatementError() {
    // Create test request
    JdbcExecutionRequest jdbcExecutionRequest = jdbcServiceTestHelper.createDefaultUpdateJdbcExecutionRequest();
    // First statement already included
    // Second statement uses case 2 which throws an error
    jdbcExecutionRequest.getStatements().add(new JdbcStatement(JdbcStatementType.UPDATE, MockJdbcOperations.CASE_2_SQL, null, null, null, null, null));
    jdbcExecutionRequest.getStatements().add(new JdbcStatement(JdbcStatementType.UPDATE, MockJdbcOperations.CASE_1_SQL, null, null, null, null, null));
    // Execute
    JdbcExecutionResponse jdbcExecutionResponse = jdbcService.executeJdbc(jdbcExecutionRequest);
    // Assert results
    Assert.assertNull("JDBC connection is null", jdbcExecutionResponse.getConnection());
    Assert.assertEquals("JDBC statements size", jdbcExecutionRequest.getStatements().size(), jdbcExecutionResponse.getStatements().size());
    {
        JdbcStatement actualJdbcStatement = jdbcExecutionResponse.getStatements().get(0);
        Assert.assertEquals("JDBC statement [0] status", JdbcStatementStatus.SUCCESS, actualJdbcStatement.getStatus());
    }
    {
        JdbcStatement actualJdbcStatement = jdbcExecutionResponse.getStatements().get(1);
        Assert.assertEquals("JDBC statement [1] status", JdbcStatementStatus.ERROR, actualJdbcStatement.getStatus());
        Assert.assertNull("JDBC statement [1] result is not null", actualJdbcStatement.getResult());
        Assert.assertEquals("JDBC statement [1] error message", "java.sql.SQLException: test DataIntegrityViolationException cause", actualJdbcStatement.getErrorMessage());
    }
    {
        JdbcStatement actualJdbcStatement = jdbcExecutionResponse.getStatements().get(2);
        Assert.assertEquals("JDBC statement [2] status", JdbcStatementStatus.SKIPPED, actualJdbcStatement.getStatus());
        Assert.assertNull("JDBC statement [2] result is not null", actualJdbcStatement.getResult());
    }
}
Also used : JdbcStatement(org.finra.herd.model.api.xml.JdbcStatement) JdbcExecutionRequest(org.finra.herd.model.api.xml.JdbcExecutionRequest) JdbcExecutionResponse(org.finra.herd.model.api.xml.JdbcExecutionResponse) Test(org.junit.Test)

Aggregations

JdbcStatement (org.finra.herd.model.api.xml.JdbcStatement)15 JdbcExecutionRequest (org.finra.herd.model.api.xml.JdbcExecutionRequest)10 JdbcExecutionResponse (org.finra.herd.model.api.xml.JdbcExecutionResponse)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)2 JdbcConnection (org.finra.herd.model.api.xml.JdbcConnection)2 S3PropertiesLocation (org.finra.herd.model.api.xml.S3PropertiesLocation)2 HashMap (java.util.HashMap)1 DataSource (javax.sql.DataSource)1 JdbcStatementResultSetRow (org.finra.herd.model.api.xml.JdbcStatementResultSetRow)1 Job (org.finra.herd.model.api.xml.Job)1 JobCreateRequest (org.finra.herd.model.api.xml.JobCreateRequest)1 Parameter (org.finra.herd.model.api.xml.Parameter)1 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)1 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)1 Transactional (org.springframework.transaction.annotation.Transactional)1