INT-3322 JDBC i-c-a SpEL Select ParameterSource

JIRA: https://jira.spring.io/browse/INT-3322

Support the use of an ExpressionEvaluatingSqlParameterSource
from the ExpressionEvaluatingSqlParameterSourceFactory as the
`select-sql-parameter-source` for an inbound channel adapter.

Add documentation showing how to construct the bean.

Add a mechanism to disable caching so that the expression is
re-evaluated each time. However, the value should still be
cached between the `hasValue()` and `getValue()` calls.

INT-3322 Polishing; PR Comments
This commit is contained in:
Gary russell
2014-03-13 16:09:19 +02:00
committed by Artem Bilan
parent 98b46c61f3
commit c73cc40e23
5 changed files with 155 additions and 12 deletions

View File

@@ -74,12 +74,65 @@
channel="target" data-source="dataSource"
update="update item set status=10 where id in (:id)" />]]></programlisting>
<note>
The parameters in the update query are specified with a colon (:) prefix to the name of a parameter (which in this case is an expression to be applied to each of the rows in the polled result set). This is a standard feature of the named parameter JDBC support in Spring JDBC combined with a convention (projection onto the polled result list) adopted in Spring Integration. The underlying Spring JDBC features limit the available expressions (e.g. most special characters other than period are disallowed), but since the target is usually a list of or an individual object addressable by simple bean paths this isn't unduly restrictive.
</note> To change the parameter generation strategy you can inject a
The parameters in the update query are specified with a colon (:)
prefix to the name of a parameter (which in this case is an expression
to be applied to each of the rows in the polled result set).
This is a standard feature of the named parameter JDBC support in Spring JDBC
combined with a convention (projection onto the polled result list)
adopted in Spring Integration.
The underlying Spring JDBC features limit the available expressions
(e.g. most special characters other than period are disallowed),
but since the target is usually a list of or an individual object addressable by simple bean paths this isn't unduly restrictive.
</note>
To change the parameter generation strategy you can inject a
<classname>SqlParameterSourceFactory</classname> into the adapter to
override the default behavior (the adapter has a
<code>sql-parameter-source-factory</code> attribute).</para>
<code>sql-parameter-source-factory</code> attribute). Spring Integration
provides a <classname>ExpressionEvaluatingSqlParameterSourceFactory</classname> which
will create a SpEL-based parameter source, with the results of the query as the
<code>#root</code> object. (If <code>update-per-row</code> is true, the root object
is the row). If the same parameter name appears multiple times in the update query, it
is evaluated only one time, and its result is cached.
</para>
<para>
You can also use a parameter source for the select query. In this case, since there is no "result"
object to evaluate against, a single parameter source is used each time (rather than using a
parameter source factory). Starting with <emphasis>version 4.0</emphasis>, you can use Spring
to create a SpEL based parameter source as follows:
</para>
<programlisting language="xml"><![CDATA[<int-jdbc:inbound-channel-adapter query="select * from item where status=:status"
channel="target" data-source="dataSource"
select-sql-parameter-source="parameterSource" />
<bean id="parameterSource" factory-bean="parameterSourceFactory"
factory-method="createParameterSourceNoCache">
<constructor-arg value="" />
</bean>
<bean id="parameterSourceFactory"
class="o.s.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
<property name="parameterExpressions">
<map>
<entry key="status" value="@statusBean.which()" />
</map>
</property>
</bean>
<bean id="statusBean" class="foo.StatusDetermination" />]]></programlisting>
<para>
The <code>value</code> in each parameter expression can be any valid SpEL expression.
The <code>#root</code> object for the expression evaluation is the
constructor argument defined on the <code>parameterSource</code> bean. It is static
for all evaluations (in this case, an empty String).
</para>
<important>
Use the <code>createParameterSourceNoCache</code> factory method; otherwise the parameter source will
cache the result of the evaluation. Also note that, because caching is disabled, if the same
parameter name appears in the select query multiple times, it will be re-evaluated for each
occurrence.
</important>
<section>
<title>Polling and Transactions</title>
@@ -355,7 +408,7 @@
For more information, please see:
</para>
<para>
<ulink url="http://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html"></ulink>
<ulink url="http://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html"/>
</para>
<para>
Also important, please ensure that you use an up-to-date version