• Post Reply Bookmark Topic Watch Topic
  • New Topic

Retry on getting exception  RSS feed

 
Kaush Kane
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me out on this one.
I am having some code in try block.

I am getting an exception of ORA-00060: deadlock detected.
If i get this exception then I want to implement a logic where I want to re-execute the code which is in the try block.
How can this be achieved?

I have following doubt:
Once the exception is thrown the execution will be in the catch block (i.e. out of the try block). Once in catch can we again go back to the try block and re-execute the code. How can this be achieved?

Please help me out.
 
Rao Raghu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting an exception of ORA-00060: deadlock detected.
If i get this exception then I want to implement a logic where I want to re-execute the code which is in the try block.


Do you mean you have a modified code after you get the exception?

If yes, then put that code in the catch clause itself. That will be an appropriate place.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way to re-run the try block is to put the entire try-catch inside a loop. Whether this will actually help, I don't know; going back 3 microseconds after an Exception may simply cause another Exception to be thrown if the original problem remains.
 
Kaush Kane
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I need to implement a logic where:
IF I get an exception of ORA-00060: deadlock detected then I need to re-execute the code which is present in the try block.
I need to do it repeatedly over a peroid of 10 minutes until it becomes successful.

How can I do this?

Please help me out.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Retry over ten minutes? You would have to record the time at the beginning of your loop, then you could put a test for whether the time has increased by 600000 (milliseconds) and whether the loop has been successfully completed, as part of your repetition.
I suggest a myThread.sleep(500) somewhere which will introduce a delay of 1/2 second approx, as well.
 
Kaush Kane
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for so quick replies.
Just one more query. How can I run a loop for ten minutes?
I had a look in the Date and Calendar class but I am not able to understand that how can I run a loop for ten minutes.

Progress till now is:
I need help to create condition for while loop in below code

try{
func1();
}
catch(SQLException e){
if (ex.getErrorCode() == 00060){
while (///a condition that will run this for 10 minutes){
try{
func1();
break; // if here means execution was successful hence break.
}catch(SQLException e){
// handling here
}

}
}
}
 
Kaush Kane
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for so quick replies.
Just one more query. How can I run a loop for ten minutes?
I had a look in the Date and Calendar class but I am not able to understand that how can I run a loop for ten minutes.

Progress till now is:
I need help to create condition for while loop in below code

try{
func1();
}
catch(SQLException e){
if (ex.getErrorCode() == 00060){
while (///a condition that will run this for 10 minutes){
try{
func1();
break; // if here means execution was successful hence break.
}catch(SQLException e){
// handling here
Thread.sleep(500);
}

}
}
}
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Date.getTime() to get the milliseconds. Add 10*60*1000(timer) to the time you by getTime and check it after each loop if you are <= timer. If yes continue else quit.
 
Kaush Kane
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the while condition I doing the following:

long currentTime = Calendar.getInstance().getTimeInMillis();
long requiredTime = currentTime + 600000;
while(Calendar.getInstance().getTimeInMillis() < requiredTime)

Please comment on the above condition.

Also when would I get value of Calendar.getInstance().getTimeInMillis() as 0
i.e. when does the Calendar reset to zero??
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Condition seems to be ok.

A zero would represent January 1, 1970, 00:00:00.
[ March 07, 2007: Message edited by: Anupam Sinha ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the approach is fine. The break & sleep look good. You can use System.currentTimeMillis() to get current time with a little less clutter.

Is a user going to sit and wait 10 minutes for a response? What happens if you haven't succeeded yet after 10 minutes?
 
Kaush Kane
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
long currentTime = Calendar.getInstance().getTimeInMillis();
long requiredTime = currentTime + 600000;
while(Calendar.getInstance().getTimeInMillis() < requiredTime)


Thanks for giving your comments on the above approach.

But I was thinking of a situation when the currentTime would reach maximum limit that long can handle and then when I add 600000 to it, then it will face some issue as adding 600000 would mean exceeding the limit of long.

Is this a realistic situation? Would anytime Calendar.getInstance().getTimeInMillis() reach maximum limit that long can handle?

Please give your comments...
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup it would probably reach its maximum value sometime. Can you determine how many years would that take. You may like to use Long.MAX_VALUE.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!