Tuesday, November 3, 2015

Mule cache scope caches session variables

Use of caching scope can result in a message's session variables being replaced with the values in a cached message.

Here a (Mule 3.5.2) sub flow contains a component that calls an injected endpoint:

<sub-flow name="transform_event_data_to_list_of_pojo">
    ...
    <component doc:name="Event JSON to List of POJO">
      <prototype-object class="com.xyz.EventJsonToPojoTransformer">
      </prototype-object>
      <binding interface="com.xyz.DbBindingInterface" method="lookupEventAndCategory">
        <outbound-endpoint ref="eventAndCategoryLookupEndpoint" exchange-pattern="request-response" />
      </binding>
    </component>
    ...
</sub-flow>

The endpoint consumer is a flow that caches database queries:

<flow name="eventLookupFlow">
...
<ee:cache doc:name="Cache lookup result" cachingStrategy-ref="Caching_Strategy_Event_And_Category">
  <db:select config-ref="dbConfig" maxRows="1" transactionalAction="NOT_SUPPORTED">
    <db:parameterized-query><![CDATA[SELECT NAME FROM EVENT_AND_CATEGORY_MAPPING WHERE EVENT_TYPE = #[payload[0]]]]></db:parameterized-query>
  </db:select>
</ee:cache>
...
</flow>

The consequence of this is that when control returns to the transform_event_data_to_list_of_pojo flow, the session variables are changed to the session variables in the cached message.

The solution is to target the result of the cache lookup to a flow variable:

<enricher target="#[flowVars['resultSet']]">
  <ee:cache doc:name="Cache lookup result" cachingStrategy-ref="Caching_Strategy_Event_And_Category">
    <db:select config-ref="dbConfig" maxRows="1" transactionalAction="NOT_SUPPORTED">
      <db:parameterized-query><![CDATA[SELECT NAME FROM EVENT_AND_CATEGORY_MAPPING WHERE EVENT_TYPE = #[payload[0]]]]></db:parameterized-query>
    </db:select>
  </ee:cache>
</enricher>