mysql...transaction question

2004-07-06 Thread bruce
hi..

i'm trying to understand if there's a difference/better reason for doing
transactions using either of the following psuedo approaches...

approach 1 does the commit inside the eval block, whereas approach 2 has the
commit outside the eval block... i've seen sample code with transactions
handled both ways...

approach 1:
eval
{
   $dbh-do(do something);

   # got this far means no errors
   # commit
   $dbh-commit();
};

# check errors/rollback
if ($@)
{
   $dbh-rollback();
}


approach 2:
eval
{
   $dbh-do(do something);
};

# check errors/rollback
if ($@)
{
   $dbh-rollback();
}
else
{
   # commit
   $dbh-commit();
}


any comments/criticisms/thoughts/etc...

thanks...

-bruce


-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]

Re: mysql...transaction question

2004-07-06 Thread mayuran
bruce wrote:
hi..
i'm trying to understand if there's a difference/better reason for doing
transactions using either of the following psuedo approaches...
approach 1 does the commit inside the eval block, whereas approach 2 has the
commit outside the eval block... i've seen sample code with transactions
handled both ways...
approach 1:
eval
{
  $dbh-do(do something);
  # got this far means no errors
  # commit
  $dbh-commit();
};
# check errors/rollback
if ($@)
{
  $dbh-rollback();
}
approach 2:
eval
{
  $dbh-do(do something);
};
# check errors/rollback
if ($@)
{
  $dbh-rollback();
}
else
{
  # commit
  $dbh-commit();
}
any comments/criticisms/thoughts/etc...
thanks...
-bruce
 

I think the commit needs to be inside the eval, because
thats when problems might arise.  Also, I assume that you
at some point set AutoCommit = 0.  I usually do transctions
like this:
sub test {
   eval { $dbh-begin_work };
   if ($@) {
   print begin transaction failed: $@;
   return;
   }
   eval {
   .. do some stuf ..
   $dbh-commit;
   };
   if ($@) {
   eval { $dbh-rollback };
   print transaction failed: $@;
   } else {
   print data committed successfully.\n;
   }
}
hope this helps,
Mayuran.
--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]