how to use dbms_scheduler to run the job every 30 minutes - oracle

I am using oracle DB , now as i am monitoring the performance of oracle DB which is connected to my java application , so rite now i have to monitor the count of active connections in DB at regular intervals lets say after every 30 minutes below is the query which return me the count of active users along with there name and count
select osuser, count(osuser) as active_conn_count
from v$session
group by osuser
order by active_conn_count desc
now please advise how can i make an schedule a job in scheduler in oracle DB itself that will get triggered at every 30 minutes .

I would suggest to keep your statistics in a table (say my_log_table), in that case schedule would look something like this:
begin
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'keep_stats',
job_type => 'PLSQL_BLOCK',
job_action => 'begin insert into my_log_table (mUser,mCnt) (select osuser, count(osuser) as active_conn_count from v$session group by osuser order by active_conn_count desc);commit;end;',
start_date => timestamp '2016-11-07 19:00:00',
repeat_interval => 'FREQ=MINUTELY;INTERVAL=30;',
enabled => TRUE);
end;

There are pretty much examples about dbms_scheduler. It's easy to submit a recurring job. But actually you do not need to do that! Oracle already stores tons of data about database performance, it will be easier and much more accurate to use these statistics.
Take a look at the active_session_history over here.
Hope this is what you are looking for.

repeat_interval => 'FREQ=HOURLY; BYMINUTE=0,30; BYSECOND=0 ', -

First, you create a schedule.
dbms_scheduler.create_schedule( schedule_name => 'EVERY_1ST_SATURDAY'
, repeat_interval => 'FREQ=MONTHLY;INTERVAL=1;BYDAY=1 SAT;BYHOUR=2;BYMINUTE=0'
, start_date => SYSTIMESTAMP
, comments => '1st Saturday of the month 2am.'
);
Optionally, you create a program:
dbms_scheduler.create_program( program_name => 'GATHER_SCHEMA_STATS'
, program_type => 'STORED_PROCEDURE'
, program_action => 'dbms_stats.GATHER_SCHEMA_STATS'
, number_of_arguments => 1
, enabled => FALSE
, comments => 'Gather schema''s stats (e.g. FRAMEWORK''s)'
);
... grant access to it:
GRANT execute ON sys.GATHER_SCHEMA_STATS TO framework;
... and parameters for it:
DBMS_SCHEDULER.define_program_argument ( 'GATHER_SCHEMA_STATS', 1 , 'ownname', 'VARCHAR2', 'FRAMEWORK' );
Secondly, you create a job:
DBMS_SCHEDULER.CREATE_JOB ( job_name => 'FRAMEWORK.GATHER_FRAMEWORK_STATS_JOB1'
, program_name => 'SYS.GATHER_SCHEMA_STATS'
, schedule_name => 'SYS.EVERY_1ST_SATURDAY'
, enabled => TRUE
, auto_drop => FALSE
, comments => 'Gather FRAMEWORK''s stats on 1st Saturday of the month.'
);

Related

Scheduler job stuck on running oracle

I have this scheduler made on Oracle pl/SQL that deletes old users from the database every 5 years.
I tried testing it by changing the time period from 5 years to 1 min (and starting date to five minutes after insert), it stucks on running and doesn't complete the job unless the scheduler gets stopped.
How can this be fixed? I tried using commit after the delete statement with no results.
Here's the code:
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'Delete_Idle_Users',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN
DELETE FROM Store_Member
WHERE Member_ID IN (
Select Member_ID
FROM (
select Member_ID, Max(Purchase_Date) AS Last_Purchase
From Cart
Group By Member_ID
)
WHERE Last_Purchase<(SYSDATE-(365.25*5))
);
END;',
start_date => TO_DATE('01-GEN-2021','DD-MON-YYYY'),
repeat_interval => 'FREQ=YEARLY',
enabled => TRUE,
comments => 'Deleting users that didn't buy anything in 5 years.'
);
END;

Create oracle scheduler job which runs daily

I want to create oracle scheduler job which runs daily at 20:00 and runs for 30 minute. This job will delete the rows from KPI_LOGS table as this table contains large amount of data and it continues to grow. I have created the below script in oracle sql developer for such job but not sure if this is correct or not as i am new to scheduler job concept.
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => '"RATOR_MONITORING"."CROP_KPI_LOGS"',
job_type => 'PLSQL_BLOCK',
job_action => 'DELETE FROM KPI_LOGS WHERE CAST(TIMESTAMP AS DATE) < (SYSDATE - 28);',
number_of_arguments => 0,
start_date => NULL,
repeat_interval => 'FREQ=DAILY;INTERVAL=30',
end_date => NULL,
enabled => FALSE,
auto_drop => FALSE,
comments => 'CROP_KPI_LOGS');
DBMS_SCHEDULER.SET_ATTRIBUTE(
name => '"RATOR_MONITORING"."CROP_KPI_LOGS"',
attribute => 'logging_level', value => DBMS_SCHEDULER.LOGGING_OFF);
DBMS_SCHEDULER.enable(
name => '"RATOR_MONITORING"."CROP_KPI_LOGS"');
END;
the repeat_internal is incorrect, what you have there will run every 30 days. to run at 8pm each day go for...
FREQ=DAILY; BYHOUR=20
you can't dictate how long it will run for, it will take as long as your DELETE statement takes

