Saturday, 9 April 2016

Make Delay in your PL\SQL code Using Java

Any scenario or any requirement can drive you make such delay in your code. recently I have came across  some requirement where I have to hold my code flow for couple of seconds. the scenario was that there is a Database job that sends E-mail through Microsoft Exchange server using Database code. I will not discuss the code of E-mail right now but the problem was when i run the code it loops through all the target emails within 4 seconds that was the problem for Exchange Server to get hold all the requests, Therefore, I need to make delay during execution of PL\SQL block.
Here it is.

You can use this according to any of your requirement like this.

I am using 






Here is your Java source that will be Stored in your Database.





CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "Sleep"
as import java.io.*;
public class Sleep extends Object
{
   public static void sleep()
   {
      try
      {
         Thread.sleep(3000);  //<----------- 1000 =  1 second
      }
      catch(Exception ex)
      {}
   
   }
 
 
}

Once compile above command you can find it in your Database tool's navigator.



Now make PL\SQL wrap for your Java Source.


Create Or Replace Procedure P_Sleep 
As Language Java Name  
'Sleep.sleep() ';


Now at this step you can use your flow even in your Package or any procedure using this code template.

Declare
   V$time_1 Number ; 
   V$time_2 Number ;  
   V$differnce Number ;  
Begin
   V$time_1:= Dbms_Utility.Get_Time;   
   Dbms_Output.Put_Line('Starting Time: ' || V$time_1 );   
   P_Sleep;
   V$time_2 := Dbms_Utility.Get_Time;
   Dbms_Output.Put_Line('Ending Time: ' || V$time_2);
   V$differnce := (V$time_2 - V$time_1) ;  
   Dbms_Output.Put_Line('Difference in Time: ' ||  V$differnce / 100 || ' - Secs') ; 
End;


1 comment: