Mule JDBC端点在执行SQL查询时导致异常

时间:2014-09-21 09:13:25

标签: sql mule mule-studio mssql-jdbc

我在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)

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

现在我想在插入数据库操作之前使用此查询..即如果表存在,那么它将离开它并将执行插入数据...如果它不存在则会创建该表首先然后它将数据插入其中.. 所以我的骡子流程如下:

 <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 ,以便检查表是否存在,然后执行Data的插入..但是我我得到以下例外: -

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执行的

3 个答案:

答案 0 :(得分:1)

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

要解决此问题,您需要:

  • 通过子类the default one创建自己的org.mule.transport.jdbc.sqlstrategy.SqlStatementStrategyFactory并添加额外行为以支持此类查询,
  • 弹簧 - 将其注入JdbcConnector

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

  

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

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

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

typedef struct {
    float* modelMatrix;
} SSBOentityInfo;

typedef struct {
    SSBOentityInfo* entityInfoBuffer;
} SSBOshaderData;