sql >> Database teknologi >  >> RDS >> Mysql

MySQL - Sådan kontrollerer du, om START TRANSAKTION er aktiv

Du kan oprette en procedure, der vil udnytte en fejl, som kun kan opstå inden for en transaktion:

DELIMITER //
CREATE PROCEDURE `is_in_transaction`(OUT $transaction bool)
BEGIN
    DECLARE oldIsolation TEXT DEFAULT @@TRANSACTION_ISOLATION;
    DECLARE EXIT HANDLER FOR 1568 BEGIN
        -- error 1568 will only be thrown within a transaction
        SET $transaction = true;
    END;
    -- will throw an error if we are within a transaction
    SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
    -- no error was thrown - we are not within a transaction
    SET TRANSACTION_ISOLATION = oldIsolation;
    SET $transaction = false;
END//
DELIMITER ;

Test proceduren:

set @within_transaction := null;
set @out_of_transaction := null;

begin;
    CALL is_in_transaction(@within_transaction);
commit;

CALL is_in_transaction(@out_of_transaction);

select @within_transaction, @out_of_transaction;

Resultat:

@within_transaction | @out_of_transaction
--------------------|--------------------
                  1 |                   0

Med MariaDB kan du bruge @@in_transaction



  1. i Php hvordan fjerner man emoji-kode?

  2. Nogle ENHVER Aggregerede Transformationer er ødelagte

  3. Forskellen mellem en bruger og et login i SQL Server

  4. MySQL binær vs char tegnsæt binær