How to limit oracle job interval for a particular day ,(Monday)?

Hi i have to show one report and the report data should refresh on every monday.
automatically
I have checked oracle scheduler . i cant see any option for limiting the interval to monday
.
I am thinking to give start date as monday and repeat 7 days interval .
is it good way
Hi I got the answer.
YOU want to right like the code below.
There are 3 way for scheduling a job on every week day it can be every monday or another day
Here i have tried for running the job automatically on every monday 6 am
We have to set the frequency
either of the below code we can use
FREQ=DAILY; BYDAY=MON; BYHOUR=6; BYMINUTE=0; BYSECOND=0;
FREQ=WEEKLY; BYDAY=MON; BYHOUR=6; BYMINUTE=0; BYSECOND=0;
FREQ=YEARLY; BYDAY=MON; BYHOUR=6; BYMINUTE=0; BYSECOND=0;
begin
dbms_scheduler.create_job(
job_name => 'JOB_TEST',
job_type => 'PLSQL_BLOCK',
job_action => 'begin PROCEDURE_NAME; end;',
start_date => systimestamp,
end_date => null,
comments => 'job is created for running automatically on every monday',
enabled => true,
repeat_interval => 'FREQ=WEEKLY; BYDAY=MON; BYHOUR=0; BYMINUTE=0; BYSECOND=0;');
end;
In oracle We can write jobs using DBMS Scheduler or DBMS JOBS
DBMS Scheduler is the above example and its the advanced one,
IF you want to Do the same Problem Using DMBS JOBS
begin
sys.dbms_job.submit(job => :job,
what => 'procedure name;',
next_date => NEXT_DAY(TRUNC(SYSDATE),'MONDAY'),
interval => 'NEXT_DAY(TRUNC(SYSDATE),''MONDAY'')');
commit;
end;
/

Oracle job scheduler not dropping automatically

I have below oracle job.
dbms_scheduler.create_job
(job_name => m_job_name,
job_type => 'PLSQL_BLOCK',
job_action => 'begin Pkg_Shell.PR_WF_PROC('
|| p_seq_request
|| '); end;',
number_of_arguments => 0,
start_date => sysdate,
repeat_interval => null,
end_date => null,
job_class => 'DEFAULT_JOB_CLASS',
enabled => false,
auto_drop => true,
comments => null
);
The above job is not dropping automatically. This job will run only once. When i went thru various sites it says
For auto drop,This flag if TRUE, causes a job to be automatically dropped after it has completed or has been automatically disabled. A job is considered completed if:
1.Its end date (or the end date of the job schedule) has passed.
2.It has run max_runs number of times. max_runs must be set with SET_ATTRIBUTE.
3.It is not a repeating job and has run once.
My job will run only once. Why my job is not Auto dropped in certain scenarios. ? We couldnt find when it is not dropped. To over come this
If i want to mention end_date like sysdate + 2 hours how to mention it ?
If i want to set max_runs or max_fails how to use that in my job. ? Whether these two settings or anyone above will solve my problem ?
After so long i found the below links that helped me to fix my issue. I have used max_runs set to 1 .
dbms_scheduler.set_attribute(m_job_name,'max_runs',1);
https://community.oracle.com/thread/936850
https://community.oracle.com/message/2458833#2458833

DBMS_SCHEDULER JOBS Running at morning until night everyday

I want make JOBS use DBMS_SCHEDULER in oracle 10g, where jobs refresh minutely with interval 2 minute running everyday start at 08.00 AM and end at 08.00 PM. I have tried this code,
BEGIN
SYS.DBMS_SCHEDULER.CREATE_JOB
(
job_name => 'UPDATE_REKAP_BALI'
,start_date => trunc(sysdate) + 8/24
,repeat_interval => 'freq=MINUTELY;interval=2'
,end_date => trunc(sysdate) + 20/24
,job_class => 'DEFAULT_JOB_CLASS'
,job_type => 'STORED_PROCEDURE'
,job_action => 'UPDATEREKAPBALI'
,comments => NULL
);
END
but, when i check on the next day, the jobs is not running, i guess that the jobs is never running up again on 08.00 AM at the next day.
Make sure you commit after submitting the job.
Edit This is incorrect: DBMS_SCHEDULER performs an implicit commit, unlike the previous DBMS_JOB which required an explicit commit.
You have to make auto_drop to false , because auto drop will make the job to be dropped once after it is running , so make it as false
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'TEST_J',
job_type => 'CHAIN',
job_action => 'TEST_C',
auto_drop => FALSE,
repeat_interval => 'FREQ=DAILY;BYHOUR=08,09,10,11,12,13,14,15,16,17,18,19;BYMINUTE=02,04,06,08,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,00;BYSECOND=00;',
enabled => TRUE);
END;
/
Auto Drop is enabled by default
http://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_sched.htm#i1000363
The answer is two links far
http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_sched.htm#CIHGCDBJ - Table 93-54 Window Attribute Values
'repeat_interval' line in the table point to "Calendaring Syntax" link http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_sched.htm#BABFBCEF where you can find out how to set a schedule in terms of Oracle

Resources