Runtime Error
Cannot execute COMMIT when in read-only transaction
What This Error Means
This error occurs when you attempt to commit changes to a database within a transaction that has been marked as read-only. In a read-only transaction, you are not allowed to make any modifications to the database.
Why It Happens
This error typically happens when you have a transaction set to read-only and then attempt to execute a COMMIT statement. The COMMIT statement is used to save changes made within a transaction, but in a read-only transaction, there are no changes to save.
How to Fix It
- 1To fix this error, you need to either alter the transaction to be read-write or discard the transaction and roll back the changes. You can do this by adding a SET TRANSACTION statement with the READ WRITE option before executing the COMMIT statement, or by using the ROLLBACK statement to discard the transaction and its changes.
Example Code Solution
❌ Before (problematic code)
SQL
START TRANSACTION READ ONLY;
INSERT INTO users (username, password) VALUES ('newuser', 'password123');
COMMIT;✅ After (fixed code)
SQL
START TRANSACTION READ WRITE;
INSERT INTO users (username, password) VALUES ('newuser', 'password123');
COMMIT;Fix for Cannot execute COMMIT when in read-only transaction
Browse Related Clusters
Related SQL Errors
Related SQL Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error