Mulesoft:插入 sql 服务器失败 b/c 的空约束,但负载有数据

Mulesoft: Insert into sql server fails b/c of null constraint however payload has data

我还是 Mulesoft's Mule ESB 的新手,我无法解决这个问题。

我无法理解为什么我的 Mule flow 因 sql 服务器插入错误而失败。

负载似乎有数据,但出于某种原因,查询参数未填充该数据。我可以从 table 或文件中正常读取它只是写入 db table 横向移动。

我需要做什么才能将我的负载写入 sql 服务器数据库?

以下是我在 AnyPoint Studio 中 运行 我的 Mule 应用程序时收到的错误消息。出于隐私考虑,我已经从消息中删除了大部分有效负载。

我也发布了我的流程 xml。

如有任何帮助,我们将不胜感激。谢谢

********************************************************************************
Message               : Cannot insert the value NULL into column 'AgentId', table 'GARDB1dev.src.AgentList'; column does not allow nulls. INSERT fails. (com.microsoft.sqlserver.jdbc.SQLServerException).
Payload               : [{Agent_ID=10032, **REDACTED DATA**  [..]]
Payload Type          : java.util.ArrayList
Element               : /Load-AgentList/processors/2/1/1 @ gar-data-load:sharepoint.xml:51
Element XML           : <db:insert config-ref="GAR-DB-Connection-SSPI" doc:name="Insert AgentList Data">
                        <db:parameterized-query>insert into src.AgentList (AgentId,PartyId,PartyType,AgentIdStatus,SalesOrg,AgentIdStatusStart,AgentIdStatusEnd,AgentName,SubOrganization,[Function],NewToOrg,NewToChannel,DaysAgentIdActive,Region,AgentPhone,CertificationSummary)Values (#[payload.Agent_ID],#[payload.Party_ID],#[payload.Party_Type],#[payload.Agent_ID_Status],#[payload.Sales_Organization],#[payload.Agent_ID_Status_Start],#[payload.Agent_ID_Status_End],#[payload.Agent_Name],#[payload.Sub_Organization],#[payload.Function],#[payload.New_To_Org],#[payload.New_To_Channel],#[payload.Days_Agent_ID_Active],#[payload.Region],#[payload.Agent_Phone],#[payload.Certification_Summary])</db:parameterized-query>
                        </db:insert>
--------------------------------------------------------------------------------

我的 Mule Flow 的 XML 描述如下:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:sharepoint2010="http://www.mulesoft.org/schema/mule/sharepoint2010" xmlns:sharepoint="http://www.mulesoft.org/schema/mule/sharepoint" 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/sharepoint http://www.mulesoft.org/schema/mule/sharepoint/current/mule-sharepoint.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/sharepoint2010 http://www.mulesoft.org/schema/mule/sharepoint2010/current/mule-sharepoint2010.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.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/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">

    <file:connector name="input" autoDelete="false" streaming="true" validateConnections="true" doc:name="File" readFromDirectory="C:\Users\rpearso7\_gar-data\input" />

    <flow name="Load-AgentList">
        <file:inbound-endpoint path="C:\Users\rpearso7\_gar-data\input"  responseTimeout="10000" doc:name="File" moveToDirectory="C:\Users\rpearso7\_gar-data\archive" connector-ref="input" moveToPattern="#[function:datestamp]-#[message.inboundProperties['originalFilename']]">
            <file:filename-regex-filter pattern="agent_list\.csv" caseSensitive="false"/>
        </file:inbound-endpoint>
        <dw:transform-message doc:name="Transform Message" metadata:id="fdb1aea0-6e94-425f-9493-ee71983b8eb1">
            <dw:input-payload mimeType="application/csv"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload]]></dw:set-payload>
        </dw:transform-message>
        <logger message="************** Payload Loaded" level="INFO" doc:name="Log Payload"/>
        <scatter-gather doc:name="Scatter-Gather">
            <threading-profile maxThreadsActive="1" poolExhaustedAction="WAIT"/>
            <processor-chain>
                <logger message="***************************** Truncate AgentList Table" level="INFO" doc:name="Logger"/>
                <db:update config-ref="GAR-DB-Connection-SSPI" doc:name="Truncate table">
                    <db:parameterized-query><![CDATA[truncate table src.AgentList;]]></db:parameterized-query>
                </db:update>
                <logger message="************************************ AgentList table truncated" level="INFO" doc:name="Logger"/>
            </processor-chain>
            <processor-chain>
                <logger message="********************************* Start Insert" level="INFO" doc:name="Logger"/>
                <db:insert config-ref="GAR-DB-Connection-SSPI" doc:name="Insert AgentList Data">
                    <db:parameterized-query><![CDATA[insert into src.AgentList (
    AgentId,
    PartyId,
    PartyType,
    AgentIdStatus,
    SalesOrg,
    AgentIdStatusStart,
    AgentIdStatusEnd,
    AgentName,
    SubOrganization,
    [Function],
    NewToOrg,
    NewToChannel,
    DaysAgentIdActive,
    Region,
    AgentPhone,
    CertificationSummary)
Values (
    #[payload.Agent_ID],
    #[payload.Party_ID],
    #[payload.Party_Type],
    #[payload.Agent_ID_Status],
    #[payload.Sales_Organization],
    #[payload.Agent_ID_Status_Start],
    #[payload.Agent_ID_Status_End],
    #[payload.Agent_Name],
    #[payload.Sub_Organization],
    #[payload.Function],
    #[payload.New_To_Org],
    #[payload.New_To_Channel],
    #[payload.Days_Agent_ID_Active],
    #[payload.Region],
    #[payload.Agent_Phone],
    #[payload.Certification_Summary])]]></db:parameterized-query>
                </db:insert>
            </processor-chain>
        </scatter-gather>
        <logger message="**************** Complete" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

我们在类似的场景中遇到过死锁问题,当时另一个进程是 运行 针对同一数据库 table 的 INSERT 语句 table。

场景:

TRUNCATE 当前正在 table 上执行,同时,另一个进程 INSERTS 在同一个 table 数据库中的一些数据。

首先确定 TRUNCATE 的优先级并获取 table 的锁。一旦完成,然后去 INSERT.

您应该在执行 TRUNCATE 之前请求一个 table 锁。

如果你这样做,你就不会出现死锁——table 锁在 INSERT 完成之前不会被授予,一旦你获得另一个锁 INSERT不可能发生。

评论更新:

您可以使用LOCK TABLE命令。

当我在 CSV 文件上使用您的转换时,我已经了解了调试器中发生的情况。

我在第一个记录器处设置了一个断点,这样我就可以停止执行并查看 Mule Expression Language (MEL) 表达式在流程的不同步骤中的行为有何不同。

当我在转换后评估#[payload];我得到一个包含 CSV 文件行的 ArrayList

这似乎是正确的。

但是...由于 scather-gather 只向每个发送副本,因此在 scather-gather 之后调试器中也可以看到相同的有效负载。

有关详细信息,请参阅以下博客 post:Scatter-Gather in Mule ESB

Scatter-Gather is a routing message processor in Mule ESB runtime that sends a request message to multiple targets concurrently. It then collects the responses from all routes and aggregates them back into a single response.

据此,您流程中的数据库 INSERT processing-step 获得 ArrayListArrayList 的元素可以用 null-based 整数索引进行索引。您不能使用 CSV 文件的列名称来使用它们。

首先,您必须 select 来自 ArrayList 的条目,然后您可以在 ArrayList 的条目上使用 CSV 文件中的列 headers,如以下示例所示:

#[payload[0].Agent_id]

此 MEL 表达式 returns 预期的代理 ID。但是如果你尝试执行 #[payload.Agent_id] ;你得到 null.

在我看来,您在 INSERT 中正是这样做的。您没有插入从 CSV 文件创建的 ArrayList 的每个条目,而是尝试在 ArrayList 而不是其元素上访问 CSV 文件的列。

可能的解决方案是添加 foreach scope.

使用它您可以遍历 ArrayList 的条目并将它们中的每一个插入到您的数据库 table.

另请查看简化版流程中不同状态下不同 MEL 表达式的结果:

(请注意,Mule 表达式语言是 case-sensitive。如果 "ID" 子字符串被替换为 "Id",那么我找回 "null"。)

我一直在使用包含以下内容的简单 CSV 文件进行测试:

Agent_ID,Agent_Name
7,james bond
1,Mr. 47
8,Dr. X

您可以考虑类似于以下的解决方案:

我刚刚添加了一个foreach scope

此流程已生成以下 console-output:

INFO  2018-03-14 16:08:37,736 [[testproject01].connector.file.mule.default.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file: X:\SomePathToTheFile\agent_list.csv
INFO  2018-03-14 16:08:38,485 [[testproject01].Load-AgentList.stage1.02] org.mule.api.processor.LoggerMessageProcessor: ************** Payload Loaded
INFO  2018-03-14 16:08:38,488 [[testproject01].ScatterGatherWorkManager.01] org.mule.api.processor.LoggerMessageProcessor: ***************************** Truncate AgentList Table
INFO  2018-03-14 16:08:38,506 [[testproject01].ScatterGatherWorkManager.01] org.mule.api.processor.LoggerMessageProcessor: ********************************* Start Insert
INFO  2018-03-14 16:08:38,506 [[testproject01].ScatterGatherWorkManager.01] org.mule.api.processor.LoggerMessageProcessor: ********************************* Start Insert
INFO  2018-03-14 16:08:38,507 [[testproject01].ScatterGatherWorkManager.01] org.mule.api.processor.LoggerMessageProcessor: ********************************* Start Insert

我希望这就是您要实现的目标。

如果您还有其他问题或者我误解了您的目标,请发表评论。

非常感谢。