We have a java application where before this all was done in the run function of the multithreaded application. Now we are moving out some codes to a separate function. We have to make some variables as global including the connection so that it can be used in the function. Below is the skeleton of the run and calling of the function. The issue now those queries which are process in the if statement and they run into problem then the whole thing goes into catch and rollback. The problem here now is that those which are called in the processOne function and if they run into any catch the general queries which are run after if else is executed too. Is there any way to stop or linked it to the processOne error? Our idea is to use a global variable because tried the dbConn.rollback in it also it do work.
public void run() {
try{
if(){
//process here
}
else{
// call function processOne
}
//some other general queries
dbconn.commit();
}
catch (SQLException ex){
try{
dbconn.rollback();
}
catch (Exception rollback){
}
}
}
void processOne(){
try{
//process queries here
catch (SQLException ex){
try{
dbconn.rollback();
}
catch (Exception rollback){
}
}
}
All you need to do is throw an exception from
processOnefunction, and then catch it in the ‘run` function and rollback the transaction:That way the entire process is rolled back.