mysql_autocommit(3)

πŸ“¦
version 13.0.2 Β· 4 versions
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, debian trixie, fedora 43, fedora 44, nixpkgs unstable, opensuse-leap 16.0, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ Name ✨

mysql_autocommit β€” the little function that toggles autocommit mode on and off, and honestly? it's giving "small but mighty" πŸ’…. This is the MariaDB Connector/C page, bestie, so pull up a chair.


πŸŽ€ Synopsis πŸŽ€

#include <mysql.h>

my_bool mysql_autocommit(MYSQL * mysql, my_bool auto_mode);

πŸ¦‹ Description πŸ¦‹

Okay so literally what this does: it toggles autocommit mode on or off for the current database connection. That's it, that's the whole vibe.

Autocommit mode gets set if mode=1, and unset if mode=0. No in-between, no maybe, it's a binary situation and we respect that. 🫢


πŸ’– Parameters πŸ’–

  • mysql β€” your connection identifier, the main character here. It was previously allocated by mysql_init(3) and connected by mysql_real_connect(3), so don't just roll up with a random pointer, bestie, it needs to be a real one. πŸ”₯
  • auto_mode β€” whether to turn autocommit on or not. That's literally the whole job description.

🌸 Notes 🌸

  • Autocommit mode only affects operations on transactional table types. If you wanna know the current state of autocommit mode, use the SQL command SELECT @@autocommit, or check the server status (there's an example below, iykyk πŸ‘€).
  • > Be aware: the [mysql_rollback()}(mysql_rollback() function will not work if autocommit mode is switched on. Not a drill. Not a vibe. It just won't work. 😭

πŸ‘‘ Examples πŸ‘‘

πŸ“ SQL

# Turn off autocommit
SET AUTOCOMMIT=0;

# Retrieve autocommit
SELECT @@autocommit;
+--------------+
| @@autocommit |
+--------------+
|            0 |
+--------------+

Look at that 0. Immaculate. ✨


🌈 MariaDB Connector/C

static int test_autocommit(MYSQL *mysql)
{
  int rc;
  unsigned int server_status;

  /* Turn autocommit off */
  rc= mysql_autocommit(mysql, 0);
  if (rc)
    return rc; /* Error */

  /* If autocommit = 0 succeeded, the last OK packet updated the server status */
  rc= mariadb_get_infov(mysql, MARIADB_CONNECTION_SERVER_STATUS, &server_status);
  if (rc)
    return rc; /* Error */

  if (server_status & SERVER_STATUS_AUTOCOMMIT)
  {
    printf("Error: autocommit is on\n");
    return 1;
  }
  printf("OK: autocommit is off\n");
  return 0;
}

Not us reading the code and being like "the way this checks server_status & SERVER_STATUS_AUTOCOMMIT and prints OK: autocommit is off"... we love a function that actually verifies its own work. πŸ’€πŸ’–


Fr fr: keep every flag, every parameter and every caveat in your head, because the manpage said it once and we are not repeating ourselves. Periodt.

Version 3.4 of the MariaDB Connector/C manual, bestie β€” that's the source and the version, and this page is mysql_autocommit(3), so go forth and toggle responsibly. Mwah, love u, bye! πŸŽ€πŸ’…πŸ¦‹πŸŒˆπŸ‘‘

πŸ“¦ where this page comes from