I am calling threads based on records selected from database.
However, I am seeing that same records are initiated multiple times in the thread.
Also I am inserting records in the keyvaluepairs inside the class Mytask and then iterating over it to update the response back into the database. the response update is also not happening for some records.
Code Snip
try {
con = DriverManager.getConnection(url, user, password);
System.out.println("Database connected successfully" + "| | | | | |");
Statement UpdateStmt = con.createStatement();
Statement stmt = con.createStatement();
String sqlQuery = "select distinct a, b,C, D from IBL_PAYMENT_STG_TBL WHERE RESPONSEFLAG in ('N','P') and TO_CHAR(creationdate, 'DD-MM-YYYY') in (TO_CHAR(sysdate-1, 'DD-MM-YYYY'),TO_CHAR(sysdate, 'DD-MM-YYYY'))";
String sqlQuery2 = "select count(*) ct from IBL_PAYMENT_STG_TBL WHERE RESPONSEFLAG in ('N','P') and TO_CHAR(creationdate, 'DD-MM-YYYY') in (TO_CHAR(sysdate-1, 'DD-MM-YYYY'),TO_CHAR(sysdate, 'DD-MM-YYYY'))";
String sqlQuery3 = null;
ResultSet resultSet2 = stmt.executeQuery(sqlQuery2);
while (resultSet2.next()) {
cnt = resultSet2.getInt("ct");
countRecords = resultSet2.getInt("ct");
System.out.println("Total Number of Payments: " + cnt + "| | | | |");
}
resultSet2.close();
if (cnt > 0) {
System.out.println("Starting");
}
int op = 0;
int LoopCount = (int) Math.ceil((double) cnt / 15);
try {
for (int j = 0; j < LoopCount; j++) {
// keyValuePairs.clear();
if (cnt >= 15) {
op = 15;
} else {
op = cnt;
}
cnt = cnt - 15;
// Thread[] threads = new Thread[op]; //Array code
List<Thread> threads = Collections.synchronizedList(new ArrayList<>()); //List code
ResultSet resultSet = stmt.executeQuery(sqlQuery);
int i = 0;
long J = 0;
String K;
String L;
String M;
while (resultSet.next() && i < op) {
// System.out.println("Thread Batch Started");
J = resultSet.getLong("a");
K = resultSet.getString("b");
L = resultSet.getString("D");
M = resultSet.getString("C");
// System.out.println(J + " : I " + i);
sqlQuery3 = "UPDATE IBL_PAYMENT_STG_TBL SET RESPONSEFLAG = 'IP' WHERE a = " + J;
UpdateStmt.executeQuery(sqlQuery3);
Thread thread = new Thread(new MyTask(J, K, L, keyValuePairs, M)); //List Code
thread.start(); //List Code
threads.add(thread); //List Code
// threads[i] = new Thread(new MyTask(J, K, L, keyValuePairs, M,i)); //Array code
// threads[i].start(); // Array Code
i++;
}
// System.out.println("Threading Complete");
resultSet.close();
//code for list
synchronized (threads){
for (Thread thread: threads){
try {
thread.join();
}catch (Exception e){
e.printStackTrace();
}
}
}
//Code for Array
// try {
// for (Thread thread : threads) {
// thread.join(); // Wait for each thread to finish
// }
//// System.out.println("Thread Joined");
// } catch (Exception e) {
// e.printStackTrace();
// System.out.println("Thread Execution failed");
// }
}
} catch (Exception exception) {
exception.printStackTrace();
String ExceptionString = exception.toString();
System.out.println(ExceptionString);
} finally {
// Code to be executed after all threads have finished
System.out.println("ALL threads executed, Update starting");
for (Long ID : keyValuePairs.keySet()) {
try {
Statement Updatestmt = con.createStatement();
String opPayload = keyValuePairs.get(ID);
System.out.println("update statement: " + opPayload);
try {
// System.out.println("update statement: "+opPayload);
Updatestmt.executeQuery(opPayload);
System.out.println("Update successful for ID: " + ID);
} catch (Exception e) {
e.printStackTrace();
String ExceptionString = e.toString();
String query = "UPDATE IBL_PAYMENT_STG_TBL " +
" SET ERRORFLAG = 'Y' ," +
" RESPONSEFLAG = 'IP'" + "," +
" ATTRIBUTE3 = ' Update Error: " + ExceptionString + "'" + "," +
" ATTRIBUTE4 = '" + opPayload.replaceAll("'", " ") + "'" +
" WHERE a =" + ID;
System.out.println(ID + "|" + "update statement Exception: " + query + "| | | | |");
Updatestmt.executeQuery(query);
}
} catch (Exception e) {
e.printStackTrace();
String ExceptionString = e.toString();
System.out.println(ID + "|" + ExceptionString);
}
}
// con.commit(); cannot manually commit as auto commit is set true
con.close();
System.out.println("update done" + "| | | | | |");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
System.out.println(throwables.toString());
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.toString());
}
New contributor
Abhishek sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.