Using transactions in Joomla

From Joomla! Documentation

Joomla Joomla 3.x introduced SQL transactions (where supported) via the JDatabaseDriver's transactionStart, transactionCommit and transactionRollback. This supersedes the queryBatch method introduced in Joomla Joomla 2.5.

$db = JFactory::getDbo();

    $query = $db->getQuery(true);
    $values = array($db->quote('TEST_CONSTANT'), $db->quote('Custom'), $db->quote('/path/to/translation.ini'));
    $query->columns($db->quoteName(array('constant', 'string', 'file')));

    $result = $db->execute();

catch (Exception $e)
    // catch any database errors.

Anything between the transactionStart and transactionCommit methods are not executed until transactionCommit is called. If an exception occurs, we can roll back the changes using the transactionRollback method. This allows us to return the database to the original state if a problem occurs even though we may execute a number of changes to the database's tables.