2013-05-30 41 views
0

我正在處理的問題是在執行與數據相比較的事務時,在以下TRY​​ {} CATCH {}語句中執行3 SP所花費的時間在2個SQL表中。我有一個從XML文件創建的數據集,解析並插入到CARD對象中,然後插入到該對象的兩個表中。正在處理的記錄數是15,198條記錄。根據每個表中的INSERTTMS列,記錄被插入到表中的時間戳記是10分33秒來完成插入。在Java代碼中運行SQL的更好的代碼

我需要幫助找到解決方案來重構此代碼,以幫助加快過程。我們正在SQL Server 2003上運行,我們正在使用Java 1.6。任何幫助/方向將不勝感激。謝謝。

下面是代碼:

做的
ResultSet results = null; 
    CallableStatement call = null; 
    PassThroughDBConnection con = null; 
    try { 
     con = new PassThroughDBConnection(); 
     con.setName("PBFDBConnection"); 
     con.setDBServer(System.getProperty(ATMServer.DBSERVER_PROPERTY)); 
     con.setDBServerType(System.getProperty(ATMServer.PROP_DBSERVERTYPE)); 
     con.setDBName(System.getProperty(ATMServer.DBNAME_PROPERTY)); 
     if (System.getProperty(ATMServer.DBUSER_PROPERTY) != null) { 
      con.setDBUser(System.getProperty(ATMServer.DBUSER_PROPERTY)); 
     } 
     if (System.getProperty(ATMServer.DBPASS_PROPERTY) != null) { 
      con.setDBPass(System.getProperty(ATMServer.DBPASS_PROPERTY)); 
     } 
     con.connect(); 

     call = con.prepareCall("{call clearData (?, ?)}"); 
     call.setInt("ServerID", ATMServer.getServerID()); 
     call.setInt("BankID", owningChannel.getBankID()); 
     call.executeUpdate(); 

     try { 
      call.close(); 
     } catch (Exception e) { 

     } 
     results = null; 
     call = null; 
     LOGGER.trace("Preparing to save PBF data to database. cards size: " + cards.size());   

     for (Card currentCard : cards.values()) { 

      // add the card record 
      // add any account records 
      call = con.prepareCall("{call insertCardData (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}"); 

      call.setInt("ServerID", ATMServer.getServerID()); 
      call.setInt("BankID", owningChannel.getBankID()); 
      call.setString("CardNumber", currentCard.cardnumber); 
      call.setString("Zip", currentCard.zip); 
      call.setString("Address", currentCard.address); 
      call.setString("Expiration", currentCard.expire); 
      call.setLong("PurchLimit", currentCard.purLimit); 
      call.setLong("PurchUsed", currentCard.purUsed); 
      call.setLong("ATMLimit", currentCard.atmLimit); 
      call.setLong("ATMUsed", currentCard.atmUsed); 
      call.setShort("Status", currentCard.status); 
      call.setShort("Sequence", currentCard.cardSequence); 
      call.setShort("CardType", currentCard.cardType.pbfValue); 

      results = call.executeQuery(); 
      Long newId = null; 
      if (results.next()) { 
       newId = results.getLong("NewID"); 
       if (newId != null) { 
        currentCard.databaseId = newId; 
        for (Account account : currentCard.accounts) { 
         if (account != null) { 
          call = con.prepareCall("{call insertAccountData (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}"); 
          call.setInt("ServerID", ATMServer.getServerID()); 
          call.setInt("BankID", owningChannel.getBankID()); 
          call.setString("AccountID", account.accountID); 
          call.setString("Zip", account.zip); 
          call.setString("Address", account.address); 
          call.setLong("Available", account.available); 
          call.setLong("Balance", account.balance); 
          call.setLong("CreditLine", account.creditline); 
          call.setShort("AccountType", (short) account.accountType.pbfValue); 
          call.setLong("CardId", newId); 
          call.executeUpdate(); 
          try { 
           call.close(); 
          } catch (Exception e) { 
           LOGGER.fatal("Error saving PBF data to database:" + FormatData.formatStack(e)); 
          } 
          results = null; 
          call = null; 
         } 
        } 
       } 
      } 
     } 

回答

1

的一種方式,這是做插件批量,而不是由一個刀片做一個。它可以節省多次到數據庫的次數並提高性能。

使用call.addBatch(),一旦所有刀片加入到批調用call.executeBatch()

像這樣的事情

   currentCard.databaseId = newId; 
       for (Account account : currentCard.accounts) { 
        if (account != null) { 
         call = con.prepareCall("{call insertAccountData (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}"); 
         call.setInt("ServerID", ATMServer.getServerID()); 
         call.setInt("BankID", owningChannel.getBankID()); 
         call.setString("AccountID", account.accountID); 
         call.setString("Zip", account.zip); 
         call.setString("Address", account.address); 
         call.setLong("Available", account.available); 
         call.setLong("Balance", account.balance); 
         call.setLong("CreditLine", account.creditline); 
         call.setShort("AccountType", (short) account.accountType.pbfValue); 
         call.setLong("CardId", newId); 
         call.addBatch(); 

        } 
       } 
       call.executeBatch()