Regarding the MariaDB transactions

In My Practice Application,

I have an accounts table that contains the details of the account holder like the name, balance, etc.

Here, I am writing a stored procedure to transfer an amount from one account to another account

          in the Transfer Amount stored procedure, 2 steps need to be included in a transaction, 

                            1)  subtracting amount from the sender account.

                            2) Adding amount to the receiver account 

  Discussion posts and replies are publicly visible

  • +1
    Certified Associate Developer

    Hi mohan, can you please try the below code. Here I have used customer and accounts table for example. If there is an error signal, customer table will be printed, else account table will be printed.

    DELIMITER $$
    CREATE DEFINER=dbadmin@% PROCEDURE transactionCode()
    BEGIN
        DECLARE _rollback BOOL DEFAULT 0;
        DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET _rollback = 1;
            
        START TRANSACTION;
            SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = "This above executed SQL statements will be rollbacked";
        IF _rollback 
        THEN
            ROLLBACK;
            SELECT * from Customer;
        ELSE
            COMMIT;
            SELECT * from accounts;
        END IF;
    END$$
    DELIMITER ;