Calling an Oracle procedure from Java - what happens when System.exit() is called?

339 views Asked by At

Here is code for an Oracle procedure to be called from Java:

CallableStatement st = connection.prepareCall("{call PROCEDURE_0(?,?,?)}");

st.setInt(1,xyz1);
st.setString(2,xyz2);
st.setInt(3,int_variable);

st.registerOutParameter(3,Types.INTEGER);

try{

st.execute();

}
catch(Exception e){

}

If a user calls System.exit() on this Java JVM - what happens to the Oracle procedure that was called? Is it guaranteed to keep running in Oracle? Or perhaps, in order to guarantee this, I should submit it to the Oracle job scheduler instead? So far, my experience has been that sometimes the procedure keeps running even if the JVM terminates, and sometimes it does not. Does this have anything to do with 'registering an out parameter'? Will the code reach the catch block if System.exit is called?

Does anyone have experience with this?

1

There are 1 answers

0
Alexander Mills On

This code which I stole from Mike McAllister which he also stole from an Oracle forum solves the problem I was mentioning in this question. It works, and you can pass dynamic variables as parameters, not just hard-coded values.

this SO question also addresses the problem:

Passing arguments to oracle stored procedure through scheduler job

-- create a stored procedure with two arguments
    create or replace procedure myproc (arg1 in varchar2, arg2 in varchar2)
    is BEGIN null; END;
    /

-- create a program with two arguments and define both
begin
dbms_scheduler.create_program
(
program_name=>'myprog',
program_action=>'myproc',
program_type=>'STORED_PROCEDURE',
number_of_arguments=>2, enabled=>FALSE
) ;

dbms_scheduler.DEFINE_PROGRAM_ARGUMENT(
program_name=>'myprog',
argument_position=>1,
argument_type=>'VARCHAR2',
DEFAULT_VALUE=>'13');

dbms_scheduler.DEFINE_PROGRAM_ARGUMENT(
program_name=>'myprog',
argument_position=>2,
argument_type=>'VARCHAR2');

dbms_scheduler.enable('myprog');
end;
/

-- create a job pointing to a program and set both argument values
begin
dbms_scheduler.create_job('myjob',program_name=>'myprog');
dbms_scheduler.set_job_argument_value('myjob',1,'first arg');
dbms_scheduler.set_job_argument_value('myjob',2,'second arg');
dbms_scheduler.enable('myjob');
end;
/