3

我非常確定空閒連接沒有被重新使用或者我正在泄漏連接。我有一個從文件使用者開始的簡單路線。文件使用者使用文本文件。拿起文件後,我檢查一個表以確保這不是重複的文件。DBCP空閒連接不在駱駝路由中重複使用

然後,我將消息正文從文件轉換爲字符串。然後,我將文件拆分並通過一條路徑運行各個部分,具體取決於它是哪種類型的記錄。這些路由中的每一條最終都會將此記錄插入運行在MySQL上的服務器上的登臺表中。

下面是路線的簡化版本。

<bean id="myPool" class="java.util.concurrent.Executors" factory-method="newFixedThreadPool"> 
     <argument index="0" value="8"/> 
</bean> 
<camelContext trace="false" handleFault="true" errorHandlerRef="redeliveryErrorHandler" id="FileETLProcess" xmlns="http://camel.apache.org/schema/blueprint"> 
<errorHandler type="DefaultErrorHandler" useOriginalMessage="true" id="redeliveryErrorHandler"> 
    <redeliveryPolicy maximumRedeliveries="3" redeliveryDelay="25" backOffMultiplier="2" useExponentialBackOff="false" retryAttemptedLogLevel="INFO"/> 
</errorHandler> 
<onException useOriginalMessage="true"> 
    <exception>java.lang.Exception</exception> 
    <handled> 
     <constant>true</constant> 
    </handled> 
    <log message="STARTING ERROR GENERAL HANDLER"/> 
    <bean ref="GeneralError"/> 
    <to uri="smtp://[email protected]&amp;[email protected]&amp;subject=GENERAL ERROR: A File Could Not Be Imported&amp;contentType=text/html"/> 
    <to uri="file:d:/Inbox/.badFile?fileName=${file:onlyname.noext}_GENERALERROR_${date:now:yyyyMMddHHmmss}.${file:name.ext}"/> 
