在 Oracle PL/SQL 中等待提交的作业完成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4599630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 22:20:20  来源:igfitidea点击:

Waiting for a submitted job to finish in Oracle PL/SQL?

multithreadingoracleplsql

提问by vicsz

I'm looking for the equivalent of Java's thread.join() in PL/SQL. I.e. I want to kick off a number of jobs (threads), and then wait for them to finish.

我在 PL/SQL 中寻找 Java 的 thread.join() 等价物。即我想启动一些作业(线程),然后等待它们完成。

How is this possible in PL/SQL?

这在 PL/SQL 中怎么可能?

I'm thinking of using dbms_job.submit (I know it's deprecated). dbms_scheduler is also an alternative.

我正在考虑使用 dbms_job.submit (我知道它已被弃用)。dbms_scheduler 也是一种选择。

My code:

我的代码:

DECLARE
  jobno1 number;
  jobno2 number;
BEGIN

  dbms_job.submit(jobno1,'begin dbms_lock.sleep(10); dbms_output.put_line(''job 1 exit'');end;');
  dbms_job.submit(jobno2,'begin dbms_lock.sleep(10); dbms_output.put_line(''job 2 exit'');end;');

  dbms_job.run(jobno1);
  dbms_job.run(jobno2);

  //Need code to Wait for jobno1 to finish
  //Need code to Wait for jobno2 to finish

END;

回答by Adam Hawkes

This is accomplished using Chains in the DBMS_SCHEDULERjob scheduling. The Oracle Documentationhas examples which accomplish what you want. Basically, you define steps, small portions of code, with an execution structure. In your example, your first step would be a start step (which would typically do initialization), then you'd have two steps which run in parallel (jobno1 and jobno2), and then a final step which would activate once both parallel jobs complete.

这是在DBMS_SCHEDULER作业调度中使用链来完成的。在Oracle文档有你想要什么可完成的例子。基本上,您使用执行结构定义步骤、代码的一小部分。在您的示例中,您的第一步将是开始步骤(通常会进行初始化),然后您将有两个并行运行的步骤(jobno1 和 jobno2),最后一步将在两个并行作业完成后激活.

回答by Mike Meyers

I like the solution from Adam Hawkes using DBMS_SCHEDULER chains. Didn't know about that since I'm still using DBMS_JOB and not having rewritten code yet.

我喜欢 Adam Hawkes 使用 DBMS_SCHEDULER 链的解决方案。不知道这一点,因为我仍在使用 DBMS_JOB 并且还没有重写代码。

Anyway... the solution I currently use for this is a combination of DBMS_JOB (although you should probably use DBMS_SCHEDULER since DBMS_JOB is deprecated as you noted) and DBMS_ALERT.

无论如何...我目前使用的解决方案是 DBMS_JOB(尽管您可能应该使用 DBMS_SCHEDULER,因为 DBMS_JOB 已被弃用)和 DBMS_ALERT。

Jobs are created using DBMS_JOB. Then we wait for the jobs to complete using dbms_alert.register and dbms_alert.waitany. Each job when it completes uses dbms_alert.signal. There may be a problem if the job completes and signals before the parent is ready but I'm sure you could work around that.

作业是使用 DBMS_JOB 创建的。然后我们使用 dbms_alert.register 和 dbms_alert.waitany 等待作业完成。每个作业完成时都使用 dbms_alert.signal。如果作业在父级准备就绪之前完成并发出信号,则可能会出现问题,但我相信您可以解决这个问题。

I'm guessing that the DBMS_SCHEDULER chains are probably the way you should do this now but am just adding my answer for completeness.

我猜 DBMS_SCHEDULER 链可能是您现在应该这样做的方式,但我只是为了完整性而添加我的答案。