How do you handle EmptyResultDataAccessException with Spring Integration?
<p>I have a situation where before I process an input file I want to check if certain information is setup in the database. In this particular case it is a client's name and parameters used for processing. If this information is not setup, the file import shall fail.</p>

<p>In many StackOverflow pages, the users resolve handling EmptyResultDataAccessException exceptions generated by queryForObject returning no rows by catching them in the Java code. </p>

<p>The issue is that Spring Integration is catching the exception well before my code is catching it and in theory, I would not be able to tell this error from any number of EmptyResultDataAccessException exceptions which may be thrown with other queries in the code. </p>

<p>Example code segment showing try...catch with queryForObject:</p>

<pre><code>    MapSqlParameterSource mapParameters = new MapSqlParameterSource();

    // Step 1 check if client exists at all
    mapParameters.addValue("clientname", clientName);
    try {
        clientID = this.namedParameterJdbcTemplate.queryForObject(FIND_BY_NAME, mapParameters, Long.class);
    } catch (EmptyResultDataAccessException e) {
        SQLException sqle = (SQLException) e.getCause();
        logger.debug("No client was found");
        logger.debug(sqle.getMessage());
        return null;
    }

    return clientID;
</code></pre>

<p>In the above code, no row was returned and I want to properly handle it (I have not coded that portion yet). Instead, the catch block is never triggered and instead, my generic error handler and associated error channel is triggered instead. </p>

<p>Segment from file BatchIntegrationConfig.java:</p>

<pre><code>    @Bean
    @ServiceActivator(inputChannel="errorChannel")
    public DefaultErrorHandlingServiceActivator errorLauncher(JobLauncher jobLauncher){
        logger.debug("====&gt; Default Error Handler &lt;====");
        return new DefaultErrorHandlingServiceActivator();
    }
</code></pre>

<p>Segment from file DefaultErrorHandlingServiceActivator.java:</p>

<pre><code>public class DefaultErrorHandlingServiceActivator {
    @ServiceActivator 
    public void handleThrowable(Message&lt;Throwable&gt; errorMessage) throws Throwable {
        // error handling code should go here
    }
}
</code></pre>

<p>Tested Facts:</p>

<ol>
<li>queryForObject expects a row to be returned and will thrown an
exception if otherwise, therefore you have to handle the exception
or use a different query which returns a row.</li>
<li>Spring Integration is monitoring exceptions and catching them before
my own code can hand them.</li>
</ol>

<p>What I want to be able to do:</p>

<ul>
<li>Catch the very specific condition and log it or let the end user know what they need to do to fix the problem. </li>
</ul>

<p>Edit on 10/26/2016 per recommendation from @Artem:</p>

<p>Changed my existing input channel to Spring provided Handler Advice:</p>

<pre><code>@Transformer(inputChannel = "memberInputChannel", outputChannel = "commonJobGateway", adviceChain="handleAdvice")  
</code></pre>

<p>Added support Bean and method for the advice:</p>

<pre><code>    @Bean
ExpressionEvaluatingRequestHandlerAdvice handleAdvice() {
    ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
    advice.setOnFailureExpression("payload");
    advice.setFailureChannel(customErrorChannel());
    advice.setReturnFailureExpressionResult(true);
    advice.setTrapException(true);
    return advice;
}

private QueueChannel customErrorChannel() {
    return new DirectChannel();
}
</code></pre>

<p>I initially had some issues with wiring up this feature, but in the end, I realized that it is creating yet another channel which will need to be monitored for errors and handled appropriately. For simplicity, I have chosen to not use another channel at this time.</p>