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

26.2k views Asked by At

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?

2

There are 2 answers

6
Jonnycake On BEST ANSWER

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).

0
Super Kai - Kazuya Ito On

With the MySQL query below, you can check if autocommit is on("1") or off("0"):

SELECT @@autocommit; -- "1" or "0"