1

I have a problem where sometimes (once every 1-3 months) a transaction gets run twice, and as a result, data gets entered into the DB twice.

Here's a gist of what goes on:

function foo()
{
   $arr = getsomeArray();
   $db = JFactory::getDbo();

   try
   {
      bar($arr);
   }
   catch(Exception $e)
   {
      $db->transactionRollback();
   }
}

function bar($arr)
{
   $db = JFactory::getDbo();

   $db->transactionStart();

   // series of inserts and updates
   // but if any fail, then throw Exception

   $db->transactionCommit();
   return true;
}

Here, $arr in foo() should contain unique values. The code prevents duplicate entries from being inserted into the DB by checking if that particular entry already exists.

To test that the base checks work, I've mocked $arr to contain 2 values that are the same. I receive an exception as expected.

However, I'm still getting double entries in the DB and I'm stumped as to how it's even possible. The time stamps suggest that a single transaction gets duplicated and both run in parallel. I haven't been able to find any help on this particular issue, mostly because it's not supposed to happen.

I have seen all examples regarding transactions in Joomla/PHP to use transactionStart, transactionCommit, and transactionRollback in a single function, but the code I've inherited has split them across 2 different functions. Will this cause problems with transactions, such as running twice?

ehz350
  • 73
  • 2
  • 12

0 Answers0