Problem with using “Optimistic locking” I can not update after viewing the table










1














I'm add tcn in table:



alter table accounts add TCN integer;


And UPDATE accounts set TCN = DBMS_UTILITY.GET_TIME;



And tcn can not be null alter table accounts modify TCN not null;



I created trigger before update



SQL> create or replace trigger acc_preupdate
2 before update on accounts
3 for each row
4 begin
5 if(:NEW.TCN != :OLD.TCN+1) THEN
6 RAISE_APPLICATION_ERROR(-20000, 'Concurrency Failure');
7 end if;
8 :NEW.TCN := DBMS_UTILITY.GET_TIME;
9 END;
10 /


And trigger before insert



SQL> create or replace trigger acc_preinsert
2 before insert on accounts
3 for each row
4 begin
5 :NEW.TCN := DBMS_UTILITY.GET_TIME;
6 end;
7 /


I insert in table



SQL> insert into accounts (acc_id, acc_name, acc_amount, acc_date)
2 values(acc_seq.nextval, 'petar', 15000, sysdate);


And when I want to update the table, I get this error



SQL> update accounts
2 set acc_amount = 1000000
3 where acc_id = 1;
update accounts
*
ERROR at line 1:
ORA-20000: Concurrency Failure
ORA-06512: at "PETAR1.ACC_PREUPDATE", line 3
ORA-04088: error during execution of trigger 'PETAR1.ACC_PREUPDATE'


now I select users with id 1.



select acc_name, acc_amount from accounts where acc_id = 1;


And when I try to update the table again, I get the same error.



Why can not I update the table?



I use Oracle 12c r2










