我正在解析HTML文件中的数据并将它们写入数据库。
它工作到10分钟前,当运行java工作流时突然出现越来越多的异常。我已经尝试过这个:set @@global.max_connections = 1250;
以下是错误示例:
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 :(得分:2)
您没有关闭与数据库的连接。一个原因是您创建了两个Database
实例。 main()
中的一个已关闭,但getData()
中的第二个未关闭。
请注意,您应该在finally
块中关闭数据库,以确保即使出现异常也会关闭连接。
重用数据库连接更改
parseIt.getData();
到
parseIt.getData(connect);
和
public void getData() throws IOException
到
public void getData(Database c) throws IOException