Explicit Transactions in MatrixOne

In MatrixOne’s explicit transactions also obey the following rules:

Explicit transaction rules

  • An explicit transaction starts and ends with BEGIN...END or START TRANSACTION...COMMIT or ROLLBACK.
  • In explicit transactions, DML (Data Manipulation Language) and DDL (Data Definition Language) can exist at the same time. All DDLs are supported.
  • In an explicit transaction, other explicit transactions cannot be nested. For example, if START TANSACTIONS is encountered after START TANSACTIONS, all statements between two START TANSACTIONS will be forced to commit, regardless of the value of AUTOCOMMIT 1 or 0.
  • In an explicit transaction, only DML and DDL can be included and cannot contain modification parameter configuration or management commands, such as set [parameter] = [value], create user, and so on.
  • In an explicit transaction, if a write-write conflict occurs when a new transaction is started without an explicit commit or rollback, the previously uncommitted transaction will be rolled back, and an error will be reported.

Differences from MySQL explicit transactions

Transaction typeTurn on autocommitTurn off autocommit
Explicit Transactions vs. AutocommitWhen AUTOCOMMIT=1, MySQL will not change the transaction, and each statement will be executed in a new auto-commit transaction.When AUTOCOMMIT=0, each statement will be executed in an explicitly opened transaction until the transaction is explicitly committed or rolled back.
Explicit transactions and non-autocommitWhen AUTOCOMMIT=1, MySQL will automatically commit uncommitted transactions after each statement execution.When AUTOCOMMIT=0, each statement will be executed in an explicitly opened transaction until the transaction is explicitly committed or rolled back.

MySQL and MatrixOne Explicit Transaction Behavior Example

  1. mysql> CREATE TABLE Accounts (account_number INT PRIMARY KEY, balance DECIMAL(10, 2));
  2. Query OK, 0 rows affected (0.07 sec)
  3. mysql> INSERT INTO Accounts (account_number, balance) VALUES (1, 1000.00), (2, 500.00);
  4. Query OK, 2 rows affected (0.00 sec)
  5. mysql> BEGIN;
  6. Query OK, 0 rows affected (0.01 sec)
  7. mysql> UPDATE Accounts SET balance = balance - 100.00 WHERE account_number = 1;
  8. Query OK, 1 row affected (0.00 sec)
  9. mysql> UPDATE Accounts SET balance = balance + 100.00 WHERE account_number = 2;
  10. Query OK, 1 row affected (0.00 sec)
  11. mysql> COMMIT;
  12. Query OK, 0 rows affected (0.01 sec)
  13. mysql> BEGIN;
  14. Query OK, 0 rows affected (0.00 sec)
  15. mysql> UPDATE Accounts SET balance = balance - 100.00 WHERE account_number = 1;
  16. Query OK, 1 row affected (0.00 sec)
  17. mysql> UPDATE Accounts SET invalid_column = 0 WHERE account_number = 2;
  18. ERROR 20101 (HY000): internal error: column 'invalid_column' not found in table
  19. Previous DML conflicts with existing constraints or data format. This transaction has to be aborted
  20. mysql> ROLLBACK;
  21. Query OK, 0 rows affected (0.00 sec)
  22. mysql> SELECT * FROM Accounts;
  23. +----------------+---------+
  24. | account_number | balance |
  25. +----------------+---------+
  26. | 1 | 900.00 |
  27. | 2 | 600.00 |
  28. +----------------+---------+
  29. 2 rows in set (0.01 sec)

Cross-Database Transaction Behavior Example

MatrixOne supports cross-database transaction behavior; here, we’ll illustrate it with a simple example.

First, let’s create two databases (db1 and db2) along with their respective tables (table1 and table2):

  1. -- Create the db1 database
  2. CREATE DATABASE db1;
  3. USE db1;
  4. -- Create table1 within db1
  5. CREATE TABLE table1 (
  6. id INT AUTO_INCREMENT PRIMARY KEY,
  7. field1 INT
  8. );
  9. -- Create the db2 database
  10. CREATE DATABASE db2;
  11. USE db2;
  12. -- Create table2 within db2
  13. CREATE TABLE table2 (
  14. id INT AUTO_INCREMENT PRIMARY KEY,
  15. field2 INT
  16. );

Now, we have created two databases and their tables. Next, let’s insert some data:

  1. -- Insert data into table1 in db1
  2. INSERT INTO db1.table1 (field1) VALUES (100), (200), (300);
  3. -- Insert data into table2 in db2
  4. INSERT INTO db2.table2 (field2) VALUES (500), (600), (700);

We now have data in both databases. Moving on, let’s execute a cross-database transaction to modify data in these two databases simultaneously:

  1. -- Start the cross-database transaction
  2. START TRANSACTION;
  3. -- Update data in table1 within db1
  4. UPDATE db1.table1 SET field1 = field1 + 10;
  5. -- Update data in table2 within db2
  6. UPDATE db2.table2 SET field2 = field2 - 50;
  7. -- Commit the cross-database transaction
  8. COMMIT;

In the above cross-database transaction, we begin with START TRANSACTION;, then proceed to update data in table1 within db1 and table2 within db2. Finally, we use COMMIT; to commit the transaction. If any step fails during the transaction, the entire transaction is rolled back to ensure data consistency.

This example demonstrates a complete cross-database transaction. Cross-database transactions can be more complex in real-world applications, but this simple example helps us understand the fundamental concepts and operations involved.