Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of Transaction in Mysql

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 180
    Comment on it

    Transaction is very useful in large rich applications such as eCommerce, Real Estate etc etc. It is mostly used in Customer Relationship Management (CRM), Enterprise Resource Plan (ERP).

    What is Transaction? When we need to execute more than one queries and that queries needs to execute several times in a loop, it means if we want to save multiple records in a database tables, then unfortunately sometimes data save in some tables and sometimes not.
    So here we know that those tables are related to each other with the foreign keys defined in other tables, at this time it is must to save all data in all tables otherwise when you will make reports you will not get required data as you want.
    So not to produce this type of problem we implement Transaction, Transaction simply says if data is saved in all tables then applied transaction code will commit the queries at the end of all data saved otherwise all queries will be rolled back due to any one of the query which fails to save the data, So in this way nothing will be save in database and it will never produce a problem.

    Real World Example of Transaction is:-

    Suppose you have a Budget of 1000 USD in your Account and you withdraw 900USD and then at the last step ATM machine suddenly stops working due to power outage, Once power comes back then again you withdraw 900 USD, you will be surprised to see that your remaining balance will be 100USD which is not dispensed to you at all. So you need transaction to perform that it should not happen, sql statements are executed inside transactions so when all money will dispense to you then only it will update the whole database of your account details.

    To Implement transaction , we write the code in mysql is:-

    mysqli_query($con, "BEGIN");

    $con is the connection variable and BEGIN is the parameter which is passed to this function and it begins the Transaction.

    try{
         // mysql queries
         // if query is true, just commit the queries at the end of try, otherwise just throw to catch
         if ($query == false)
         {
              throw new Exception("Oops! Some Error Occurred, Please try again");
         }
          mysqli_query($con, "COMMIT");
          // echo success message;
    }
    catch (Exception $ex){
        mysqli_query($con, "ROLLBACK");
        // get message and echo message
       $message = $ex->getMessage();
       echo $message;
    }
    

    Note MyISAM storage engine in MySQL doesn't support transaction. To make transaction works in MySQL use InnoDB.

    Thanks

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: