Oracle DBMS SCHEDULER
Windows provide the link between the scheduler and the resource manager, allowing different resource plans to be activated at different times. Since job classes point to resource consumer groups, and therefore resource plans, this mechanism allows control over the resources allocated to job classes and their jobs during specific time periods.
Only one window can be active (open) at any time, with one resource plan assigned to the window. The affect of resource plan switches is instantly visible to running jobs which are assigned to job classes. The interaction between the resource manager and the scheduler is beyond the scope of this document.
An inline schedule.
BEGIN
DBMS_SCHEDULER.create_window (
window_name => 'test_window_1',
resource_plan => NULL,
schedule_name => 'test_hourly_schedule',
duration => INTERVAL '60' MINUTE,
window_priority => 'LOW',
comments => 'Window with a predefined schedule.');
-- Window with an inline schedule.
DBMS_SCHEDULER.create_window (
window_name => 'test_window_2',
resource_plan => NULL,
start_date => SYSTIMESTAMP,
repeat_interval => 'freq=hourly; byminute=0',
end_date => NULL,
duration => INTERVAL '60' MINUTE,
window_priority => 'LOW',
comments => 'Window with an inline schedule.');
END;
/
PL/SQL procedure successfully completed.
Display window group details.
SELECT window_name, resource_plan, enabled, active FROM dba_scheduler_windows;
WINDOW_NAME RESOURCE_PLAN ENABL ACTIV
------------------------------ ------------------------------ ----- -----
WEEKNIGHT_WINDOW TRUE FALSE
WEEKEND_WINDOW TRUE FALSE
TEST_WINDOW_1 TRUE FALSE
TEST_WINDOW_2 TRUE FALSE
4 rows selected.
Windows can be opened and closed manually using the OPEN_WINDOW and CLOSE_WINDOW procedures.
BEGIN
DBMS_SCHEDULER.open_window (
window_name => 'test_window_2',
duration => INTERVAL '1' MINUTE,
force => TRUE);
END;
/
PL/SQL procedure successfully completed.
Display window group details.
SELECT window_name, resource_plan, enabled, active FROM dba_scheduler_windows;
WINDOW_NAME RESOURCE_PLAN ENABL ACTIV
------------------------------ ------------------------------ ----- -----
WEEKNIGHT_WINDOW TRUE FALSE
WEEKEND_WINDOW TRUE FALSE
TEST_WINDOW_1 TRUE FALSE
TEST_WINDOW_2 TRUE TRUE
4 rows selected.
Close window.:-
BEGIN
DBMS_SCHEDULER.close_window (
window_name => 'test_window_2');
END;
/
PL/SQL procedure successfully completed.
Display window group details.
SELECT window_name, resource_plan, enabled, active FROM dba_scheduler_windows; WINDOW_NAME RESOURCE_PLAN ENABL ACTIV ------------------------------ ------------------------------ ----- ----- WEEKNIGHT_WINDOW TRUE FALSE WEEKEND_WINDOW TRUE FALSE TEST_WINDOW_1 TRUE FALSE TEST_WINDOW_2 TRUE FALSE 4 rows selected.
Windows can be dropped using the DROP_WINDOW procedure.
BEGIN
DBMS_SCHEDULER.drop_window (
window_name => 'test_window_1',
force => TRUE);
DBMS_SCHEDULER.drop_window (
window_name => 'test_window_2',
force => TRUE);
END;
/
PL/SQL procedure successfully completed.
Display window group details.
SELECT window_name, resource_plan, enabled, active FROM dba_scheduler_windows;
WINDOW_NAME RESOURCE_PLAN ENABL ACTIV
------------------------------ ------------------------------ ----- -----
WEEKNIGHT_WINDOW TRUE FALSE
WEEKEND_WINDOW TRUE FALSE
2 rows selected.
Windows Groups
BEGIN
DBMS_SCHEDULER.create_window_group (
group_name => 'test_window_group',
window_list => 'test_window_1, test_window_2',
comments => 'A test window group');
END;
/
PL/SQL procedure successfully completed.
A window group is a collection of related windows. It can be created with 0, 1 or many windows as group members using the CREATE_WINDOW_GROUP procedure.
Display window group details.
SELECT window_group_name, enabled, number_of_windowS FROM dba_scheduler_window_groups;
WINDOW_GROUP_NAME ENABL NUMBER_OF_WINDOWS
------------------------------ ----- -----------------
MAINTENANCE_WINDOW_GROUP TRUE 2
TEST_WINDOW_GROUP TRUE 2
2 rows selected.
Windows can be added and removed from a group using the ADD_WINDOW_GROUP_MEMBER and REMOVE_WINDOW_GROUP_MEMBER procedures.
Create a new window.
BEGIN
DBMS_SCHEDULER.create_window (
window_name => 'test_window_3',
resource_plan => NULL,
schedule_name => 'test_hourly_schedule',
duration => INTERVAL '60' MINUTE,
window_priority => 'LOW',
comments => 'Window with a predefined schedule.');
DBMS_SCHEDULER.add_window_group_member (
group_name => 'test_window_group',
window_list => 'test_window_3');
END;
/
PL/SQL procedure successfully completed.
Display window group members.
SELECT window_group_name, window_name FROM dba_scheduler_wingroup_members;
WINDOW_GROUP_NAME WINDOW_NAME
------------------------------ ------------------------------
MAINTENANCE_WINDOW_GROUP WEEKNIGHT_WINDOW
MAINTENANCE_WINDOW_GROUP WEEKEND_WINDOW
TEST_WINDOW_GROUP TEST_WINDOW_1
TEST_WINDOW_GROUP TEST_WINDOW_2
TEST_WINDOW_GROUP TEST_WINDOW_3
5 rows selected.
BEGIN
DBMS_SCHEDULER.remove_window_group_member (
group_name => 'test_window_group',
window_list => 'test_window_3');
END;
/
PL/SQL procedure successfully completed.
Display window group members.
SELECT window_group_name, window_name FROM dba_scheduler_wingroup_members;
WINDOW_GROUP_NAME WINDOW_NAME
------------------------------ ------------------------------
MAINTENANCE_WINDOW_GROUP WEEKNIGHT_WINDOW
MAINTENANCE_WINDOW_GROUP WEEKEND_WINDOW
TEST_WINDOW_GROUP TEST_WINDOW_1
TEST_WINDOW_GROUP TEST_WINDOW_2
4 rows selected.
Window groups can be dropped using the DROP_WINDOW_GROUP procedure.
BEGIN
DBMS_SCHEDULER.drop_window_group (
group_name => 'test_window_group',
force => TRUE);
END;
/
PL/SQL procedure successfully completed.
Display window group details.
SELECT window_group_name, enabled, number_of_windows FROM dba_scheduler_window_groups;
WINDOW_GROUP_NAME ENABL NUMBER_OF_WINDOWS
------------------------------ ----- -----------------
MAINTENANCE_WINDOW_GROUP TRUE 2
1 row selected.
The force option must be used if the window group currently has members.
Enable, Disable and Attributes
All applicable scheduler objects can be enabled and disabled using the overloaded ENABLE and DISABLE procedures.
Enable programs and jobs.
BEGIN
DBMS_SCHEDULER.enable (name => 'test_stored_procedure_prog');
DBMS_SCHEDULER.enable (name => 'test_full_job_definition');
Disable programs and jobs.
DBMS_SCHEDULER.disable (name => 'test_stored_procedure_prog');
DBMS_SCHEDULER.disable (name => 'test_full_job_definition');
END;
/
The values for individual attributes of all scheduler objects can be altered using one of the SET_ATTRIBUTE overloads.
BEGIN
DBMS_SCHEDULER.set_attribute (
name => 'hourly_schedule',
attribute => 'repeat_interval',
value => 'freq=hourly; byminute=30');
END;
/
The values can be set to NULL using the SET_ATTRIBUTE_NULL procedure.
BEGIN
DBMS_SCHEDULER.set_attribute_null (
name => 'hourly_schedule',
attribute => 'repeat_interval');
END;
/
Configuring The Scheduler
- The SCHEDULER_ADMIN role gives a user the ability to control every aspect of the scheduler, as well as generating jobs to run as any other user. For this reason you should avoid granting it to anyone other than trusted DBAs.
- For the majority of users, the CREATE JOB privilege will be sufficient.
- For users requiring some level of scheduler administrative privileges, the MANAGE SCHEDULER privilege allows them to create additional scheduler objects, as well as allowing them to set and retrieve scheduler attributes using the SET_SCHEDULER_ATTRIBUTE and GET_SCHEDULER_ATTRIBUTE procedures.
Extracting DDL
The script used to create scheduler objects can be extracted using the DBMS_METADATA package, as shown in the following example.
CONN test/test
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => 'MY_TEST_JOB',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN NULL; END;',
start_date => TRUNC(SYSDATE),
repeat_interval => 'FREQ=monthly;BYDAY=SUN;BYHOUR=22;BYMINUTE=0;BYSECOND=0');
END;
/
SELECT DBMS_METADATA.get_ddl('PROCOBJ','MY_TEST_JOB', 'TEST') AS job_def FROM dual;
JOB_DEF
--------------------------------------------------------------------------------
BEGIN
dbms_scheduler.create_job('"MY_TEST_JOB"',
job_type=>'PLSQL_BLOCK', job_action=>
'BEGIN NULL; END;'
, number_of_arguments=>0,
start_date=>TO_TIMESTAMP_TZ('04-APR-2012 12.00.00.000000000 AM +01:00','DD-MON-R
RRR HH.MI.SSXFF AM TZR','NLS_DATE_LANGUAGE=english'), repeat_interval=>
'FREQ=monthly;BYDAY=SUN;BYHOUR=22;BYMINUTE=0;BYSECOND=0'
, end_date=>NULL,
JOB_DEF
--------------------------------------------------------------------------------
job_class=>'"DEFAULT_JOB_CLASS"', enabled=>FALSE, auto_drop=>TRUE,comments=>
NULL
);
COMMIT;
END;
Clean up the test job using the floowing.
EXEC DBMS_SCHEDULER.drop_job('MY_TEST_JOB');
Comments
Post a Comment