--echo #
--echo # MDEV-36959 Deadlock does not rollback transaction fully
--echo #

--source include/have_log_bin.inc
--source include/have_innodb.inc
--source include/count_sessions.inc

CREATE TABLE t1(col1 INT PRIMARY KEY, col2 INT) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1, 1), (2, 2);
SELECT * FROM t1;

--connect (con1,localhost,root,,)
START TRANSACTION;
--echo # Trx-1: Lock 1st record
UPDATE t1 SET col2=10 where col1=1;

--connection default
START TRANSACTION;
--echo # Trx-2: Lock 2nd record
UPDATE t1 SET col2=100 where col1=2;

--connection con1
--echo # Trx-1: Try locking 1st record : Wait
--send UPDATE t1 SET col2=10 where col1=2

--connection default
--echo # Wait for Trx-1 to get into lock wait stage
let $wait_condition=
  SELECT COUNT(*) >= 2 FROM INFORMATION_SCHEMA.INNODB_LOCKS
  WHERE lock_table like "%t1%";
--source include/wait_condition.inc

--echo # Trx-2: Try locking 2nd record : Deadlock
--error ER_LOCK_DEADLOCK
UPDATE t1 SET col2=100 where col1=1;

--connection con1
--reap
ROLLBACK;

--connection default
SELECT @@in_transaction;
UPDATE t1 SET col2=10 where col1=1;
UPDATE t1 SET col2=100 where col1=2;
SELECT @@in_transaction;
ROLLBACK;

SELECT * FROM t1;
DROP TABLE t1;

--echo #
--echo # MDEV-37141 DML committed within XA transaction block after deadlock error and implicit rollback
--echo #

CREATE TABLE t1(col1 INT PRIMARY KEY, col2 INT) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1, 1), (2, 2);
SELECT * FROM t1;

--echo # Wait for information schema cache for locks to get cleared
let $wait_condition=
  SELECT COUNT(*) = 0 FROM INFORMATION_SCHEMA.INNODB_LOCKS
  WHERE lock_table like "%t1%";
--source include/wait_condition.inc

--connection con1
XA BEGIN 'x1';
--echo # XA Trx-1: Lock 1st record
UPDATE t1 SET col2=10 where col1=1;

--connection default
XA BEGIN 'x2';
--echo # XA Trx-2: Lock 2nd record
UPDATE t1 SET col2=100 where col1=2;

--connection con1
--echo # XA Trx-1: Try locking 1st record : Wait
--send UPDATE t1 SET col2=10 where col1=2

--connection default
--echo # Wait for XA Trx-1 to get into lock wait stage
let $wait_condition=
  SELECT COUNT(*) >= 2 FROM INFORMATION_SCHEMA.INNODB_LOCKS
  WHERE lock_table like "%t1%";
--source include/wait_condition.inc

--echo # XA Trx-2: Try locking 2nd record : Deadlock
--error ER_LOCK_DEADLOCK
UPDATE t1 SET col2=100 where col1=1;
--error ER_XAER_RMFAIL
INSERT INTO t1 VALUES (3, 3), (4, 4);
--error ER_XAER_RMFAIL
XA END 'x2';
XA ROLLBACK 'x2';

--connection con1
--reap
XA END 'x1';
XA ROLLBACK 'x1';

--connection default
SELECT * FROM t1;
DROP TABLE t1;

--disconnect con1
--source include/wait_until_count_sessions.inc
