Difference between revisions of "Using transactions in Joomla"

From Joomla! Documentation

(Add version)
(A few markup changes.)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 
{{version|3.x}}
 
{{version|3.x}}
{{dablink|'''IMPORTANT NOTE:''' Transactions can only be used against transaction-aware storage engine such as InnoDB. All core Joomla tables use InnoDB however 3rd party extensions may not. Check this before using transactions}}
+
{{dablink|'''IMPORTANT NOTE''' Transactions can only be used against transaction-aware storage engine such as InnoDB. All core Joomla tables use InnoDB, however third party extensions may not. Check this before using transactions}}
  
Joomla {{JVer|3.x}} introduced SQL transactions (where supported) via the JDatabaseDriver's transactionStart, transactionCommit and transactionRollback. This supersedes the queryBatch method which was introduced in Joomla {{JVer|2.5}}.
+
Joomla {{JVer|3.x}} introduced SQL transactions (where supported) via the JDatabaseDriver's ''transactionStart'', ''transactionCommit'' and ''transactionRollback''. This supersedes the ''queryBatch'' method introduced in Joomla {{JVer|2.5}}.
  
<source lang="php">
+
<syntaxhighlight lang="php">
 
$db = JFactory::getDbo();
 
$db = JFactory::getDbo();
  
Line 30: Line 30:
 
     JErrorPage::render($e);
 
     JErrorPage::render($e);
 
}
 
}
</source>
+
</syntaxhighlight>
  
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.
+
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.
 +
 
 +
[[Category:Database]]

Latest revision as of 16:51, 6 November 2022

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();

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

    $db->setQuery($query);
    $result = $db->execute();

    $db->transactionCommit();
}
catch (Exception $e)
{
    // catch any database errors.
    $db->transactionRollback();
    JErrorPage::render($e);
}

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.