工具 | 版本 |
---|---|
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,事务控制达到预期。