How to Use MySQL Transactions on Alibaba Cloud ECS or ApsaraDB for MySQL

By Francis Ndungu, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

MySQL is one of the most popular Relational Database Management Systems (RDBMS). The open-source database server is widely adopted in production environment for data storage especially with Content Management Systems like WordPress and Magento.

Alibaba Cloud is at the forefront of offering scalable, affordable and secure MySQL services through Elastic Compute Service (ECS) and ApsaraDB for RDS.

One of the key features of MySQL is its supports for transactions and explicit locks that make it the best when it comes to ACID compliance. We will explain this later in the guide.

MySQL transactions refers to group of database manipulation operations that are performed in a single batch. A transaction is regarded complete if all the individual operations succeed. If one of them fails, the transaction is rolled back to ensure data integrity.

In this guide, we will show you how to implement MySQL transactions on your MySQL database hosted on Alibaba Cloud to ensure consistency and integrity of your data.


  1. A valid Alibaba Account(Sign up now and get up a free trial worth up to $1200 to test 40+ Alibaba Cloud products)
  2. An ECS instance with MySQL installed or an ECS instance for ApsaraDB for Redis with MySQL.
  3. Username and password for your MySQL database.

Step 1: Defining ACID Compliance in Database Systems

When we say that MySQL is ACID compliant, it means the database supports 4 characteristics: Atomicity, Consistency, Isolation and Durability.


In database systems, atomicity ensures that multi-query operations are either completed successfully or nothing is updated on the system at all if an error happens between the different grouped queries. For instance, in a bank application, the transaction should be completed only if the software is able to debit the drawer and credit the payee. Otherwise, if one of the operation fails, the partial transaction should be rolled back.


Consistency guarantees that a data operation is changed in the allowed manner. For instance, if an operation is saving data both to the orders and orders products table, the execution plan should affect both tables. If an error occurs when writing data to the second table, the transaction must be rolled back.


To support isolation MySQL uses locks to ensure concurrent operations and integrity of data visible to multiple users. In a multi-user environment, isolation is inevitable.

Consider a scenario where millions of customers are gambling for the last seat in a booking system. If proper isolation of data is not supported on the system, multiple customers would end up with the same seat if they run the ordering process within the same microsecond.

It may sound rare but it happens in a production environment. In an ecommerce platform, isolation allows the application to assign a different and unique order id even if millions of customers are purchasing from the shopping cart simultaneously especially during peak seasons.


Durability is the process of committing transaction in a database permanently. Durability makes sure that changes are made permanently before allowing any other transaction of the same kind. E.g. in a mobile money transaction, a module issuing transactions IDs should book another user on the system only when the first user is assigned an ID that has been successfully saved to the disk.

To implement all these features, this is where MySQL transactions and locks come in to play.

Step 2: Logging to your MySQL Server

You can implement MySQL transactions when running queries either directly on the MySQL Command Line Interface, GUI based database administration tools(e.g. MySQL), or from your favourite programming language such as PHP or Python.

To follow along with this guide, we recommend that you login to your database server through a CLI. The basic syntax is:


Step 3: Creating a Sample Database and Tables

First, create a database named my_store. We will use innoDB as the storage engine for the tables because it supports the standard ACID-compliant transactions:

We will demonstrate MySQL transactions from an e-commerce perspective. We will need to create 4 tables:

Before we do this, select the database we have created above first:

A brief explanation about the tables we are going to use:

  1. customers table. This table will handle information about customers buying from the store.
  2. products table. This is our data store for our products.
  3. orders table. Once a customer makes an order, we will record the details here with the current date/time and customer_id.
  4. order_products table. This table will handle all products associated with a particular order_id

Creating the ‘customers’ Table:

Load Sample Data to ‘customers’ Table

You may run the query below to confirm the customers list:

Creating the ‘products’ Table:

Loading Sample Data to ‘products’ Table

Confirming the Products

Creating the ‘orders’ Table:

The order_id field will be the primary key. However, we won’t use the auto increment feature here. We will find the maximum order number from the previous transactions and add 1 to get the new order number. This is to demonstrate the principle of isolation.

Since our transaction will be isolated with locks, there is no chance of two customers getting the same order number even if millions are buying from our sites. Our database will only allow a new order to be executed once we are through with the first order and released the table locks.

So let’s create the orders table first:

Creating the ‘order_products’ Table:

The order_products table must contain the product_id so that it can log the product sold through a particular order. It must also have the order_id to identify the order and a order_product_id which is an auto generated field acting as the primary key for the table.

Step 3: Creating a Sales Order Transactions

With our database schema and sample data in place, we can run an ACID compliant transaction. When a customer makes an order, 2 tables should be affected. That is, a new order should be registered on the orders table and the products purchased by the customer should be added to the order_products table.

If any of the operation fails, the transaction should be rolled back. The correct way to do this is to begin a transaction with SET autocommit =0 followed by lock tables, then do the two transactions, commit the changes and finally unlock the tables as shown below:

So this is the correct syntax. In the following ACID compliant SQL statement we will create a transaction to signify an order made by customer 1 (John Doe). He bought 2 Bluetooth Speakers, 4 pieces of the 240GB SSD and 6 pieces of Wireless Mouse.

Please note, we will just record the product_id on the order_products table. In case we need to know the names of the merchandise that the customer bought, we can just run a join with the products table.

Also, we are acquiring a WRITE lock to ensure that other MySQL connections/sessions won’t be able to read or write to the table before we complete the transaction. If a new sessions tries to create an order before we complete ours, it will be waitlisted until we unlock the tables. This is where the beauty of transactions comes in.

To create our order, run the SQL commands one by one. If any of the transaction fails, issue the ROLLBACK; and UNLOCK TABLES; command after each other:

Confirming the Transactions:

The orders table:

The order_products table:

Remember if you were using a programming language like PHP to run the transaction, you would evaluate whether the transaction was successful before issuing the commit operation, otherwise, you would roll back the transaction.

Irrespective of whether the transaction is successful or not, always issue the unlock tables command to avoid holding other transactions on the queue.

If you have followed our tutorial up to this point and everything worked as explained, congratulations! You have just created the first ACID compliant transaction on your MySQL database hosted on Alibaba Cloud.


In this guide, we have taken you through the steps of creating transactions on your MySQL database hosted on Alibaba Cloud ECS instance or ApsaraDB for Redis. Remember, while this is a demonstration to prove the concept of transactions in MySQL, you can expand it further and apply it in a production environment to avoid race conditions on your applications and enhance database integrity in a multi-user environment.


Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store