With MySQL, how do BEGIN; ROLLBACK; and COMMIT; relate to autocommit? - mysql

It seems like you can achieve all you need with transactions using only BEGIN; ROLLBACK and COMMIT;. Are there certain scenarios that require using autocommit? Does calling BEGIN; set autocommit to false? Does calling COMMIT; set autocommit to true?

autocommit can be set per session or globally. No scenario that require autocommit (not all RDMS support auto-commit mode). In my opinion, the reason why autocommit is presented and true by default is because MyISAM engine does not support transactions at all, so to alleviate porting applications written for MyISAM they mimic the same behaviour.
Does calling BEGIN; set autocommit to false? Does calling COMMIT; set autocommit to true?
From mysql documentation
To disable autocommit mode for a single series of statements, use the START TRANSACTION statement: ...
With START TRANSACTION, autocommit remains disabled until you end the transaction with COMMIT or ROLLBACK. The autocommit mode then reverts to its previous state.

Related

Is autocommit supposed to allow/disallow rollback in MySql

SET autocommit = 0;
start transaction;
update ... where ID=29;
rollback;
Whether autocommit is 1 or 0 does not seem to affect the rollback? Does autocommit ontrol whether update is committed immediately or not? What does autocommit supposed to affect?
Even if you have autocommit enabled, that doesn't stop you from starting a transaction. When you explicitly start a transaction, this supercedes autocommit for the duration of the transaction. Once you commit (or rollback), autocommit resumes at its previous value.
So if you explicitly start a transaction, you can rollback, even if normally you have autocommit enabled.
From documentation autocommit, Commit, and Rollback:
If autocommit mode is enabled, each SQL statement forms a single transaction on its own
It means that after DML statement ends(without error), it is committed(unless it is wrapped with explicit transaction).

Transactions and AUTOCOMMIT in Mysql

To my knowledge for InnoDB tables in MySQL, all transactions are wrapped with a START TRANSACTION; and end with a COMMIT; unless explicitly stated not to.
If I were to explictly define the transaction block, would it be correct to do something like this:
SET AUTOCOMMIT = 0;
START TRANSACTION;
[SQL STATEMENTS]
COMMIT;
SET AUTOCOMMIT = 1;
Would the next transaction after this go back to the way InnoDb handles transactions by default? My intention is to only sometimes have explicitly defined transactions in my application but all other transactions will be handled by the engine.
You do not need to set autocommit to 0 in your case. Defining a transaction implicitly does that for you. From MySQL documentation:
"To disable autocommit mode implicitly for a single series of statements, use the START TRANSACTION statement:"

MySQL AUTOCOMMIT status while using BEGIN and START TRANSACTION

I need to use a transaction in my project on MySQL. But I'm not sure if I have to use mysql_query("SET AUTOCOMMIT=0"); or not.
I know I have 2 options:
BEGIN
START TRANSACTION
Also I have heard that one of the both items does not need using AUTOCOMMIT = 0.
Please help me to know when I have to use AUTOCOMMIT = 0 actually, With BEGIN or with START TRANSACTION?
Thank you.
As explained in the manual:
By default, MySQL runs with autocommit mode enabled. This means that as soon as you execute a statement that updates (modifies) a table, MySQL stores the update on disk to make it permanent. The change cannot be rolled back.
To disable autocommit mode implicitly for a single series of statements, use the START TRANSACTION statement:
START TRANSACTION;
SELECT #A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=#A WHERE type=1;
COMMIT;
With START TRANSACTION, autocommit remains disabled until you end the transaction with COMMIT or ROLLBACK. The autocommit mode then reverts to its previous state.
The manual goes on to say:
To disable autocommit mode explicitly, use the following statement:
SET autocommit=0;
After disabling autocommit mode by setting the autocommit variable to zero, changes to transaction-safe tables (such as those for InnoDB or NDBCLUSTER) are not made permanent immediately. You must use COMMIT to store your changes to disk or ROLLBACK to ignore the changes.
autocommit is a session variable and must be set for each session. To disable autocommit mode for each new connection, see the description of the autocommit system variable at Section 5.1.3, “Server System Variables”.
BEGIN and BEGIN WORK are supported as aliases of START TRANSACTION for initiating a transaction. START TRANSACTION is standard SQL syntax and is the recommended way to start an ad-hoc transaction.
There is a small difference between Start Transaction and SET AUTOCOMMIT=0.
If START TRANSACTION appears at the beginning of session and AUTOCOMMIT is set 1 (Mysql begins with AUTOCOMMIT enabled) after ROLLBACK, Autocommit is set silently to 1 again
If I put SET AUTOCOMMIT=0, instead of START TRANSACTION, evidently a ROLLBACK let AUTOCOMMIT disabled

