2014-07-21 73 views
0

我解析出HTML文件中的數據並將它們寫入數據庫。 它一直運行到10分鐘前,當運行java工作流程時突然出現越來越多的異常。我已經嘗試過這樣的:set @@global.max_connections = 1250;以下是錯誤的例子:我是執行我的代碼似乎效率低下,並引發jdbc異常

Jul 21, 2014 4:22:58 PM org.jsoup.examples.Database OpenConnectionDB 
SEVERE: null 
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections" 
    at sun.reflect.GeneratedConstructorAccessor9.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:408) 
    at com.mysql.jdbc.Util.getInstance(Util.java:383) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1023) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:997) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:983) 
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1112) 
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2508) 
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2541) 
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2323) 
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832) 
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46) 
    at sun.reflect.GeneratedConstructorAccessor5.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:408) 
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417) 
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at org.jsoup.examples.Database.OpenConnectionDB(Database.java:34) 
    at org.jsoup.examples.Database.insert(Database.java:67) 
    at org.jsoup.examples.parseEasy.getData(parseEasy.java:61) 
    at org.jsoup.examples.parseEasy.main(parseEasy.java:30) 

的(牆)代碼是在這裏。也許你的某個人對我在這裏犯的錯誤有很好的看法。我無法弄清楚,我不希望任何人通過它進行搜索。如果有人立即知道錯誤發生在哪裏,我很樂意聽到它。謝謝!

package org.jsoup.examples; 

import java.io.*; 

import org.jsoup.*; 
import org.jsoup.nodes.*; 
import org.jsoup.select.Elements; 

import java.io.IOException; 

/** 
* Example program to list links from a URL. 
*/ 
public class parseEasy { 



     static parseEasy parseIt = new parseEasy(); 

     String companyName = "Platzhalter"; 
     String jobTitle = "Platzhalter"; 
     String location = "Platzhalter"; 
     String timeAdded = "Platzhalter"; 

    public static void main(String[] args) throws IOException 
    { 
     Database connect = new Database(); 
     connect.OpenConnectionDB(); 

     parseIt.getData(); 

     connect.closeConnectionDB(); 


    } 

    // 
    public void getData() throws IOException 
    { 
     Database c = new Database(); 

     Document document = Jsoup.parse(new File("C:/Talend/workspace/WEBCRAWLER/output/keywords_Medizin.txt"), "utf-8"); 
     Elements elements = document.select(".joblisting"); 
     for (Element element : elements) 
     { 
      // Counter for Number of Elements returned 


      // Parse Data into Elements 
      Elements jobTitleElement = element.select(".job_title span"); 
      Elements companyNameElement = element.select(".company_name span[itemprop=name]"); 
      Elements locationElement = element.select(".locality span[itemprop=addressLocality]"); 
      Elements dateElement = element.select(".job_date_added [datetime]"); 

      // Strip Data from unnecessary tags 
      String companyName = companyNameElement.text(); 
      String jobTitle = jobTitleElement.text(); 
      String location = locationElement.text(); 
      String timeAdded = dateElement.attr("datetime"); 

      c.insert(companyName, jobTitle, timeAdded, location); 



      // Test output 
      //System.out.println("Firma:\t"+ companyName + "\t" + jobTitle + "\t in:\t" + location + " \t Erstellt am \t" + timeAdded + "\t. Eintrag Nummer:\t" + count); 

     } 

    } 

    } 
+0

連接太多。 –

+0

爲什麼你要在'getData()'中創建一個新的數據庫? – vandale

+0

我對編程頗爲陌生。我想我不能從一些靜態的上下文中引用它?嗯,任何想法如何解決它?謝謝你到目前爲止 – ZedBrannigan

回答

2

您沒有關閉與數據庫的連接。其中一個原因是您創建了Database的兩個實例。 main()中的一個已關閉,但getData()中的第二個不是。

請注意,您應該關閉finally塊中的數據庫,以確保即使存在異常時連接仍處於關閉狀態。

複用數據庫連接改變

parseIt.getData(); 

parseIt.getData(connect); 

public void getData() throws IOException 

public void getData(Database c) throws IOException 
+0

這對我來說很有意義。謝謝,我會試試! – ZedBrannigan

+0

對不起,關於這兩個實例的一個問題:有沒有一種方法可以使用一個?如果我嘗試使用connect(之前調用的數據庫實例),我會得到「靜態引用非靜態....」錯誤? – ZedBrannigan

+0

爲什麼不將'main()'實例作爲參數傳遞給'getData()'? –