Gaurav
Gaurav

Reputation: 126

two datasource in Spring integration inbound channel

I am using int-jdbc:inbound-channel-adapter of Spring Integration.

My Query is how to use two different datasource i.e. datasource A for querying and datasource B for update , in a single adapter?

Upvotes: 2

Views: 234

Answers (2)

Artem Bilan
Artem Bilan

Reputation: 121177

In Spring JDBC module we have something like AbstractRoutingDataSource. Which you can implement based on some ThreadLocal variable.

From other side the JdbcPollingChannelAdapter has code like this:

private Object poll() {
    List<?> payload = doPoll(this.sqlQueryParameterSource);
    ...
    executeUpdateQuery(payload);

    return payload;
}

So, you should somehow to hook in between doPoll() and executeUpdateQuery and, therefore, change the key in the ThreadLocal to be able to switch to another DataSource in the AbstractRoutingDataSource.

I only the hack like custom sqlParameterSourceFactory.createParameterSource() and the ThreadLocal modification there. Just because the code is like:

private void executeUpdateQuery(Object obj) {
    SqlParameterSource updateParamaterSource = this.sqlParameterSourceFactory.createParameterSource(obj);
    this.jdbcOperations.update(this.updateSql, updateParamaterSource);
}

(Will commit the fix for updateParamaterSource typo soon :-)).

But! As Gary mentioned in his answer, it would be better to have several JDBC adapters for different DataSources : one for SELECT and another for UPDATE. Both of them may work in the same XA Transaction (<transactional> on the <poller>). And from there we really differentiate the business logic and level of responsibility.

Upvotes: 0

Gary Russell
Gary Russell

Reputation: 174494

You cannot; the same JdbcTemplate is used for both operations; you can omit the update query and do the update on an outbound channel adapter.

Upvotes: 2

Related Questions