share|improve this question




























    1














    I'm add tcn in table:



    alter table accounts add TCN integer;


    And UPDATE accounts set TCN = DBMS_UTILITY.GET_TIME;



    And tcn can not be null alter table accounts modify TCN not null;



    I created trigger before update



    SQL> create or replace trigger acc_preupdate
    2 before update on accounts
    3 for each row
    4 begin
    5 if(:NEW.TCN != :OLD.TCN+1) THEN
    6 RAISE_APPLICATION_ERROR(-20000, 'Concurrency Failure');
    7 end if;
    8 :NEW.TCN := DBMS_UTILITY.GET_TIME;
    9 END;
    10 /


    And trigger before insert



    SQL> create or replace trigger acc_preinsert
    2 before insert on accounts
    3 for each row
    4 begin
    5 :NEW.TCN := DBMS_UTILITY.GET_TIME;
    6 end;
    7 /


    I insert in table



    SQL> insert into accounts (acc_id, acc_name, acc_amount, acc_date)
    2 values(acc_seq.nextval, 'petar', 15000, sysdate);


    And when I want to update the table, I get this error



    SQL> update accounts
    2 set acc_amount = 1000000
    3 where acc_id = 1;
    update accounts
    *
    ERROR at line 1:
    ORA-20000: Concurrency Failure
    ORA-06512: at "PETAR1.ACC_PREUPDATE", line 3
    ORA-04088: error during execution of trigger 'PETAR1.ACC_PREUPDATE'


    now I select users with id 1.



    select acc_name, acc_amount from accounts where acc_id = 1;


    And when I try to update the table again, I get the same error.



    Why can not I update the table?



    I use Oracle 12c r2










    share|improve this question


























      1












      1








      1


      1





      I'm add tcn in table:



      alter table accounts add TCN integer;


      And UPDATE accounts set TCN = DBMS_UTILITY.GET_TIME;



      And tcn can not be null alter table accounts modify TCN not null;



      I created trigger before update



      SQL> create or replace trigger acc_preupdate
      2 before update on accounts
      3 for each row
      4 begin
      5 if(:NEW.TCN != :OLD.TCN+1) THEN
      6 RAISE_APPLICATION_ERROR(-20000, 'Concurrency Failure');
      7 end if;
      8 :NEW.TCN := DBMS_UTILITY.GET_TIME;
      9 END;
      10 /


      And trigger before insert



      SQL> create or replace trigger acc_preinsert
      2 before insert on accounts
      3 for each row
      4 begin
      5 :NEW.TCN := DBMS_UTILITY.GET_TIME;
      6 end;
      7 /


      I insert in table



      SQL> insert into accounts (acc_id, acc_name, acc_amount, acc_date)
      2 values(acc_seq.nextval, 'petar', 15000, sysdate);


      And when I want to update the table, I get this error



      SQL> update accounts
      2 set acc_amount = 1000000
      3 where acc_id = 1;
      update accounts
      *
      ERROR at line 1:
      ORA-20000: Concurrency Failure
      ORA-06512: at "PETAR1.ACC_PREUPDATE", line 3
      ORA-04088: error during execution of trigger 'PETAR1.ACC_PREUPDATE'


      now I select users with id 1.



      select acc_name, acc_amount from accounts where acc_id = 1;


      And when I try to update the table again, I get the same error.



      Why can not I update the table?



      I use Oracle 12c r2










      share|improve this question















      I'm add tcn in table:



      alter table accounts add TCN integer;


      And UPDATE accounts set TCN = DBMS_UTILITY.GET_TIME;



      And tcn can not be null alter table accounts modify TCN not null;



      I created trigger before update



      SQL> create or replace trigger acc_preupdate
      2 before update on accounts
      3 for each row
      4 begin
      5 if(:NEW.TCN != :OLD.TCN+1) THEN
      6 RAISE_APPLICATION_ERROR(-20000, 'Concurrency Failure');
      7 end if;
      8 :NEW.TCN := DBMS_UTILITY.GET_TIME;
      9 END;
      10 /


      And trigger before insert



      SQL> create or replace trigger acc_preinsert
      2 before insert on accounts
      3 for each row
      4 begin
      5 :NEW.TCN := DBMS_UTILITY.GET_TIME;
      6 end;
      7 /


      I insert in table



      SQL> insert into accounts (acc_id, acc_name, acc_amount, acc_date)
      2 values(acc_seq.nextval, 'petar', 15000, sysdate);


      And when I want to update the table, I get this error



      SQL> update accounts
      2 set acc_amount = 1000000
      3 where acc_id = 1;
      update accounts
      *
      ERROR at line 1:
      ORA-20000: Concurrency Failure
      ORA-06512: at "PETAR1.ACC_PREUPDATE", line 3
      ORA-04088: error during execution of trigger 'PETAR1.ACC_PREUPDATE'


      now I select users with id 1.



      select acc_name, acc_amount from accounts where acc_id = 1;


      And when I try to update the table again, I get the same error.



      Why can not I update the table?



      I use Oracle 12c r2







      oracle plsql database-trigger






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 9:44









      APC

      117k15116229




      117k15116229










      asked Nov 9 at 22:22









      petar

      545




      545






















          1 Answer
          1






          active

          oldest

          votes


















          1














          In your final update statement you do not specify an update to TCN, so in the update trigger, the value of :OLD.TCN is what you had inserted before (via the insert trigger). Since the update does not explicitly change it, also :NEW.TCN is that same value.



          So :OLD.TCN is equal to :NEW.TCN and making the if condition in the update trigger true, raising your custom exception.






          share|improve this answer




















          • Thank you very much, when I update tcn with one number higher, everything works well.
            – petar
            Nov 9 at 23:03










          • What do you think about my code is it okay to do that?
            – petar
            Nov 10 at 19:47










          • You seem to want to implement optimistic locking, but then your update statements must include a where clause with the expected value of TCN (which should be queried before). Then the update should increment TCN. If a concurrent update happens the TCN will not match with the one in the WHERE clause. However, this comment section is not suitable to discuss all that. Please read about how to implement it. For instance here
            – trincot
            Nov 10 at 19:56











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234028%2fproblem-with-using-optimistic-locking-i-can-not-update-after-viewing-the-table%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          In your final update statement you do not specify an update to TCN, so in the update trigger, the value of :OLD.TCN is what you had inserted before (via the insert trigger). Since the update does not explicitly change it, also :NEW.TCN is that same value.



          So :OLD.TCN is equal to :NEW.TCN and making the if condition in the update trigger true, raising your custom exception.






          share|improve this answer




















          • Thank you very much, when I update tcn with one number higher, everything works well.
            – petar
            Nov 9 at 23:03










          • What do you think about my code is it okay to do that?
            – petar
            Nov 10 at 19:47










          • You seem to want to implement optimistic locking, but then your update statements must include a where clause with the expected value of TCN (which should be queried before). Then the update should increment TCN. If a concurrent update happens the TCN will not match with the one in the WHERE clause. However, this comment section is not suitable to discuss all that. Please read about how to implement it. For instance here
            – trincot
            Nov 10 at 19:56
















          1














          In your final update statement you do not specify an update to TCN, so in the update trigger, the value of :OLD.TCN is what you had inserted before (via the insert trigger). Since the update does not explicitly change it, also :NEW.TCN is that same value.



          So :OLD.TCN is equal to :NEW.TCN and making the if condition in the update trigger true, raising your custom exception.






          share|improve this answer




















          • Thank you very much, when I update tcn with one number higher, everything works well.
            – petar
            Nov 9 at 23:03










          • What do you think about my code is it okay to do that?
            – petar
            Nov 10 at 19:47










          • You seem to want to implement optimistic locking, but then your update statements must include a where clause with the expected value of TCN (which should be queried before). Then the update should increment TCN. If a concurrent update happens the TCN will not match with the one in the WHERE clause. However, this comment section is not suitable to discuss all that. Please read about how to implement it. For instance here
            – trincot
            Nov 10 at 19:56














          1












          1








          1






          In your final update statement you do not specify an update to TCN, so in the update trigger, the value of :OLD.TCN is what you had inserted before (via the insert trigger). Since the update does not explicitly change it, also :NEW.TCN is that same value.



          So :OLD.TCN is equal to :NEW.TCN and making the if condition in the update trigger true, raising your custom exception.






          share|improve this answer












          In your final update statement you do not specify an update to TCN, so in the update trigger, the value of :OLD.TCN is what you had inserted before (via the insert trigger). Since the update does not explicitly change it, also :NEW.TCN is that same value.



          So :OLD.TCN is equal to :NEW.TCN and making the if condition in the update trigger true, raising your custom exception.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 22:49









          trincot

          117k1478110




          117k1478110











          • Thank you very much, when I update tcn with one number higher, everything works well.
            – petar
            Nov 9 at 23:03










          • What do you think about my code is it okay to do that?
            – petar
            Nov 10 at 19:47










          • You seem to want to implement optimistic locking, but then your update statements must include a where clause with the expected value of TCN (which should be queried before). Then the update should increment TCN. If a concurrent update happens the TCN will not match with the one in the WHERE clause. However, this comment section is not suitable to discuss all that. Please read about how to implement it. For instance here
            – trincot
            Nov 10 at 19:56

















          • Thank you very much, when I update tcn with one number higher, everything works well.
            – petar
            Nov 9 at 23:03










          • What do you think about my code is it okay to do that?
            – petar
            Nov 10 at 19:47










          • You seem to want to implement optimistic locking, but then your update statements must include a where clause with the expected value of TCN (which should be queried before). Then the update should increment TCN. If a concurrent update happens the TCN will not match with the one in the WHERE clause. However, this comment section is not suitable to discuss all that. Please read about how to implement it. For instance here
            – trincot
            Nov 10 at 19:56
















          Thank you very much, when I update tcn with one number higher, everything works well.
          – petar
          Nov 9 at 23:03




          Thank you very much, when I update tcn with one number higher, everything works well.
          – petar
          Nov 9 at 23:03












          What do you think about my code is it okay to do that?
          – petar
          Nov 10 at 19:47




          What do you think about my code is it okay to do that?
          – petar
          Nov 10 at 19:47












          You seem to want to implement optimistic locking, but then your update statements must include a where clause with the expected value of TCN (which should be queried before). Then the update should increment TCN. If a concurrent update happens the TCN will not match with the one in the WHERE clause. However, this comment section is not suitable to discuss all that. Please read about how to implement it. For instance here
          – trincot
          Nov 10 at 19:56





          You seem to want to implement optimistic locking, but then your update statements must include a where clause with the expected value of TCN (which should be queried before). Then the update should increment TCN. If a concurrent update happens the TCN will not match with the one in the WHERE clause. However, this comment section is not suitable to discuss all that. Please read about how to implement it. For instance here
          – trincot
          Nov 10 at 19:56


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234028%2fproblem-with-using-optimistic-locking-i-can-not-update-after-viewing-the-table%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Edmonton

          Crossroads (UK TV series)