Using Transactions In Joomla to maintaining ACID Property
Using Transactions In Joomla to maintaining ACID Property: A transaction is a very small unit of a program and it may contain several lowlevel tasks. A transaction in a database system must maintain Atomicity, Consistency, Isolation, and Durability − commonly known as ACID properties − in order to ensure accuracy, completeness, and data integrity.
Atomicity :
Consistency :
Isolation :
Durability :
Example
This will play an important role where we have to perform operations on more than one table at a time.
eg: we have 4 separate Insert query for 4 different tables and we want that if any one of query fails, then all query must be rollback. i.e. either all 4 query execute successfully or none of them.
/**
* Webkul Software.
*
* @category Webkul
* @author Webkul <support@webkul.com>
* @copyright Copyright (c) 2010-2018 Webkul Software Private Limited
* @license https://store.webkul.com/license.html
*/
//to get instance of JDatabaseDriver class
$db=JFactory::getDBO();
try {
//Method to initialize a transaction
$db->transactionStart();
//initialize JDatabaseQuery object for query
$query1=$db->getQuery(true);
$values = array($db->quote('VALUE_1'),
$db->quote('VALUE_2'),
$db->quote('VALUE_3'));
$query->insert($db->quoteName('#__YOUR_TABLE'));
$query->columns($db->quoteName(
array('FIELD_1',
'FIELD_2',
'FIELD_3')));
$query->values(implode(',',$values));
$db->setQuery($query);
$result = $db->execute();
//It is used to commit Transaction.
$db->transactionCommit();
}
catch (Exception $e) {
// catch any database errors.
//It is used to rollback Transactions if query fails.
$db->transactionRollback();
JErrorPage::render($e);
}
Conclusion:
- transactionStart: Method to initialize a transaction.
Definition : transactionStart(boolean $asSavepoint = false) : void
- transactionCommit: Method to commit a transaction.
Definition :transactionCommit(boolean $toSavepoint = false) : void
- transactionRollback: Method to roll back a transaction.
Definition :transactionRollback(boolean $toSavepoint = false) : void
Support
For any query regarding Joomla virtuemart Extensions and add-ons you can communicate with us at:
support@webkul.com
Please visit this link to find more extensions of joomla Webkul Joomla extensions