2014-09-21 52 views
1

我在騾子有一個奇怪的問題。我有一個執行簡單的CRUD操作騾子暴露web服務.. 現在的問題是有一個SQL查詢: -騾子JDBC端點引起的異常而執行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 ...... 

正如你所看到的..我試圖調用CheckTableExistsQueryInsertQuery,以便它檢查表的存在與否,然後進行數據的插入..但我得到以下異常: -

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) 
******************************************************************************** 

但奇怪的事實是,如果我mplement它工作正常一樣使用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)

成功?爲什麼它沒有得到來自騾JDBC端點,而它的從JdbcTemplate的Java代碼得到執行的執行

回答

1

騾子不承認if not exists...查詢,因此不知道該怎麼d o與它。

要解決這個問題,你需要:

  • 創建自己的org.mule.transport.jdbc.sqlstrategy.SqlStatementStrategyFactory由子類the default one和增加額外的行爲來支持這種類型的查詢,
  • 它春注入JdbcConnector
0

所以按照大衛的建議,最終使用在騾子流Java組件和Groovy組件if not exists查詢,併爲我工作

0

我遇到完全相同的錯誤來了。事實上,當騾子不知道該怎麼辦,不支持的SQL查詢甚至未queryKey:

java.lang.IllegalArgumentException異常:未找到SQL語句

對我來說,SQL是戰略是後者,我的test-Suite jdbc:connector從類路徑中丟失了,所以我添加了它。

在你的情況下,嘗試重新查詢如下的查詢。這一個爲我工作:

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