Dharmin Shah
Dharmin Shah

Reputation: 41

MULE ESB: Saving result from Sql query in Session Variable

I have created a sample mule application that fetches one row from my database. It fetches USER_NAME and USER_ID from the Database. when I convert the result to JSON or XML I get the output as

[{"USER_ID":"U001","USER_NAME":"Dharmin"}]

Now i want to save USER_ID and USER_NAME in Session variables. Can someone guide me ?

edit: updated the basic flow image

http://imgur.com/PzvG5eW

Upvotes: 2

Views: 1785

Answers (2)

Möoz
Möoz

Reputation: 863

Do you need to use the results from the DB as a Json?

If not, don't even bother converting the values to JSON before saving them to the sessionVars. Access them directly from the Payload after the DB call:

enter image description here

enter image description here

enter image description here

And here's the configuration XML:

<flow name="testsFlow">

    <db:select config-ref="ORacle_DBCP_Config" doc:name="inputdata">
        <db:parameterized-query><![CDATA[SELECT 'U001' AS USER_ID, 'Dharmin' AS USER_NAME FROM DUAL]]></db:parameterized-query>
    </db:select>
    <set-session-variable variableName="userName" value="#[payload[0].USER_NAME]" doc:name="userName"/>
    <set-session-variable variableName="userID" value="#[payload[0].USER_ID]" doc:name="userID"/>
    <logger message="#[&quot;UserID: &quot; + sessionVars.userID + &quot; | UserName: &quot; + sessionVars.userName]" level="INFO" doc:name="Output the test"/>
</flow>

The output of the logger is:

processor.LoggerMessageProcessor: UserID: U001 | UserName: Dharmin

Upvotes: 1

Anirban Sen Chowdhary
Anirban Sen Chowdhary

Reputation: 8311

After converting to JSON Add this :-

<json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>

and after that put the value into session variable using the following :-

<set-session-variable doc:name="Session Variable" value="message.payload.USER_ID" variableName="USER_ID"/> 

and

<set-session-variable doc:name="Session Variable" value="message.payload.USER_NAME" variableName="USER_NAME"/>

Upvotes: 1

Related Questions