36
loading...
This website collects cookies to deliver better user experience
drop table if exists franck;
create table franck(v int);
insert into franck values (42);
begin transaction;
--default-- set transaction isolation level read committed;
select sum(v) from franck;
\gset
sum
variable. Your mission is to enter a compensation row, to set the total to zero (by inserting - sum
). You do that on both terminals where you initiated the transaction:insert into franck values(-:sum);
commit;
select * from franck;
drop table if exists franck;
create table franck(v int);
insert into franck values (42);
begin transaction;
set transaction isolation level serializable;
select sum(v) from franck;
\gset
insert into franck values(-:sum);
commit;
select * from franck;
ERROR: could not serialize access due to read/write dependencies among transactions
DETAIL: Reason code: Canceled on identification as a pivot, during write.
HINT: The transaction might succeed if retried.
exec begin execute immediate 'drop table franck'; exception when others then null; end;
create table franck(v int);
insert into franck values (42);
commit;
set transaction isolation level serializable;
column sum_v new_value sum;
select sum(v) sum_v from franck;
insert into franck values(-&sum);
commit;
select * from franck;
drop table if exists franck;
create table franck(v int);
insert into franck values (42);
begin transaction;
set transaction isolation level serializable;
select sum(v) from franck;
\gset
insert into franck values(-:sum);
commit;
select * from franck;
yb_transaction_priority_lower_bound
and yb_transaction_priority_upper_bound
but you can set non-overlapping bounds if you want to.ERROR: Operation failed. Try again: d1bbba13-dfab-423b-b577-6baf39eb9054 Conflicts with higher priority transaction: f25928b4-df3b-435e-94b4-fd118174c97a
explicitly mentions that the application should retry. Optimistic locking is used when the probability of conflict is low, and better handle retries rather than locking which would not be scalable. From the application, this error can be detected with SQLSTATE 40001} catch(SQLException e) {
if ( "40001".equals(e.getSQLState()) ) { // transaction conflict
System.out.printf(
Instant.now().toString()+" SQLSTATE %s on retry #%d %s\n",e.getSQLState(),retries,e
);
catch(SQLException e)
and "40001".equals(e.getSQLState()
can use a retry counter, in a loop that waits a few milliseconds.