将日期参数传递给Mule中的jdbc查询

时间:2012-01-02 13:42:39

标签: sql oracle jdbc mule

我在Mule中有一个流程,我想在一个查询中使用日期参数作为另一个查询的输入。

<jdbc:connector name="myConnector" transactionPerMessage="false" dataSource-ref="myDataSource">
    <jdbc:query key="getPollTimes" value="SELECT to_char(last_poll_start, 'YYYY-MM-DD HH24:MI:SS') as last_poll_start, to_char(last_poll_end, 'YYYY-MM-DD HH24:MI:SS') as last_poll_end FROM db_sources WHERE source_system = 'mySystem'" />
    <jdbc:query key="getCustomerIds" value="SELECT id FROM customers WHERE updated &lt; TO_DATE(#[variable:last_poll_end],'YYYY-MM-DD HH24:MI:SS')" />
</jdbc:connector>

<flow name="myFlow">
    <enricher>
        <jdbc:outbound-endpoint queryKey="getPollTimes" exchange-pattern="request-response" />
        <enrich target="#[variable:last_poll_end]" source="#[groovy:payload.last_poll_end]"/>
    </enricher>
    <logger level="INFO" message="last_poll_end = #[variable:last_poll_end]" />
    <jdbc:outbound-endpoint queryKey="getCustomerIds" exchange-pattern="request-response" />
</flow>

运行时我无法使用它(请注意我使用的是Oracle DB)。我在下面列出了例外情况。有没有人遇到过这个?

--------------------------------------------------------------------------------
Exception stack is:
1. Invalid column type(SQL Code: 17004, SQL State: + null) (java.sql.SQLException)
  oracle.jdbc.driver.DatabaseError:113 (null)
2. Invalid column type Query: SELECT ID FROM CUSTOMERS WHERE UPDATED < TO_DATE(?,'YYYY-MM-DD HH24:MI:SS') Parameters: [[2000-01-01]](SQL Code: 17004, SQL State: + null) (java.sql.SQLException)
  org.apache.commons.dbutils.QueryRunner:540 (null)
3. Failed to route event via endpoint: DefaultOutboundEndpoint{endpointUri=jdbc://getCustomerIds, connector=JdbcConnector
{
  name=myConnector
  lifecycle=start
  this=668e94
  numberOfConcurrentTransactedReceivers=4
  createMultipleTransactedReceivers=false
  connected=true
  supportedProtocols=[jdbc]
  serviceOverrides=<none>
}
,  name='endpoint.jdbc.getCustomerIds', mep=REQUEST_RESPONSE, properties={queryTimeout=-1}, transactionConfig=Transaction{factory=null, action=INDIFFERENT, timeout=0}, deleteUnacceptedMessages=false, initialState=started, responseTimeout=10000, endpointEncoding=UTF-8, disableTransportTransformer=false}. Message payload is of type: ArrayList (org.mule.api.transport.DispatchException)
  org.mule.transport.AbstractMessageDispatcher:106 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transport/DispatchException.html)
--------------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:1)

问题解决了。问题部分在于我从第一个查询返回的日期变量存储为数组。为了解决这个问题,我选择了第一个元素。除此之外,我在第二个sql查询中删除了to_date()。

这将获得数组中的第一个元素:

<enrich target="#[variable:last_poll_end]" source="#[groovy:payload.last_poll_end[0]]"/>

更新的sql:

<jdbc:query key="getCustomerIds" value="SELECT id FROM customers WHERE updated &lt; #[variable:last_poll_end]" />