</onException> 
<route id="ExtractFileRoute"> 
    <from uri="file:d:/Inbox?delay=10000&amp;move=.donebackup/${date:now:yyyyMMdd}/${file:onlyname.noext}_DONE_${date:now:yyyyMMddHHmmss}.${file:name.ext}&amp;readLock=changed&amp;include=.*.dl&amp;maxMessagesPerPoll=0&amp;sortBy=${file:length}"/> 
    <bean ref="FileCheck"/> 
    <choice> 
     <when> 
      <simple>${header.ACCEPTEDFILE} == 'YES'</simple> 
      <log message="File Import Route Started At:${date:now:yyyy-MM-dd HH:mm:ss}"/> 
      <convertBodyTo type="java.lang.String"/> 
      <log message="Converted File To String:${date:now:yyyy-MM-dd HH:mm:ss} handing data to File To DB route."/> 
      <split parallelProcessing="true" executorServiceRef="myPool" streaming="true" shareUnitOfWork="true"> 
       <tokenize token="\n"></tokenize> 
       <setHeader headerName="SPLITFINISHED"> 
        <simple>${property.CamelSplitComplete}</simple> 
       </setHeader> 
       <setHeader headerName="SPLITNUMBER"> 
        <simple>${property.CamelSplitIndex}</simple> 
       </setHeader> 
       <bean ref="EnrichHeader"/> 
       <choice> 
        <when> 
         <simple>${header.RECORDTYPE} == 'HEADER'</simple> 
         <doTry> 
          <unmarshal ref="bindyHeader"/> 
          <bean ref="HeaderPersist"/> 
          <choice> 
           <when> 
            <simple>${property.CamelSplitComplete} == true</simple> 
            <to uri="direct:auxrecordsmove"/> 
           </when> 
          </choice> 
          <doCatch> 
           <exception>java.lang.Exception</exception> 
           <handled> 
            <constant>true</constant> 
           </handled> 
           <bean ref="RecordErrorReport"/> 
           <choice> 
            <when> 
             <simple>${property.CamelSplitComplete} == true</simple> 
             <to uri="direct:auxrecordsmove"/> 
            </when> 
           </choice> 
          </doCatch> 
         </doTry> 
        </when> 
        <when> 
         <simple>${header.RECORDTYPE} == 'A'</simple> 
         <doTry> 
          <unmarshal ref="bindyAccount"/> 
          <bean ref="AccountPersist"/> 
          <choice> 
           <when> 
            <simple>${property.CamelSplitComplete} == true</simple> 
            <to uri="direct:auxrecordsmove"/> 
           </when> 
          </choice> 
          <doCatch> 
           <exception>java.lang.Exception</exception> 
           <handled> 
            <constant>true</constant> 
           </handled> 
           <bean ref="RecordErrorReport"/> 
           <choice> 
            <when> 
             <simple>${property.CamelSplitComplete} == true</simple> 
             <to uri="direct:auxrecordsmove"/> 
            </when> 
           </choice> 
          </doCatch> 
         </doTry> 
        </when> 
        <when> 
         <simple>${header.RECORDTYPE} == 'C'</simple> 
         <doTry> 
          <unmarshal ref="bindyComaker"/> 
          <bean ref="CoMakerPersist"/> 
          <choice> 
           <when> 
            <simple>${property.CamelSplitComplete} == true</simple> 
            <to uri="direct:auxrecordsmove"/> 
           </when> 
          </choice> 
          <doCatch> 
           <exception>java.lang.Exception</exception> 
           <handled> 
            <constant>true</constant> 
           </handled> 
           <bean ref="RecordErrorReport"/> 
           <choice> 
            <when> 
             <simple>${property.CamelSplitComplete} == true</simple> 
             <to uri="direct:auxrecordsmove"/> 
            </when> 
           </choice> 
          </doCatch> 
         </doTry> 
        </when> 
        Some other beans here........ 
        <when> 
         <simple>${property.CamelSplitComplete} == true</simple> 
         <to uri="direct:auxrecordsmove"/> 
        </when> 
        <otherwise> 
         <to uri="smtp://[email protected]&amp;[email protected]&amp;subject=URGENT:UNKOWN RECORD TYPE FOUND IN FILE"/> 
         <choice> 
          <when> 
           <simple>${property.CamelSplitComplete} == true</simple> 
           <to uri="direct:auxrecordsmove"/> 
          </when> 
         </choice> 
        </otherwise> 
       </choice> 
      </split> 
     </when> 
     <otherwise> 
      <to uri="file:d:/RMSInbox/.badFile?fileName=${file:onlyname.noext}_POSSIBLE_DUPLICATE_ERROR_${date:now:yyyyMMddHHmmss}.${file:name.ext}"/> 
      <bean ref="FileErrorReport"/> 
      <to uri="smtp://[email protected]&amp;[email protected]&amp;subject=ERROR: A File Could Not Be Imported&amp;contentType=text/html"/> 
     </otherwise> 
    </choice> 
</route> 

因此,這條路線上的每條消息最終都會碰到一個將它插入數據庫的bean。因此,我增加DBCP的依賴關係,然後在我的OSGi的XML藍圖聲明,如下所示:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
    <property name="url" value="jdbc:mysql://Canttouchthis:3306/ETLDB"/> 
    <property name="username" value="ETLUser"/> 
    <property name="password" value="password"/> 
    <property name="initialSize" value="2"/> 
    <property name="maxActive" value="16"/> 
    <property name="maxIdle" value="16"/> 
    <property name="minIdle" value="2"/> 
    <property name="timeBetweenEvictionRunsMillis" value="180000"/> 
    <property name="minEvictableIdleTimeMillis" value="180000"/> 
    <property name="testOnBorrow" value="true"/> 
    <property name="testWhileIdle" value="true"/> 
    <property name="testOnReturn" value="true"/> 
    <property name="validationQuery" value="SELECT 1"/> 
    <property name="maxWait" value="10000"/> 
    <property name="removeAbandoned" value="true"/> 
    <property name="logAbandoned" value="false"/> 
    <property name="removeAbandonedTimeout" value="300"/> 
