Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get the default value of autocommit in MySQL?

I'm just wondering as per the title if this can be done? For example, if I was to do something like this:

$this->db->autocommit(false);

But then afterwards I wanted to set it back to it's default value; I can't just assume it's defaults to true and do this:

$this->db->autocommit(true);

I would like to reset it back to it's default value if possible; or what do other people do when wanting a single query to auto commit? Do you always run:

$this->db->autocommit(true);

...before the query to make sure that autocommit is on?

like image 534
Brett Avatar asked Nov 13 '14 16:11

Brett


People also ask

What is the default value of autocommit in MySQL?

By default, MySQL starts the session for each new connection with autocommit enabled, so MySQL does a commit after each SQL statement if that statement did not return an error. If a statement returns an error, the commit or rollback behavior depends on the error. See Section 14.21. 4, “InnoDB Error Handling”.

How do I check my autocommit?

To determine the current state of autocommit mode use the SQL command SELECT @@autocommit . Be aware: the mysql_rollback() function will not work if autocommit mode is switched on.

What is autocommit default?

By default, JDBC uses an operation mode called auto-commit. This means that every update to the database is immediately made permanent. Any situation where a logical unit of work requires more than one update to the database cannot be done safely in auto-commit mode.

Can we ROLLBACK after COMMIT in MySQL?

No, there's no query that will "undo" a committed data-modifying query. If you have a backup of the database, you can restore the backup and use DBA tools (in MySQL's case, it's mysqlbinlog) to "replay" all data-modifying queries from the logs since the backup back to the database, but skip over the problem query.


1 Answers

http://php.net/manual/en/mysqli.autocommit.php

To determine the current state of autocommit use the SQL command SELECT @@autocommit.

It seems silly to determine if it's on and if not turn it on though.

Another way would be using:

SHOW VARIABLES WHERE Variable_name='autocommit';

Also autocommit among other variables are on a per-connection basis, you setting autocommit to true will not affect any other connections. The only way to find what the default value is, is by checking right when the connection is made (before changing the state).

like image 105
Jonnycake Avatar answered Oct 01 '22 19:10

Jonnycake