I want to kill some jobs from a java app by calling the procedure below and passing the parameters which are job id, session id and session serial number. It won't work and I am very lost here.
PROCEDURE kill_batch_test (
V_JOB IN VARCHAR2,
V_SID IN VARCHAR2,
V_SERIAL IN VARCHAR2 )
IS
BEGIN
DBMS_JOB.REMOVE(V_JOB);
execute immediate 'Alter System Kill Session '''|| to_char (V_SID) || ',' || to_char (V_SERIAL) || ''' IMMEDIATE';
EXCEPTION
WHEN OTHERS THEN
RETURN;
END;
[EDIT]
The java code : This gets me the user jobs :
String sql1 = "select sid SID, username, serial# SERIAL,j.job JOB, WHAT from (select dj.LOG_USER, dj.JOB, dj.BROKEN, dj.FAILURES, dj.LAST_DATE,dj.LAST_SEC, dj.THIS_DATE, dj.THIS_SEC, dj.NEXT_DATE, dj.NEXT_SEC, dj.INTERVAL, dj.WHAT from dba_jobs dj ) j,(select p.spid, s.username, s.sid, s.serial# from v$process p, v$session s where p.addr = s.paddr AND s.username = 'EPS') s ";
SQLQuery res = (SQLQuery) session.createSQLQuery(sql1).setCacheable(false);
res.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List e = res.list();
Object o;
Iterator it = e.iterator();
for(int i=0;i<e.size();i++){
o = it.next();
Map m = (Map)o;
String sid = null;
String serial = null;
String job = null;
sid = m.get("SID").toString();
serial = m.get("SERIAL").toString();
job = m.get("JOB").toString();
here I call the procedure :
CallableStatement storedProc = hibConnection.prepareCall(sql);
storedProc.setString(1,job);
storedProc.setString(2,sid);
storedProc.setString(3,serial);
storedProc.executeUpdate();
}
Firstly, as pointed out in the comments, the EXCEPTION WHEN OTHERS THEN RETURN
section of your procedure really isn't helping you. To stop it swallowing exceptions, simply delete it.
To fix the 'insufficient privileges' error that then appears, connect to your database as a suitably-privileged user and run GRANT ALTER SYSTEM TO <user your Java app connects as>
. Be careful with this privilege as there are a lot of other possible ALTER SYSTEM
statements that your user now has permission to run.
Finally (you may already be aware of this), consider moving from DBMS_JOB
to DBMS_SCHEDULER
. In particular, the latter has a more robust way of stopping jobs without the forceful approach you are using which involves finding the session used to run the job and killing that session.