Should i use SELECT for update NOWAIT ?

Hi:
Do I need to use, in my pl/sql triggers and procedures, the SELECT FOR UPDATE NOWAIT sentence, to avoid locks before using update table sentences ? Is it common to use it on stored procedures and triggers?
Thanks
Joao Oliveira

First, what, exactly do you mean by "avoid locks"? I was interpreting that to mean "I want to avoid creating locks in my session that might block someone else", not "I want to avoid having my SELECT wait for locks to be released-- I want it to fail immediately". If you meant the latter, then SELECT ... FOR UPDATE NOWAIT would be what you want. If you meant the former, then pessimistic locking is not what you want.
Second, what sort of Oracle Forms architecture do you have? Are you still using old-school client-server applications? Or are you using a three-tiered approach? As Tom discusses in that thread, pessimistic locking is only an option when your client application is able to maintain database state across calls (i.e. client/server systems) not when you have stateless connections (which is the norm in the three-tier model). The old client-server versions of Forms would automatically and transparently do pessimistic locking. Since you didn't mention anything about your architecture, most of us probably assumed the more common stateless client architecture (note how Tom's answers progress over the 5 years in that thread as client/server architecture became less and less common).
Third, while your question is appropriate for either the Database - General forum or the SQL and PL/SQL forum, that generally means that you are free to post it either forum, not that it should be posted in both. The vast majority of the folks that hang out in one forum hang out in the other. It's also rather frustrating to answer a post in one forum only to discover that there is another post in a different forum where someone else had already covered the same points half an hour earlier or to discover that there was additional information in another thread that might have changed your answer.
Fourth, if you are going to do pessimistic locking, that requires that you are able to maintain state across various database calls, that you are locking on the lowest possible level of granularity, and that you are able to time out sessions relatively aggressively to ensure that someone doesn't open a record, thereby locking it, go to lunch (or have their system die) and then block everyone else from working. Assuming that is the case, and that you have some reasonable way to handle the error that gets generated other than simply retrying the operation, adding NOWAIT is certainly an option. Most applications, particularly those getting written today, cannot guarantee all these things, so pessimistic locking is generally not appropriate there.
Looking at your other thread (where there is new information that would be useful in this discussion, one of the reasons that multiple threads are generally a bad idea), it seems that you have an ERP application and you are concerned about the performance of entering orders. Obviously, there shouldn't be any locking issues on the ORDER or ORDER_DETAILS tables, assuming that multiple users aren't going to be inserting the same order at the same time. The contention would almost certainly come when multiple orders are trying to update the STOCK and INVENTORY tables, since multiple orders presumably rely on the same rows in those tables. In that case, I'm not sure what adding a NOWAIT would buy you-- unless you were going to roll back the entire order because someone is updating the STOCK row for #2 pencils and your order has an item of #2 pencils, you'd have to keep retrying the operation until you were able to modify the STOCK row, which would be less efficient than just letting that update block until the row was free.
Now, you could certainly redesign the application to minimize that contention by not trying to update what I assume are aggregate tables like STOCK and INVENTORY directly as part of your OLTP processing or, at least, by minimizing the time that you're locking a row. You could, for example, make STOCK and INVENTORY materialized views rather than tables that refresh ON COMMIT, which should decrease the time that your locks are held. You could also have those tables refreshed asynchronously, which would be even more efficient but may require that you reasses your holdback requirements.
Justin

