Problem with using “Optimistic locking” I can not update after viewing the table
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
add a comment |
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
add a comment |
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
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
oracle plsql database-trigger
edited Nov 10 at 9:44
APC
117k15116229
117k15116229
asked Nov 9 at 22:22
petar
545
545
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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