1

我在 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 执行

4

3 回答 3

1

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

要解决此问题,您需要:

  • 通过将默认org.mule.transport.jdbc.sqlstrategy.SqlStatementStrategyFactory的子类化并添加额外的行为来支持这种类型的查询来创建自己的,
  • 将其注入到JdbcConnector中。
于 2014-09-21T16:21:55.077 回答
0

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

java.lang.IllegalArgumentException:没有为 SQL 语句找到 SQL 策略

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

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

DROP TABLE if exists your_table; 
CREATE TABLE your_table(...
于 2016-07-27T22:33:45.080 回答
0

因此,根据 David 的建议,最终if not exists在 Mule 流程中的 Java 组件和 Groovy 组件中使用查询,并且为我工作

于 2015-07-21T07:02:39.567 回答