Similar Messages

  • SELECT FOR UPDATE NOWAIT

    Hi everyone!
    I have procedure that looks like this:
    procedure set_expired_users(
        p_QID   integer,
        p_Value integer default 1)
      is
        l_mdate timestamp(3) := null;
      begin
        l_mdate := get_mdate(p_Value);
        MERGE INTO cached_lists cl
        USING (SELECT distinct s.rootof as list_id, p_Value as is_expired, l_mdate as mdate
               FROM   zzz_userrelflat f, zzz_users u, zzz_temp_nn z, speclists s, lists_table lt
               WHERE  u.grp <> 1
                      AND f.userm = u.id
                      AND lt.id = z.id
                      AND f.userg = lt.ra
                      AND z.qid = p_QID
                      AND userm <> -2
                      AND s.ruser = userm) t
        ON (cl.list_id = t.list_id)
        when matched then
          UPDATE SET cl.is_expired = t.is_expired, cl.mdate = decode(t.mdate, null, cl.mdate, t.mdate)
        when not matched then
          INSERT VALUES (t.list_id, t.is_expired, l_mdate);
      end set_expired_users;As you can see there is no commit, commit executes in other stored procedures after that. in some cases I have bottleneck(enq TX), I rewrote procedure:
      procedure set_expired_users(
        p_QID   integer,
        p_Value integer default 1)
      is
        l_mdate timestamp(3) := null;
        busy_lock exception;
        PRAGMA exception_init(busy_lock,-54);
          cursor cur_upd  is
        SELECT 1 from cached_lists cl where cl.list_id in (SELECT distinct s.rootof as list_id
               FROM   zzz_userrelflat f, zzz_users u, zzz_temp_nn z, speclists s, lists_table lt
               WHERE  u.grp <> 1
                      AND f.userm = u.id
                      AND lt.id = z.id
                      AND f.userg = lt.ra
                      AND z.qid = p_QID
                      AND userm <> -2
                      AND s.ruser = userm) FOR UPDATE NOWAIT;
      begin
      open cur_upd;
        l_mdate := get_mdate(p_Value);
       MERGE INTO cached_lists cl
        USING (SELECT distinct s.rootof as list_id, p_Value as is_expired, l_mdate as mdate
               FROM   zzz_userrelflat f, zzz_users u, zzz_temp_nn z, speclists s, lists_table lt
               WHERE  u.grp <> 1
                      AND f.userm = u.id
                      AND lt.id = z.id
                      AND f.userg = lt.ra
                      AND z.qid = p_QID
                      AND userm <> -2
                      AND s.ruser = userm) t
        ON (cl.list_id = t.list_id)
        when matched then
          UPDATE SET cl.is_expired = t.is_expired, cl.mdate = decode(t.mdate, null, cl.mdate, t.mdate)
        when not matched then
          INSERT VALUES (t.list_id, t.is_expired, l_mdate);
            close cur_upd;  
          exception
      when busy_lock then
        null;
      end set_expired_users;Is this a good method to exclude enq TX?
    Sincerely,
    Pavel.

    In my opinion, you shouldn't rewrite that procedure, but you should rethink (ie. streamline) the overall transaction and try to reduce the wait times between the procedure call and the commit/rollback.

  • [ORACLE 9] Select For Update Nowait - Managing the Nowait

    Hi,
    Our application ( Oracle E Business R11) has a Form that allows Updates.
    The problem is, when a user updates a record in the form, we want to prevent another user to open the same form, to avoid locks.
    So I thought that I could
    a) issue a 'select ...for update nowait',
    b) get a code from Oracle
    c )and use this code to prevent the form to be reopened.
    I tried catching the Nowait in the exception section but I cannot get it to work.
    Here is my code:
    declare
      v_var varchar2(40) := '---';
    lv_nom           tmp_delegue_jbm.del_nom%type;
    begin
    v_var := 'Phase 1';
    select   del_nom
    into     lv_nom
    from     tmp_delegue_jbm
    where    del_id = 3
    for update of del_nom nowait;
    dbms_output.put_line( 'Phase:  ' || v_var  );
    v_var := 'Phase 2';
    update tmp_delegue_jbm
    set del_nom = del_nom || ' in'
    where del_id = 3;
    dbms_output.put_line( 'Phase:  ' || v_var  );
    exception
       when others then -- Should deal with the nowait situation
             dbms_output.put_line( 'Error:  ' || v_var || ' - ' || sqlerrm );
    end;
    [End Code]
    Many thanks                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    Far easier to poke a value into memory using DBMS_APPLICATION_INFO.SET_ACTION or SET_MODULE and have any session calling the form check first to see of another session has it already opened. Just make sure your exception handlers clear the lock.
    http://www.morganslibrary.org/reference/dbms_applic_info.html

  • JDBC Thin: Can use "select for update"?

    Hello,
    Can anyone tell me if/how can I solve the concurrency
    upon a result set?
    JDK 1.2 documentation contains the following Connection's
    method:
    "public Statement createStatement(int resultSetType,
    int resultSetConcurrency)
    throws SQLException
    JDBC 2.0 Creates ..."
    Thanks,
    Alexandra
    null

    We don't have support for JDBC 2.0 yet.
    You can use SQL with 'for update' clause in the present
    8i drivers. However, make sure that autoCommit
    is OFF
    Alexandra (guest) wrote:
    : Hello,
    : Can anyone tell me if/how can I solve the concurrency
    : upon a result set?
    : JDK 1.2 documentation contains the following Connection's
    : method:
    : "public Statement createStatement(int resultSetType,
    : int resultSetConcurrency)
    : throws SQLException
    : JDBC 2.0 Creates ..."
    : Thanks,
    : Alexandra
    Oracle Technology Network
    http://technet.oracle.com
    null

  • How to avoid 'select for update'

    Hi,
    we are using the bc4j framework of jdev 3.2.3. We have a View which collects data from several tables in different database schemes, so we naturally have to use the 'union clause'. But if we try to make an update on a row (via 'setAttribute'), we got a DMLException ORA-02014 telling us that we should not use 'select for update' on views whith unions.
    Since i cannot avoid using a union clause, is there a way to avoid the 'select for update' in the bc4j framework?
    Please help, its urgent!
    Thanx,
    Dietmar

    SELECT FOR UPDATE is used for our implementation or row-level locking.
    If you are using pessimistic locking mode, this will occur the first time any attribute is modified.
    If you are using optimistic locking mode, it will be deferred until post/commit time.
    If you are using "none" locking mode, it will not happen in your application may hang indefinitely if another session has locked the row.
    Are you asking how to avoid locking?
    Do you mean to be updating this view with a union over multiple databases?

  • OpenSQL DataSource not allowed if select-for-update is used Error?

    Hi,
    I have created an enterprise application, EJB (Session + entity) + WAR, and corresponding ear file deployes with the following warning message, I have done similar apps in the past without erros.
    Warning: DataSource TMP_EMPLOYEES_DATA is OpenSQL. It is used by an abstract schema that uses select-for-update locking. It is now allwed to use OpenSQL DataSource if select-for-updaet is used.
    Any idea?
    Thanks

    Hi Ezatullah, all,
    while I do not propose an alternative solution, I'd like to add some explanation to the error message itself: Open SQL for Java strives to provide portable semantics across the set of supported databases and does not offer features which can not be provided by all the databases.
    Now, a SELECT .. FOR UPDATE in the semantics as used here is not generally available, in particular not regarding the locking semantics.
    Thus, the feature is rejected in combination with a Open SQL/JDBC data source.
    Best Regards, Dietmar

  • Performance of using a Select For Update vs a correlated subquery

    I was wondering wether or not it is more effecient to use the
    Select ... For Update (with a cursor etc.) versus a correlated
    subquery.
    I can accomplish the same thing with either however performance
    at our site is an issue.

    Use select for update cursor as that is faster as it updates
    based on the rowid. One thing to keep in mind is that rowid is
    session specific and the rows to be updated get locked so that
    nobody else can update them till the lock is released. I have
    had very good performance results with these cursors.
    Good luck !
    Sudha

  • FOR UPDATE NOWAIT Fails to Detect Lock

    Locking a bitmap indexed row would cause other rows locked. I heard that, if FOR UPDATE NOWAIT is used on these accidentally locked rows (Oracle SQL High Performance Turning by Prentice hall), it may not be able to detect the lock. Is it true? I cannot find related documenation from Oracle's manual. And, what should we do to prevent an incorrect lock status returned by FOR UPDATE NOWAIT?

    SELECT FOR UPDATE NOWAIT detects locks affected DATA blocks.
    Look the example below:
    SQL> create table t1 (id number, bit_col number);
    Table created.
    SQL> begin
      2  insert into t1 values(0,1);
      3  insert into t1 values(1,1);
      4  insert into t1 values(2,2);
      5  insert into t1 values(3,3);
      6  insert into t1 values(4,4);
      7  end;
      8  /
    PL/SQL procedure successfully completed.
    SQL> commit;
    Commit complete.
    SQL> create bitmap index t1_bit on t1(bit_col);
    Index created.Now in session 1 we change the bitmap-indexed column and it affects
    index node:
    SQL> update t1
      2  set bit_col = 4
      3  where id = 2;
    1 row updated.In accordance to bitmap index structure this operator locks the index section
    the locked row pertains to:
    2th session waits for the lock release even when it tries to lock another row -
    two rows pertain to the same index section which is locked by the first session:
    SQL> update t1
      2  set bit_col = 2
      3  where id = 3;After rollback in the first session the second one gets the resource:
    SQL> update t1
      2  set bit_col = 2
      3  where id = 3;
    1 row updated.Now lets do rollback in both and repeate the first UPDATE in the first session:
    SQL> update t1
      2  set bit_col = 4
      3  where id = 2;
    1 row updated.In the second session we can lock the row (not index section) using
    SELECT FOR UPDATE:(in contrast with UPDATE statement which changes
    indexed column):
    SQL> select * from t1 where id=3 for update nowait;
            ID    BIT_COL
             3          3But certainly we detect row-level lock in the data block for ID = 2:
    SQL> select * from t1 where id=2 for update nowait;
    select * from t1 where id=2 for update nowait
    ERROR at line 1:
    ORA-00054: resource busy and acquire with NOWAIT specifiedRgds.

  • Database select for update locks ADF

    Hi,
    When a user has initiated an update session in an adf application and locking is optimistic it will acquire a lock on table row using select for update no wait; . But when the user closes a tab the session would not be terminated. Now i know as HTTP is a stateless protocol, we can wait for the timeout and then the lock will be released using a session listener implementation. But if the user instead tries to log in again in a new tab and tries to edit the same record he will receive a message stating that another user already holds a lock on the record which is correct, but is misleading.
    So can we rely on javascript for these scenarios that as soon as the user closes the tab the session should be terminated.
    Here's a snippet
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/dojo/1.4/dojo/dojo.xd.js" ></script>
    <script type="text/javascript">
    var unLoad = function() {
        dojo.io.script.get({
        url:'http://127.0.0.1:7101/myapp/adfAuthentication?logout=true',
        timeout:15000,
      dojo.addOnWindowUnload(unLoad);
    </script>I know this might not work always as it depends on the fact that request might / might not be processed by the server.
    Are there any alternate solutions and also reducing the session timeout is ruled out in my scenario.

    Ramandeep,
    So are there other alternatives or solutionsAlternatives or solutions to what, exactly? As Jobinesh has told you, as long as you use optimistic locking, ADF doesn't acquire database locks except in the context of a transaction that is going to be completed in the current HTTP request. You could obviously force ADF to deviate from this if you called "postChanges" during an HTTP request and leave the transaction hanging, but that would just be wrong in an optimistic locking scenario - the solution would be "don't do that."
    John

  • Inconsistent Locking with Select for Update

    Hi,
    I seem to be having some issues in using SELECT FOR UPDATE and was hoping to get some insight from the Oralce Guru's out there.
    I have a J2EE application, running in WebLogic 8.1.4 using Oralce 9.2.0.1.0.
    The application contains code that requires locking to be done on a specific table with multiple transactions (tx) requesting the same lock. Eg:
    Tx 1: Select * from Zone where Zoneid = 'Zone1' for update (Obtains lock)
    Tx 2: Select * from Zone where Zoneid = 'Zone1' for update (waits)
    Tx 100: Select * from Zone where Zoneid = 'Zone1' for update
    Tx1 commits.
    It appears that the following transactions, i.e. Tx2 - Tx100 do not seem to execute in the order the lock was requested. That is Tx 100 always appears to be the second last transaction to execute, after which some arbitrary transaction between Tx2 - Tx99 will execute after Tx100 has committed.
    This seems to tell me that the lock is not being handed in a FIFO manner and is causing us great pain as our data is not longer consistent.
    Does anyone know how i might be able to trace which transaction is being awarded the lock? Also if anyone has any suggestion on how to troubleshoot/solve this issue, greatly appreciated.
    TIA
    Prem

    Oracle does not have a lock queue/manager at all. The locked status of a record is essentially an attribute of the record itself. It is stored on the datablock header. When a transaction requests a lock and can't get it, and is willing to wait (SELECT FOR UPDATE without NOWAIT), it first spins while waiting for the lock (four times as I recall), then sleeps waiting for the lock. The the more times it sleeps before getting the lock, the longer it will sleep before trying again.
    What is likely happening here is that transaction 100 is still spinning when transaction 1 commits, so checks back more frequently and gets the lock first. The rest get the lock whenever they wake up and noone else has taken the lock.
    If you need the transaction to occur in order, then I do not think you can use Oracle's native locking mechanism. Depending on what exactly you are trying to do, you may want to look at Advanced Queueing, or possibly the built-in package DBMS_LOCK.
    HTH
    John

  • FOR UPDATE NOWAIT   and  ora-00054

    Dears
    my scenario is :-
    In a busy transaction system,in a procedure i am making first lock the transaction with FOR UPDATE NOWAIT in a query.
    Then I am checking some status and then i am doing the transaction.
    It was ok but sometimes i am facing "ORA-00054: Resource Busy and Acquire with NOWAIT Specified" this error.
    so, now how can i overcome this. or any alternative way please?
    Regards
    Halim
    Edited by: Abdul Halim on Mar 1, 2011 1:11 PM

    Abdul Halim wrote:
    Dears
    my scenario is :-
    In a busy transaction system,in a procedure i am making first lock the transaction with FOR UPDATE NOWAIT in a query.
    Then I am checking some status and then i am doing the transaction.
    It was ok but sometimes i am facing "ORA-00054: Resource Busy and Acquire with NOWAIT Specified" this error.
    so, now how can i overcome this. or any alternative way please?
    Regards
    Halim
    Edited by: Abdul Halim on Mar 1, 2011 1:11 PMThis is normal behavior.So when you use SELECT FOR UPDATE statement in this case oracle will exclusive row level lock according records.And if you use NOWAIT clause in this case oracle will not wait if these records/table locked by another users.In additionally you can use without NOWAIT clause then other users will wait(without errors) also you can set limit this waiting using DISTRIBUTED_LOCK_TIMEOUT initialization parameter.

  • Oracle select for update: not releasing lock

    My JDBC code uses "select for update" to modify record in Oracle database. I tried to simulate network connection down situation.
    After my JDBC code locked on a record using "select for update", I unplugged the network cable. I then tried to run the same code on another computer, but it could not accquire the lock, because the previous lock was not released. I tried sqlplus to lock the record, but failed also. The lock has been there for at least an hour now. I guess it may finally be released, but is there a way for oracle to release the lock as soon as the connection is down? Not know if it is a JDBC setting or oracle setting.

    Dear Friend,
    What you are trying to do is not correct way of checking the concurrency and transaction.
    The reason is as listed below.
    01.Always remember http is a stateless protocol and removing the connection or just closing the browser will never be informed to the database or to the application server thus the transaction monitor (TM)or processor will never release the lock as it will deem that the actor is manipulating the data.
    02.As per locking goes every database is having a �TM� and the default isolation level setting is different like oracle uses serializable DB2udb 7.0 or db2/As400 uses repeatable read. You can change this setting by editing the default setting in the database but be very sure before touching it.
    03.     You can also transpose this with your Application server setting for that piece of code or Globally but again be very sure about it as it will change the entire gamete.
    04.     For releasing lock you have to manually do it or you can change the settings of App server or the Database to release the connection after some wait time.
    Regards,
    Ruchir

  • Select for update on stateless connections

    i have read that using select for update for a web application will not work specially if the stateless connection are used , and the best way to make sure that the column you are reading was not changed is to use the time stamp approach and not select for update?? i am right

    Not entirely correct.
    A connection/session to Oracle has state. There is no such thing as a stateless Oracle connection.
    The stateless connection is from the web browser to the web server. It makes a connection. Asks for a page (GET, PUT or POST typically). It gets a response from the web server. It closes that connection.
    When the web server response, it opens a (stateful) connection to Oracle. Or it re-uses an existing (stateful) Oracle connection (now idle after having serviced another web browser/web server request).
    The problem with pessimistic locking (e.g. SELECT FOR UPDATE) is that the very same (stateful) Oracle session will either
    a) be closed when a response is send to the web browser
    b) be used for another totally different web browser
    Thus any locks made will either be lost (option a) or will get used by the wrong web browser (option b).
    A method is therefore needed to make the lock spans different Oracle (stateful) sessions. Web browser selects rows to update using Oracle session 101 at Time 1. Web browser submits updated rows and a commit using Oracle session 142 at Time 2.
    The "best way" to handle optimistic locking is likely using the Oracle System Change Number (SCN). This represents the "current version" of the rows.
    So at Time 1, via Session 101, you give the web browser a 100 rows to update - together with the SCN that says the current version of the rows are v1.2.0.1.
    At Time 2, using Session 142, the web browser submits its changes for those 100 rows. Together with the SCN v1.2.0.1.
    The SQL UPDATE issues the update statement - but adds the SCN version criteria. If the UPDATE fails to update all 100 rows, it means that some of the rows no longer exist, or that some rows have a new version number (was changed in the meantime).
    In this case, the UPDATE is rolled back and an exception raised to tell the web browser that some (or all) of those 100 rows have been changed in the meantime.
    Refer to the [url http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/pseudocolumns007.htm#BABFAFIC]Oracle® Database SQL Reference for details.

  • Difference between FOR UPDATE NOWAIT and no FOR UPDATE NOWAIT

    Hi,
    I have a quick question relating to the difference in SELECT statements and the use of FOR UPDATE NOWAIT.
    Example:
    If I query the following
    SELECT * FROM ZX_LINES WHERE APPLICATION_ID = :B4 AND ENTITY_CODE = :B3 AND EVENT_CLASS_CODE = :B2 AND TRX_ID = :B1
    / with binds parsed, the result in Autotrace is as follows:
    consistent gets     87
    consistent gets - examination     2
    consistent gets from cache     87
    However, if I query the following
    SELECT * FROM ZX_LINES WHERE APPLICATION_ID = :B4 AND ENTITY_CODE = :B3 AND EVENT_CLASS_CODE = :B2 AND TRX_ID = :B1
    FOR UPDATE NOWAIT
    / the results in Autotrace are:
    consistent gets     4341
    consistent gets - examination     2
    consistent gets from cache     4341
    db block changes     5894
    db block gets     5920
    db block gets from cache     5920
    So, I'm trying to understand why it happens that a FOR UPDATE NOWAIT requires so many more consistent gets, as opposed to not using a FOR UPDATE?

    >
    So, I'm trying to understand why it happens that a FOR UPDATE NOWAIT requires so many more consistent gets, as opposed to not using a FOR UPDATE?
    >
    The main difference is that when you use FOR UPDATE Oracle has to LOCK each row.
    How many rows will your query return?
    Did you notice that in the second example ALL of the consistent gets were from the cache?
    Did you notice all of the block gets? And that ALL of them were from the cache?

  • Any parameter is required to set "select for Update"

    Hi all,
    For using "Select for Update" statement, is any parameter is required to set at the database level.
    Thanks in advance,

    Hi,
    I did't get any problem. but before implementing that I searching for any overheads.
    I had read that some transaction isolation level should be required to use "select for update".
    I did't catch it clearly.
    Can u explain briefly if you know/any body know.

Maybe you are looking for

  • How do I use an SMB to store my videos?

    SUNY Downstate wants to move the videos in iTunes U to an SMB machine, how would we change the iTunes U .XML files to reflect it? TIA

  • Select iPhoto libraries in Media?

    I have several iPhoto libraries. Media is always showing the same library. How can I select a different one ? Francois

  • Background Video In Idvd

    I made a dvd and was able to export a clip from imovie to use as the background video in idvd. I burned the dvd and it looks great. I then updated most of the Imac software from apple. And now I can not find the settings for exporting a clip. The set

  • Cannot get sound to go through earphones

    plug them in and sound / music is played from phon'es speaker, not earphones.

  • SXMB_MONI Q Status

    In file to file scenario at SXMB_MONI it displays the status as green and in the Q.Status it says STOPED. what is this error how to release that msgs