DB2 LUW中DBMS_JOB并行执行及会话释放问题求助
Alright, let's tackle your two DB2 LUW job scheduling issues step by step—they're both rooted in how the database handles background job execution and session behavior:
If your jobs are running one after another instead of side by side, here's what you need to fix:
Increase the Job Queue Process Limit
The biggest reason for serial execution is usually the default job queue capacity. DB2 LUW uses theJOB_QUEUE_PROCESSESparameter to control how many concurrent background jobs can run. By default, this is often set to 1 (forcing one job at a time). Adjust it to a value that matches your system's resource capacity (start with 4-8 if you're unsure):ALTER SYSTEM SET JOB_QUEUE_PROCESSES = 6 SCOPE=BOTH;This change takes effect immediately for new jobs, so you don't need to restart the database.
Make Sure Jobs Don't Block Each Other
Even with parallel processes enabled, jobs will run serially if they're fighting for locks. Since you're targeting different large tables, double-check:- Each job operates on a distinct table (or non-overlapping partitions) so they don't wait for table-level locks.
- Your stored procedure uses row-level locks instead of table locks wherever possible (avoid
LOCK TABLEstatements unless absolutely necessary).
Submit Jobs Asynchronously (Skip DBMS_JOB.RUN for Immediate Dispatch)
If you're callingDBMS_JOB.RUNright after submitting each job, that's forcing your session to wait for that single job to finish before moving to the next. Instead, let the job queue handle execution automatically by settingnext_datetoSYSDATEwhen submitting:DECLARE v_job_id NUMBER; BEGIN -- Submit job for table1 DBMS_JOB.SUBMIT( job => v_job_id, what => 'CALL your_table_proc(''table1'');', next_date => SYSDATE, interval => NULL -- No repeat needed since this is a one-time job ); COMMIT; -- Critical to finalize the job submission DBMS_OUTPUT.PUT_LINE('Submitted job ID: ' || v_job_id); -- Repeat this block for table2, table3, etc. END; /Each job gets added to the queue, and the database will run them in parallel as soon as processes are available.
Upgrade to DBMS_SCHEDULER (Better Parallel Control)
DBMS_JOBis a legacy package—DB2 LUW'sDBMS_SCHEDULERgives you far more control over parallel execution. You can create a dedicated job class with a defined parallelism level, then assign all your table-processing jobs to it:-- Create a job class with parallelism enabled BEGIN DBMS_SCHEDULER.CREATE_JOB_CLASS( job_class_name => 'PARALLEL_TABLE_JOBS', parallelism => 6 -- Number of concurrent jobs allowed ); END; / -- Submit a job to the class DECLARE v_job_name VARCHAR2(100) := 'PROCESS_TABLE1_JOB'; BEGIN DBMS_SCHEDULER.CREATE_JOB( job_name => v_job_name, job_type => 'STORED_PROCEDURE', job_action => 'your_table_proc', number_of_arguments => 1, job_class => 'PARALLEL_TABLE_JOBS', enabled => TRUE ); DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE(v_job_name, 1, 'table1'); COMMIT; END; /This lets you manage resource allocation for your parallel jobs more effectively.
The key here is to avoid forcing your session to wait for job execution:
Stop Using DBMS_JOB.RUN for Synchronous Execution
DBMS_JOB.RUNwill block your session until the job finishes if you call it immediately after submission. Instead, rely on the job queue to run jobs in the background. When you submit a job withnext_date => SYSDATE, it's added to the queue and your session gets control back right after you commit.Remove Any Custom Waiting Logic
If your code includes loops that check the job status (e.g., queryingUSER_JOBSuntil the job shows as completed), delete that code. Let the jobs run in the background while you use your session for other tasks.Always Commit After Submitting Jobs
Forgetting to commit afterDBMS_JOB.SUBMITorDBMS_SCHEDULER.CREATE_JOBcan leave the job in a pending state, making your session appear to hang. A commit finalizes the job submission and sends it to the queue immediately.
To verify parallel execution is working, you can check the status of your jobs with this query:
SELECT job, what, status, this_date FROM USER_JOBS;
Look for multiple jobs with status = 'RUNNING' to confirm they're executing in parallel.
内容的提问来源于stack exchange,提问作者Vivek Harry




