Issue
I am writing a Java database program, in this program i have a loop that calls about 6 methods, in each method an connection is established to MySQL indepentantly from the other connections in order to return a boolean value,the while loop runs 20 times(row count is 20) and therefore (6*20=120) connections are made in that loop, problem is, after the 14th or the 19th iteration, i get this error from MySQL:
Exception in thread "main" java.lang.NullPointerException
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
Also this : at project_2.Reserve.inLarge(Reserve.java:116)
Where line 116 contains:
Statement s = c.createStatement();
And where c is a Connection type (i.e.MySQL Java Connection type).
every function creates a connection called c and its' own statments.
I am not expereinced in MySQL, so I expect that some would tell me that this is a terrible way to do it and then yell at me...just kidding, thanks for help.
I also tried adding thread.sleep to delay things but it didn't work.
Solution
I found the problem, it was in get_conn() function i had this line twice so obviously it was a little hard on the mysql server DriverManager.getConnection(conn_str,username,pass);
:
public static Connection get_conn (){
Connection conn=null;
try{
conn =(Connection) DriverManager.getConnection(conn_str,username,pass);
conn =(Connection) DriverManager.getConnection(conn_str,username,pass);
}catch (SQLException e){
System.out.println(e);
}
return conn;
}
Answered By - aero
Answer Checked By - David Goodson (JavaFixing Volunteer)