Transactions
GaussDB is compatible with MySQL transactions, but there are some differences. This section describes transaction-related differences in GaussDB M-compatible databases.
Default Transaction Isolation Levels
The default isolation level of an M-compatible database is READ COMMITTED, and that of MySQL is REPEATABLE-READ.
-- View the current transaction isolation level. m_db=# SHOW transaction_isolation;
Sub-transactions
In an M-compatible database, SAVEPOINT is used to create a savepoint (sub-transaction) in the current transaction, and ROLLBACK TO SAVEPOINT is used to roll back to a savepoint (sub-transaction). After the sub-transaction is rolled back, the parent transaction can continue to run, the rollback of a sub-transaction does not affect the transaction status of the parent transaction.
No savepoint (sub-transaction) can be created in MySQL.
Nested Transactions
A nested transaction refers to a new transaction started in a transaction block.
In an M-compatible database, if a new transaction is started in a normal transaction block, a warning is displayed indicating that an ongoing transaction exists and the start command is ignored. If a new transaction is started in an abnormal transaction block, an error is reported. The transaction can be executed only after ROLLBACK/COMMIT is executed. If ROLLBACK/COMMIT is executed, the previous statement is rolled back.
In MySQL, if a new transaction is started in a normal transaction block, the previous transaction is committed and then the new transaction is started. If a new transaction is started in an abnormal transaction block, the error is ignored, and the previous error-free statement is committed and the new transaction is started.
-- In an M-compatible database, if a new transaction is started in a normal transaction block, a warning is generated and the transaction is ignored. m_db=# DROP TABLE IF EXISTS test_t; m_db=# CREATE TABLE test_t(a int, b int); m_db=# BEGIN; m_db=# INSERT INTO test_t values(1, 2); m_db=# BEGIN; -- The warning "There is already a transaction in progress" is displayed. m_db=# SELECT * FROM test_t ORDER BY 1; m_db=# COMMIT; -- In an M-compatible database, if a new transaction is started in an abnormal transaction block, an error is reported. The transaction can be executed only after ROLLBACK/COMMIT is executed. m_db=# BEGIN; m_db=# ERROR sql; -- Error statement. m_db=# BEGIN; -- An error is reported. m_db=# COMMIT; -- It can be executed only after ROLLBACK/COMMIT is executed.
Statements Committed Implicitly
An M-compatible database uses GaussDB for storage and inherits the GaussDB transaction mechanism. If a DDL or DCL statement is executed in a transaction, the transaction is not automatically committed.
In MySQL, if DDL, DCL, management-related, or lock-related statements are executed, the transaction is automatically committed.
-- In M-compatible database, table creation and GUC parameter setting support rollback. m_db=# DROP TABLE IF EXISTS test_table_rollback; m_db=# BEGIN; m_db=# CREATE TABLE test_table_rollback(a int, b int); m_db=# \d test_table_rollback; m_db=# ROLLBACK; The m_db=# \d test_table_rollback; -- This table does not exist.
Differences in SET TRANSACTION
In an M-compatible database, if SET TRANSACTION is used to set the isolation level or transaction access mode for multiple times, only the last one takes effect. Transaction features can be separated by spaces and commas (,).
In MySQL, SET TRANSACTION cannot be used to set the isolation level or transaction access mode for multiple times. Transaction features can only be separated by commas (,).
No. |
Syntax |
Function |
Difference |
---|---|---|---|
1 |
SET TRANSACTION |
Sets transactions. |
In an M-compatible database, SET TRANSACTION takes effect in session-level transactions. In MySQL, SET TRANSACTION takes effect at the next transaction. |
2 |
SET SESSION TRANSACTION |
Sets session-level transactions. |
- |
3 |
SET GLOBAL TRANSACTION |
Sets global session-level transactions. This feature applies to subsequent sessions and has no impact on the current session. |
In an M-compatible database, GLOBAL takes effect in transactions at the global session level and applies only to the current database instance. In MySQL, this feature takes effect in all databases. |
-- SET TRANSACTION takes effect in session-level transactions. m_db=# SET TRANSACTION ISOLATION LEVEL READ COMMITTED READ WRITE; m_db=# SHOW transaction_isolation; m_db=# SHOW transaction_read_only; -- In an M-compatible database, if the isolation level or transaction access mode is set for multiple times, only the last one takes effect. m_db=# SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED, ISOLATION LEVEL REPEATABLE READ, READ WRITE, READ ONLY; m_db=# SHOW transaction_isolation; -- repeatable read m_db=# SHOW transaction_read_only; -- on
Differences in START TRANSACTION
In an M-compatible database, when START TRANSACTION is used to start a transaction, the isolation level can be set. If the isolation level or transaction access mode is set for multiple times, only the last one takes effect. In the current version, consistency snapshot cannot be enabled immediately. Transaction features can be separated by spaces and commas (,).
In MySQL, if START TRANSACTION is used to start a transaction, the isolation level cannot be set and the transaction access mode cannot be set for multiple times. Transaction features can only be separated by commas (,).
-- Start a transaction and set the isolation level. m_db=# START TRANSACTION ISOLATION LEVEL READ COMMITTED; m_db=# COMMIT; -- Set the access mode for multiple times. m_db=# START TRANSACTION READ ONLY, READ WRITE; m_db=# COMMIT;
Transaction-related GUC Parameters
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot