javamysqlmaxhit

MySQL maximum hit in one looping


I have one Class ArrayList Named "Sentence". each Sentence contains value "Words". I have to check each words into MySQL database whether it is a stopwords or not. Let say, i have 200 "Sentence" and each Sentence have a various number of "Words".

I've check the "Words" one by one into the database, when it hit over 2500 number of "Words" (sometimes it hits until 3500th Words before the connection fail), it turns Error. But it will be fine if the number of "Words" below 2500.

This is my Query code

public Query() throws SQLException, ClassNotFoundException{
    try{
        cd = new Connect();
        connect = cd.getConnection();
        statement = (Statement) connect.createStatement();

    }catch(SQLException e){
        System.err.println("Database connecting failed");
    }
}

public boolean isStopWords(String word) throws SQLException{

    String query = "SELECT stopwords FROM stopword WHERE stopwords = '"+word+"'";

    rs = statement.executeQuery(query);

    if(!rs.next()) return false;
    else return true;
}

This is my Connection code

public Connect() throws SQLException{
    try{
        Class forName = Class.forName ("com.mysql.jdbc.Driver");
    }catch(ClassNotFoundException ex){

    }
    connect = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/mydatabase", "root", null);
}

public Connection getConnection(){
    return connect;
}

This is my method to retrieve the Word from Class Sentence.String[] tempWords is the value of Words in one object Sentence. And the Sentence itself will looping according to the size of the ArrayList Sentence

protected ArrayList<String> eraseStopWord (String[] tempWords) throws SQLException, ClassNotFoundException{
    result = new ArrayList<String>();
    db = new Query();
    for(int i=0;i<tempWords.length;i++){

         if(!db.isStopWords(tempWords[i])){
             result.add(tempWords[i]);
         }   
    }

    return result;

}

Why its fine until it hits approximately 2500 "Word"? (sometimes its fine until 3500th words before the connection fail, it just stable below 2500th of words)

UPDATE : now i know where the connection stopped. It stopped at 153rd sentence looping. so its not depend on the words. stil look after the bug


Solution

  • My fault. In my previous code, i make a connection everytime the Sentence created (db = new Query)

        protected ArrayList<String> eraseStopWord (String[] tempWords) throws SQLException, ClassNotFoundException{
        result = new ArrayList<String>();
        db = new Query();
        for(int i=0;i<tempWords.length;i++){
    
             if(!db.isStopWords(tempWords[i])){
                 result.add(tempWords[i]);
             }   
        }
    
        return result;
    
    }
    

    so, when it reach the 153rd sentence, it will stopped because of too many connection The solution is write the open connection code before Sentence looping