</bean> 

我也宣佈我的豆子,將做處理這樣的:

<bean id="AccountPersist" class="com.foo.NewAccount.AccountInformationToDatabase"> 
    <property name="dataSource" ref="dataSource"/> 
</bean> 

現在拆分時在文件已經完成我想確保記錄匹配。基本上文件有帳戶記錄和一些輔助信息。所以我在分離完成時檢查路由,然後一旦文件完全在暫存表中,我在MySQL中執行一些額外的完整性檢查。

這第二條路看起來是這樣的:

<route trace="false" id="MoveMatchingAuxRecordsFromStage"> 
    <from uri="direct:auxrecordsmove"/> 
    <log message="File Import Route Ended At:${date:now:yyyy-MM-dd HH:mm:ss}"/> 
    <log message="ETL Route Start AT: ${date:now:yyyy-MM-dd HH:mm:ss}"/> 
    <log message="Moving Matching Comaker records at: ${date:now:yyyy-MM-dd HH:mm:ss}"/> 
    <bean ref="CoMakerETL"/> 
    <log message="Matching Comaker records move finised at: ${date:now:yyyy-MM-dd HH:mm:ss}"/> 
    <log message="Moving Matching Credit History records at: ${date:now:yyyy-MM-dd HH:mm:ss}"/> 
    <bean ref="CreditHistoryETL"/> 
    <log message="Matching Credit History records move finised at: ${date:now:yyyy-MM-dd HH:mm:ss}"/> 
    <log message="Moving Matching Extra Detail records at: ${date:now:yyyy-MM-dd HH:mm:ss}"/> 
    <bean ref="ExtraDetailInformationETL"/> 
    <log message="Matching Extra Detail records move finised at: ${date:now:yyyy-MM-dd HH:mm:ss}"/> 
    <log message="Moving Legal Information records at: ${date:now:yyyy-MM-dd HH:mm:ss}"/> 

    <bean ref="LegalInformationETL"/> 
    <log message="Matching Legal Information records move finised at: ${date:now:yyyy-MM-dd HH:mm:ss}"/> 
    <log message="ETL Route Finished At ${date:now:yyyy-MM-dd HH:mm:ss}"/> 

</route> 

所以在我的測試一切正常,我可以導入安靜有效像這樣的文件。當我在文件夾中放置超過5個文件時,我的問題就開始了。基本上,我觀察MySQL將連接池擴展到最大大小,然後不重新使用連接。

所以我們打16個併發連接他們去已經在4,5,6文件加載的地方,然後我突然收到以下錯誤後的幾個文件,睡覺:

Cannot get a connection, pool error Timeout waiting for idle object 

或者,正如它出現在日誌

