【问题标题】:Mule JDBC endpoint causing exception while executing SQL queryMule JDBC 端点在执行 SQL 查询时导致异常
【发布时间】:2016-07-28 09:16:19
【问题描述】:

我在 Mule 中遇到了一个奇怪的问题。我在 Mule 中公开了一个 Web 服务,它执行简单的 CRUD 操作。 现在的问题是有一个 SQL 查询:-

if not exists (select * from sysobjects where name='getData' and xtype='U')create table getData (ID int NOT NULL, NAME varchar(50) NULL,AGE int NULL,DESIGNATION varchar(50) NULL)

此查询的作用是检查数据库中是否存在该表。如果存在,则离开,如果不存在,则创建一个具有相同名称和相同字段的新表..

现在我想在插入数据库操作之前使用这个查询..如果表存在,那么它将离开它并执行插入数据到它..如果它不存在,那么它将创建表首先,然后它将数据插入其中.. 所以我的 Mule Flow 如下:

 <jdbc-ee:connector name="Database_Global" dataSource-ref="DB_Source" validateConnections="true" queryTimeout="-1" pollingFrequency="0" doc:name="Database">
<jdbc-ee:query key="CheckTableExistsQuery" value="if not exists (select * from sysobjects where name='getData' and xtype='U')create table getData (ID int NOT NULL, NAME varchar(50) NULL,AGE int NULL,DESIGNATION varchar(50) NULL)"/>
<jdbc-ee:query key="InsertQuery" value="INSERT INTO getData(ID,NAME,AGE,DESIGNATION)VALUES(#[flowVars['id']],#[flowVars['name']],#[flowVars['age']],#[flowVars['designation']])"/> 
</jdbc-ee:connector>

<flow name="MuleDbInsertFlow1" doc:name="MuleDbInsertFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" path="mainData" doc:name="HTTP"/>
<cxf:jaxws-service service="MainData" serviceClass="com.test.services.schema.maindata.v1.MainData"  doc:name="SOAPWithHeader" />
<component class="com.test.services.schema.maindata.v1.Impl.MainDataImpl" doc:name="JavaMain_ServiceImpl"/>
<mulexml:object-to-xml-transformer doc:name="Object to XML"/>
<choice doc:name="Choice">
  <when expression="#[message.inboundProperties['SOAPAction'] contains 'insertDataOperation']">
    <processor-chain doc:name="Processor Chain">
    <logger message="INSERTDATA" level="INFO" doc:name="Logger"/>
    <jdbc-ee:outbound-endpoint exchange-pattern="request-response" queryKey="CheckTableExistsQuery" queryTimeout="-1" connector-ref="Database_Global" doc:name="Database (JDBC)"/>
    <jdbc-ee:outbound-endpoint exchange-pattern="request-response" queryKey="InsertQuery" queryTimeout="-1" connector-ref="Database_Global" doc:name="Database (JDBC)"/>

//remaining code ......

如您所见..我试图在 InsertQuery 之前调用 CheckTableExistsQuery 以便它检查表是否存在,然后执行插入数据 .. 但我出现以下异常:-

ERROR 2014-09-21 14:03:48,424 [[test].connector.http.mule.default.receiver.02] org.mule.exception.CatchMessagingExceptionStrategy: 
********************************************************************************
Message               : Failed to route event via endpoint: DefaultOutboundEndpoint{endpointUri=jdbc://CheckTableExistsQuery, connector=EEJdbcConnector
{
  name=Database_Global
  lifecycle=start
  this=79fcce6c
  numberOfConcurrentTransactedReceivers=4
  createMultipleTransactedReceivers=false
  connected=true
  supportedProtocols=[jdbc]
  serviceOverrides=<none>
}
,  name='endpoint.jdbc.CheckTableExistsQuery', mep=REQUEST_RESPONSE, properties={queryTimeout=-1}, transactionConfig=Transaction{factory=null, action=INDIFFERENT, timeout=0}, deleteUnacceptedMessages=false, initialState=started, responseTimeout=10000, endpointEncoding=UTF-8, disableTransportTransformer=false}. Message payload is of type: String
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. No SQL Strategy found for SQL statement: {if not exists (select * from sysobjects where name='getData' and xtype='U')create table getData (ID int NOT NULL, NAME varchar(50) NULL,AGE int NULL,DESIGNATION varchar(50) NULL)} (java.lang.IllegalArgumentException)
  com.mulesoft.mule.transport.jdbc.sqlstrategy.EESqlStatementStrategyFactory:105 (null)
