工具 | 版本 |
---|---|
mule-standalone | 3.9.0 |
Anypoint-Studio | 6.4.0 |
寫在前面
mule esb提供了一個Transactional
元素,該元素屬於scope
類型,在當前社區版本下,Anypoint-Studio提供了11個scope類型的元素。事務
的重要性不言而喻,數據庫相信是和事務打交道最多的地方。本例子使用Database
和For Each
元素,配合Transactional
元素,展示mule esb中事務的使用。
例子目標:Oracle數據庫表EMPLOYEES
有三條員工記錄,現在修改這三條記錄的LAST_NAME
字段值爲cgydawn,若是其中一條修改出錯,則三條記錄狀態回滾。(即一起修改LAST_NAME爲cgydawn,或者都不變維持原值,模擬事務的原子性)。
Flow結構圖
For Each
域裏面,先使用循環查詢出 employee_id爲100,101,102的三條記錄,隨之將每次的記錄轉爲json
形式(轉爲json爲了方便使用MEL
表達式判斷),隨後 Vallidation
組件驗證,如果employee_id 爲102,則拋出異常(模擬事務操作中出現錯誤),該整個過程均包含在 Transactional
域中。
XML文檔
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:validation="http://www.mulesoft.org/schema/mule/validation" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/validation http://www.mulesoft.org/schema/mule/validation/current/mule-validation.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<db:oracle-config name="Oracle_Configuration" host="localhost" port="1521" instance="orcl" user="hr" password="hr" doc:name="Oracle Configuration"/>
<flow name="testprojectFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/transactional" doc:name="HTTP"/>
<transactional action="ALWAYS_BEGIN" doc:name="Transactional">
<foreach collection="#[[100,101,102]]" doc:name="For Each">
<db:select config-ref="Oracle_Configuration" doc:name="QUERY_DB">
<db:parameterized-query><![CDATA[select * from employees where employee_id = #[payload]]]></db:parameterized-query>
</db:select>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<set-variable variableName="employeeId" value="#[json:[0]/'EMPLOYEE_ID']" doc:name="GET_RECORD_EMPID"/>
<logger message="#[java.lang.Integer.parseInt(flowVars.employeeId)==102]" level="INFO" doc:name="LOGGER EMPID IS 102?"/>
<validation:is-false expression="#[flowVars.employeeId==104]" doc:name="Validation"/>
<db:update config-ref="Oracle_Configuration" doc:name="Database">
<db:parameterized-query><![CDATA[update employees set last_name='cgydawn' where employee_id=#[json:[0]/'EMPLOYEE_ID']]]></db:parameterized-query>
</db:update>
</foreach>
</transactional>
</flow>
</mule>
運行前數據庫數據
localhost:8081/transactional 運行後控制檯結果
報錯後,數據庫數據沒有改變,可以證明Transactional
按照預期控制了事務,使得100和101記錄行的last_name字段值沒有被修改,若是修改Logger
表達式爲#[java.lang.Integer.parseInt(flowVars.employeeId)==103]
以及修改Validation
條件爲#[flowVars.employeeId==103]
和類型爲Is False
,則不會觸發驗證器拋出異常。
localhost:8081/transactional 重新修改異常觸發條件後運行結果
修改後結果看到三條記錄的 last_name字段均變爲cgydawn,事務控制達到預期。