I have code that looks like this:
function foobar(array& $objects, $con = null)
{
if (is_null($con))
$con = DbSingleton::getConnectio();
$con->beginTransaction(); // <- question 1
try
{
foreach($objects as $object)
{
// allocate memory for new object
$new_obj = new MyShiningNewObject();
// do something to the new object ...
$new_obj->setParentId($object->getId());
$new_obj->save($con);
// mark for garbage collection
unset($new_obj); // <- question 2
}
$con->commit();
}
catch(Exception $e){ $con->rollBack(); }
}
My questions are:
I am begining a transaction, this could well be a nested transaction. In the case of a nested transaction, if an exception is thrown and I rollback, how far back does the rollback go (to the outermost transaction) - common sense suggest that this should be the case, but one never knows.
I am freeing memory (ok, marking as 'freeable' by the Zend GC). Since I am commiting the transaction AFTER the loop, (the variable is marked as frreable IN the loop), is this safe - I mean will the data be safely stored in the db even though I have unset the variable that the value came from?