[    pool-3-thread-35] oveMatchingAuxRecordsFromStage INFO Matching Extra Detail records move finised at: 2013-07-26 17:41:59 
[    pool-3-thread-35] oveMatchingAuxRecordsFromStage INFO Moving Legal  Information records at: 2013-07-26 17:41:59 
[    pool-3-thread-35] DefaultErrorHandler   INFO Failed delivery for (MessageId: ID-IMS-WS2013-001-52799-1374824474993-0-2693 on ExchangeId: ID-IMS-WS2013- 001-52799-1374824474993-0-3230). On delivery attempt: 0 caught: java.lang.Exception: Cannot get a connection, pool error Timeout waiting for idle object 
[thread #0 - file://d:/RMSInbox] ExtractRMSNewAccountFileRoute INFO STARTING ERROR GENERAL HANDLER 

在MySQL的最大連接數已經被推到一個巨大的512。我曾嘗試過各種池大小,線程選項等

作爲的問題我感興趣的是我的所有JDBC代碼遵循這種結構。它沒有複雜的SQL剛插入語句....

public class RecordParserErrorReporter { 
private static final String SQL_INSERT="INSERT INTO `ETL`.`ETLLog` "+ 
    " ("+ 
    " `ETL_log_text`, "+ 
    " `filename`, "+ 
    " `record_number`, "+ 
    " `error_message`) "+ 
     " VALUES "+ 
     " ("+ 
      " ?, "+ 
      " ?, "+ 
      " ?, "+ 
      " ? "+ 
     "); "; 

private BasicDataSource dataSource; 
public BasicDataSource getDataSource() { 
    return dataSource; 
} 
public void setDataSource(BasicDataSource dataSource) { 
    this.dataSource = dataSource; 
} 
public void HandleError 
(
     @Body String msgBody 
     , @Headers Map hdr 
     , Exchange exch 
) 
{ 

    Connection conn = null; 
    PreparedStatement stmt=null; 
    try 
    { 

     Exception e = exch.getProperty(Exchange.EXCEPTION_CAUGHT,Exception.class); 
     conn= dataSource.getConnection(); 
     stmt =conn.prepareStatement(SQL_INSERT);      
     stmt.setString(1, msgBody); 
     stmt.setString(2, (String)hdr.get("CamelFileName")); 
     stmt.setInt(3, (Integer)hdr.get("SPLITNUMBER")); 
     stmt.setString(4, e.getMessage()); 
     stmt.executeUpdate();   






    } 
    catch (Exception e) 
    { 
     System.out.println(e.getMessage()); 

    } 

    finally 
    { 
     try 
     { 
       if (stmt!=null) 
       { 
        stmt.close(); 
       } 
       if (conn!=null) 
       { 
        conn.close(); 
        conn= null; 
       } 
     } 
     catch(SQLException e) 
     { 
      System.out.println(e.getMessage()); 

     } 

    } 



} 

}

如何追查爲什麼我的連接不被重用?或者如果我泄漏連接,我該如何跟蹤?

更新:

我設置文件消費者消費的每一個投票1個文件與投票之間的第二延遲。我可以看到它爲每次路由啓動創建一個新的連接池,然後不重用先前創建的池。顯示對於我排隊的每個文件都會創建一個新的連接池。不完全是我想要的。它是否正確。

我還列舉了這看起來見下面的屏幕截圖:

enter image description here

更新2:

這不是在春天DM但OSGI藍圖運行。我相當確信數據源不止一次被實例化。

更新3:

好確定,因此DataSource不獲取實例化不止一次。閒置連接根本不用。我確實發現了一些有趣的東西,所以我懷疑這可能與我所看到的更多信息有關:鏈接:http://fusesource.com/forums/thread.jspa?threadID=4659&tstart=15

根據視圖的數量來判斷這個問題越來越明顯了。

回答

1

發現問題。這是一個真正的野餐錯誤或id10t錯誤在我身邊。

在我的ETL豆類我有下面的代碼行

try 
{ 
     conn= dataSource.getConnection(); 
     stmt =conn.prepareStatement(SQL_ETL_INSERT);  
     stmt.setString(1, (String)hdr.get("CamelFileName")); 
     stmt.executeUpdate();   
     conn= dataSource.getConnection(); 

     stmt =conn.prepareStatement(SQL_ETL_DELETE); 
     stmt.setString(1, (String)hdr.get("CamelFileName")); 
     stmt.executeUpdate();  

    } 
    catch (Exception e) 
    { 

     throw new Exception(e.getMessage()); 

    } 

    finally 
    { 
     try 
     { 
       if (stmt!=null) 
       { 
        stmt.close(); 
        stmt= null; 
       } 
       if (conn!=null) 
       { 
        conn.close(); 
        conn= null; 
       } 
     } 
     catch(SQLException e) 
     { 

      throw new Exception(e.getMessage()); 

     } 

    }  

通知我運行conn= dataSource.getConnection();兩次!然後我只發佈其中一個連接。複製和粘貼以及深夜編碼不會混合使用。