Difference between SET autocommit=1 and START TRANSACTION in mysql (Have I missed something?)

I am reading up on transactions in MySQL and am not sure whether I have grasped something specific correctly, and I want to be sure I understood that correctly, so here goes. I know what a transaction is supposed to do, I'm just not sure whether I understood the statement semantics or not.
So, my question is, is anything wrong, (and, if that is the case, what is wrong) with the following:
By default, autocommit mode is enabled in MySQL.
Now, SET autocommit=0; will begin a transaction, SET autocommit=1; will implicitly commit. It is possible to COMMIT; as well as ROLLBACK;, in both of which cases autocommit is still set to 0 afterwards (and a new transaction is implicitly started).
START TRANSACTION; will basically SET autocommit=0; until a COMMIT; or ROLLBACK; takes place.
In other words, START TRANSACTION; and SET autocommit=0; are equivalent, except for the fact that START TRANSACTION; does the equivalent of implicitly adding a SET autocommit=1; after COMMIT; or ROLLBACK;
If that is the case, I don't understand http://dev.mysql.com/doc/refman/5.5/en/set-transaction.html#isolevel_serializable - seeing as having an isolation level implies that there is a transaction, meaning that autocommit should be off anyway?
And if there is another difference (other than the one described above) between beginning a transaction and setting autocommit, what is it?
Being aware of the transaction (autocommit, explicit and implicit) handling for your database can save you from having to restore data from a backup.
Transactions control data manipulation statement(s) to ensure they are atomic. Being "atomic" means the transaction either occurs, or it does not. The only way to signal the completion of the transaction to database is by using either a COMMIT or ROLLBACK statement (per ANSI-92, which sadly did not include syntax for creating/beginning a transaction so it is vendor specific). COMMIT applies the changes (if any) made within the transaction. ROLLBACK disregards whatever actions took place within the transaction - highly desirable when an UPDATE/DELETE statement does something unintended.
Typically individual DML (Insert, Update, Delete) statements are performed in an autocommit transaction - they are committed as soon as the statement successfully completes. Which means there's no opportunity to roll back the database to the state prior to the statement having been run in cases like yours. When something goes wrong, the only restoration option available is to reconstruct the data from a backup (providing one exists). In MySQL, autocommit is on by default for InnoDB - MyISAM doesn't support transactions. It can be disabled by using:
SET autocommit = 0
An explicit transaction is when statement(s) are wrapped within an explicitly defined transaction code block - for MySQL, that's START TRANSACTION. It also requires an explicitly made COMMIT or ROLLBACK statement at the end of the transaction. Nested transactions is beyond the scope of this topic.
Implicit transactions are slightly different from explicit ones. Implicit transactions do not require explicity defining a transaction. However, like explicit transactions they require a COMMIT or ROLLBACK statement to be supplied.
Conclusion
Explicit transactions are the most ideal solution - they require a statement, COMMIT or ROLLBACK, to finalize the transaction, and what is happening is clearly stated for others to read should there be a need. Implicit transactions are OK if working with the database interactively, but COMMIT statements should only be specified once results have been tested & thoroughly determined to be valid.
That means you should use:
SET autocommit = 0;
START TRANSACTION;
UPDATE ...;
...and only use COMMIT; when the results are correct.
That said, UPDATE and DELETE statements typically only return the number of rows affected, not specific details. Convert such statements into SELECT statements & review the results to ensure correctness prior to attempting the UPDATE/DELETE statement.
Addendum
DDL (Data Definition Language) statements are automatically committed - they do not require a COMMIT statement. IE: Table, index, stored procedure, database, and view creation or alteration statements.
In InnoDB you have START TRANSACTION;, which in this engine is the officialy recommended way to do transactions, instead of SET AUTOCOMMIT = 0; (don't use SET AUTOCOMMIT = 0; for transactions in InnoDB unless it is for optimizing read only transactions). Commit with COMMIT;.
You might want to use SET AUTOCOMMIT = 0; in InnoDB for testing purposes, and not precisely for transactions.
In MyISAM you do not have START TRANSACTION;. In this engine, use SET AUTOCOMMIT = 0; for transactions. Commit with COMMIT; or SET AUTOCOMMIT = 1; (Difference explained in MyISAM example commentary below). You can do transactions this way in InnoDB too.
Source: http://dev.mysql.com/doc/refman/5.6/en/glossary.html#glos_autocommit
Examples of general use transactions:
/* InnoDB */
START TRANSACTION;
INSERT INTO table_name (table_field) VALUES ('foo');
INSERT INTO table_name (table_field) VALUES ('bar');
COMMIT; /* SET AUTOCOMMIT = 1 might not set AUTOCOMMIT to its previous state */
/* MyISAM */
SET AUTOCOMMIT = 0;
INSERT INTO table_name (table_field) VALUES ('foo');
INSERT INTO table_name (table_field) VALUES ('bar');
SET AUTOCOMMIT = 1; /* COMMIT statement instead would not restore AUTOCOMMIT to 1 */
https://dev.mysql.com/doc/refman/8.0/en/lock-tables.html
The correct way to use LOCK TABLES and UNLOCK TABLES with transactional tables, such as InnoDB tables, is to begin a transaction with SET autocommit = 0 (not START TRANSACTION) followed by LOCK TABLES, and to not call UNLOCK TABLES until you commit the transaction explicitly. For example, if you need to write to table t1 and read from table t2, you can do this:
SET autocommit=0;
LOCK TABLES t1 WRITE, t2 READ, ...;... do something with tables t1 and t2 here ...
COMMIT;
UNLOCK TABLES;
If you want to use rollback, then use start transaction and otherwise forget all those things,
By default, MySQL automatically commits the changes to the database.
To force MySQL not to commit these changes automatically, execute following:
SET autocommit = 0;
//OR
SET autocommit = OFF
To enable the autocommit mode explicitly:
SET autocommit = 1;
//OR
SET autocommit = ON;

Rollback not working in MySQL

I have a user table, and I have 5 records.
I deleted two records, then executed the rollback command, it executed successfully.
But that deleted two records not recovered.
The user table engine is InnoDB.
You should be able to rollback your transaction as the table engine is InnoDB.
Anyways here is the correct way to do transactions,
SET autocommit=0;
START TRANSACTION;
Your Query here.
ROLLBACK;
and make sure that you are not using COMMIT after the Query which you need to rollback. Refer
Table Engines and Transaction. And When a DB connection is created, it is in auto-commit mode by default. This means that each individual SQL statement is treated as a transaction and is automatically committed right after it is executed. So if you need to do transactions yourself, you must turn off the autocommit mode by AUTOCOMMIT = 0. Refer this link for more info.
By default, MySQL starts the session
for each new connection with
autocommit enabled,
You could set autocommit mode disable before running your query
SET autocommit=0;
ref:
Link 1
Link 2
Make sure you already do command
start transaction;
before the query delete.
SET autocommit=0;
BEGIN;
.
.
.
ROLLBACK;
START TRANSACTION, COMMIT, and ROLLBACK Syntax
I don't know if you were locking tables, but I ran into the same problem where it seemed like rollback wasn't working.
Within my transaction I called "LOCK TABLES...", which
implicitly commits any active transaction before attempting to lock
the tables
(http://dev.mysql.com/doc/refman/5.0/en/lock-tables-and-transactions.html)
I ended up locking outside of (before) the transaction, as that was the first thing I was doing in the transaction anyway. Alternatively, you can do as the docs suggest:
SET autocommit=0;
LOCK TABLES t1 WRITE, t2 READ, ...;
... do something with tables t1 and t2 here ...
COMMIT;
UNLOCK TABLES;
I have the same problem, but i has checked the innodb support
First:
Verify if the Engine INNODB is Available: with this:
mysql> show engines;
if INNODB is disabled: then
Second:
go to the file "my.ini", in my case is in C:\AppServ\mysql
in this file removes the "#" of the line
#default-storage-engine=INNODB -> default-storage-engine=INNODB
and the line skip-innodb add "#"
skip-innodb -> #skip-innodb
Third:
save and restart mysql service, and the problem was solved.
I think there is one important thing to mention: to re-enable autocommit after transaction is done.
SET autocommit = 0;
START TRANSACTION;
INSERT INTO ..
UPDATE <table> ..
COMMIT;
SET autocommit = 1;
Otherwise anything you do after disabling autocommit even without transaction will require you to explicitly commit.
If you're using MySQL Workbench you can turn off Auto-commit transactions in 'Query' tab.
And don't forget to use "COMMIT;" after the Query which you need to rollback.
Even for me rollback was not working for InnoDB engine.
But adding begin; statement after start transaction; made the fix.
This worked for me
SET autocommit = 0;
start transaction;
begin;
-- DML query goes here
rollback;
SET autocommit = 1;
But
This doesn't worked for me
SET autocommit = 0;
start transaction;
-- DML query goes here
rollback;
SET autocommit = 1;
Really i don't know the reason, If anyone knows please comment here.