• <legend id='PHOsH'><style id='PHOsH'><dir id='PHOsH'><q id='PHOsH'></q></dir></style></legend>
      1. <tfoot id='PHOsH'></tfoot>
        <i id='PHOsH'><tr id='PHOsH'><dt id='PHOsH'><q id='PHOsH'><span id='PHOsH'><b id='PHOsH'><form id='PHOsH'><ins id='PHOsH'></ins><ul id='PHOsH'></ul><sub id='PHOsH'></sub></form><legend id='PHOsH'></legend><bdo id='PHOsH'><pre id='PHOsH'><center id='PHOsH'></center></pre></bdo></b><th id='PHOsH'></th></span></q></dt></tr></i><div id='PHOsH'><tfoot id='PHOsH'></tfoot><dl id='PHOsH'><fieldset id='PHOsH'></fieldset></dl></div>

        <small id='PHOsH'></small><noframes id='PHOsH'>

          <bdo id='PHOsH'></bdo><ul id='PHOsH'></ul>
      2. PL/SQL 过程的同步.如何保证一次只执行一个程序?

        Synchronisation of PL/SQL procedure. How to guaranty execution of procedure only one at time?(PL/SQL 过程的同步.如何保证一次只执行一个程序?)

          • <tfoot id='QpHUw'></tfoot>
              <bdo id='QpHUw'></bdo><ul id='QpHUw'></ul>
                <legend id='QpHUw'><style id='QpHUw'><dir id='QpHUw'><q id='QpHUw'></q></dir></style></legend>

                <small id='QpHUw'></small><noframes id='QpHUw'>

                  <tbody id='QpHUw'></tbody>

                  <i id='QpHUw'><tr id='QpHUw'><dt id='QpHUw'><q id='QpHUw'><span id='QpHUw'><b id='QpHUw'><form id='QpHUw'><ins id='QpHUw'></ins><ul id='QpHUw'></ul><sub id='QpHUw'></sub></form><legend id='QpHUw'></legend><bdo id='QpHUw'><pre id='QpHUw'><center id='QpHUw'></center></pre></bdo></b><th id='QpHUw'></th></span></q></dt></tr></i><div id='QpHUw'><tfoot id='QpHUw'></tfoot><dl id='QpHUw'><fieldset id='QpHUw'></fieldset></dl></div>
                1. 本文介绍了PL/SQL 过程的同步.如何保证一次只执行一个程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  某些 PL/SQL 过程需要花费大量时间进行评估(如重新计算和更新大量数据).

                  Some PL/SQL procedure take a lot of time for evaluation (as recalculate and update a lot of data).

                  我想将评估此过程的执行上下文的数量限制为单个.

                  I want to limit numbers of execution context which evaluate this procedure to single.

                  如果其他上下文执行它,则什么都不做...

                  Just do nothing if some another context execute it...

                  但我不知道在 PL/SQL 代码中使用哪些原子操作.

                  But I don't know which atomic operations to use in PL/SQL code.

                  注意:如果服务器在过程评估中间停止,我担心使用表行进行同步会导致数据库中的状态不一致......

                  Note: I afraid that using table row for synchronization can lead to inconsistent state in DB if server halted on middle of procedure evaluation...

                  推荐答案

                  您可以使用 DBMS_LOCK.request 生成唯一的锁句柄.只有一个会话可以同时持有这个锁.如果会话的数据库重启意外结束,锁会自动释放.

                  You can use DBMS_LOCK.request to generate a unique lock handle. Only one session can hold this lock at the same time. If the database restarts of the session ends unexpectedly, the lock will be released automatically.

                  您在请求锁时决定是否跨提交持有锁.

                  You decide when requesting the lock if the lock will be held across commits or not.

                  这是一个例子:

                  SQL> CREATE OR REPLACE PROCEDURE serial IS
                    2     l_lock_handle  VARCHAR2(128 BYTE);
                    3     l_lock_request INTEGER;
                    4  BEGIN
                    5     dbms_lock.allocate_unique(lockname => 'MY_SERIAL_PROC',
                    6                               lockhandle => l_lock_handle);
                    7     l_lock_request := dbms_lock.request(lockhandle => l_lock_handle,
                    8                                         timeout => 5,
                    9                                         release_on_commit => FALSE);
                   10     CASE l_lock_request
                   11        WHEN 0 THEN
                   12           NULL; -- success
                   13        WHEN 1 THEN
                   14           raise_application_error(-20002, 'lock already reserved');
                   15        ELSE
                   16           raise_application_error(-20001, 'Lock error: ' || l_lock_request);
                   17     END CASE;
                   18     BEGIN
                   19        ---------- serialized block of code           ----------
                   20        ---------- (lock will be kept accross commit) ----------
                   21        dbms_lock.sleep(30);
                   22        ---------- End of serialized code             ----------
                   23     EXCEPTION
                   24        WHEN OTHERS THEN -- release lock in case of uncatched error
                   25           l_lock_request := dbms_lock.release(lockhandle => l_lock_handle);
                   26           RAISE;
                   27     END;
                   28     l_lock_request := dbms_lock.release(lockhandle => l_lock_handle);
                   29  END;
                   30  /
                  
                  Procedure created
                  

                  我将同时运行两个会话:

                  I'll run two sessions at the same time:

                  Session A> exec serial;                
                  
                                                         Session B> -- Before session A ends
                                                         Session B> exec serial;
                  
                                                         ERROR at line 1:
                                                         ORA-20002: lock already reserved
                                                         ORA-06512: at "APPS.SERIAL", line 13
                                                         ORA-06512: at line 1
                  
                  
                  PL/SQL procedure successfully completed
                  
                                                         Session B> -- After session A ends
                                                         Session B> exec serial;
                  
                                                         PL/SQL procedure successfully completed.
                  

                  这篇关于PL/SQL 过程的同步.如何保证一次只执行一个程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to import CSV into sqlite using RSqlite?(如何使用 RSqlite 将 CSV 导入 sqlite?)
                  How do you import a large MS SQL .sql file?(如何导入大型 MS SQL .sql 文件?)
                  SQL Server SMO complains of missing DLL(SQL Server SMO 抱怨缺少 DLL)
                  What#39;s the difference between VARCHAR(255) and TINYTEXT string types in MySQL?(MySQL 中的 VARCHAR(255) 和 TINYTEXT 字符串类型有什么区别?)
                  Common MySQL fields and their appropriate data types(常见的 MySQL 字段及其相应的数据类型)
                  How to call Oracle function or stored procedure using spring persistence framework?(如何使用 Spring 持久化框架调用 Oracle 函数或存储过程?)

                    <small id='Doyur'></small><noframes id='Doyur'>

                    <legend id='Doyur'><style id='Doyur'><dir id='Doyur'><q id='Doyur'></q></dir></style></legend>
                    <tfoot id='Doyur'></tfoot>

                        <tbody id='Doyur'></tbody>
                          • <bdo id='Doyur'></bdo><ul id='Doyur'></ul>
                            <i id='Doyur'><tr id='Doyur'><dt id='Doyur'><q id='Doyur'><span id='Doyur'><b id='Doyur'><form id='Doyur'><ins id='Doyur'></ins><ul id='Doyur'></ul><sub id='Doyur'></sub></form><legend id='Doyur'></legend><bdo id='Doyur'><pre id='Doyur'><center id='Doyur'></center></pre></bdo></b><th id='Doyur'></th></span></q></dt></tr></i><div id='Doyur'><tfoot id='Doyur'></tfoot><dl id='Doyur'><fieldset id='Doyur'></fieldset></dl></div>