2. Failed to route event via endpoint: DefaultOutboundEndpoint{endpointUri=jdbc://CheckTableExistsQuery, connector=EEJdbcConnector
{
  name=Database_Global
  lifecycle=start
  this=79fcce6c
  numberOfConcurrentTransactedReceivers=4
  createMultipleTransactedReceivers=false
  connected=true
  supportedProtocols=[jdbc]
  serviceOverrides=<none>
}
,  name='endpoint.jdbc.CheckTableExistsQuery', mep=REQUEST_RESPONSE, properties={queryTimeout=-1}, transactionConfig=Transaction{factory=null, action=INDIFFERENT, timeout=0}, deleteUnacceptedMessages=false, initialState=started, responseTimeout=10000, endpointEncoding=UTF-8, disableTransportTransformer=false}. Message payload is of type: String (org.mule.api.transport.DispatchException)
  org.mule.transport.AbstractMessageDispatcher:117 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transport/DispatchException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
java.lang.IllegalArgumentException: No SQL Strategy found for SQL statement: {if not exists (select * from sysobjects where name='getData' and xtype='U')create table getData (ID int NOT NULL, NAME varchar(50) NULL,AGE int NULL,DESIGNATION varchar(50) NULL)}
    at com.mulesoft.mule.transport.jdbc.sqlstrategy.EESqlStatementStrategyFactory.create(EESqlStatementStrategyFactory.java:105)
    at org.mule.transport.jdbc.JdbcMessageDispatcher.doSend(JdbcMessageDispatcher.java:65)
    at org.mule.transport.AbstractMessageDispatcher.process(AbstractMessageDispatcher.java:84)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

但奇怪的事实是 .. 如果我使用 Java 代码实现相同的功能,它可以正常工作.. 例如在 Java 代码中,我使用 JDBCTemplate 来执行查询:-

Check table exists and create it */
        String checkTableExists=getQueryByKey("CheckTableExistsQuery"); // Query for check existing table
        jdbcTemplate.execute(checkTableExists); //Create Table If not exists

try {

                String insertDataIntoDB = getQueryByKey("InsertQuery");
                jdbcTemplate.update(insertDataIntoDB, ID, NAME, AGE,
                        DESIGNATION);
                dataResponse.setResponse("Data inserted Successfully");
            } catch (DataIntegrityViolationException e) {
                SQLException sql = (SQLException) e.getCause();
                e.printStackTrace();
                throw sql;
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }

请帮帮我..请告诉我如何执行查询

if not exists (select * from sysobjects where name='getData' and xtype='U')create table getData (ID int NOT NULL, NAME varchar(50) NULL,AGE int NULL,DESIGNATION varchar(50) NULL)

成功...为什么它没有从 Mule JDBC 端点执行,而它从 Java 代码中的 JDBCTemplate 执行

【问题讨论】:

    标签: sql mule mule-studio mssql-jdbc


    【解决方案1】:

    Mule 无法识别 if not exists... 查询,因此不知道如何处理它。

    要解决此问题,您需要:

    • 通过对the default one 进行子类化并添加额外的行为来支持此类查询,创建您自己的org.mule.transport.jdbc.sqlstrategy.SqlStatementStrategyFactory,
    • 弹簧注入到JdbcConnector。

    【讨论】:

      【解决方案2】:

      所以按照 David 的建议,最终在 Mule 流程中的 Java 组件和 Groovy 组件中使用 if not exists 查询,并且为我工作

      【讨论】:

        【解决方案3】:

        我遇到了完全相同的错误。确实,当 Mule 不知道该做什么时,不支持的 SQL 查询甚至缺少 queryKey:

        java.lang.IllegalArgumentException: No SQL Strategy found for SQL statement

        在我的情况下是后者,类路径中缺少我的 test-Suite jdbc:connector,所以我添加了它。

        在您的情况下,尝试如下重写查询。这个对我有用:

        DROP TABLE if exists your_table; 
        CREATE TABLE your_table(...
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-06-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-16
          • 1970-01-01
          • 2013-02-12
          • 2011-06-08
          相关资源
          最近更新 更多