Can I use Doctrine transaction together with mysql PDO statements? - php

From below code I expect that $query1 will be undone after Exception is
thrown. But it's not.
try {
$this->em->getConnection()->beginTransaction();
PDO::exec($query1 = 'INSERT INTO foo...')
throw new Exception();
PDO::exec($query2 = 'INSERT INTO bar...')
$this->em->flush();
$this->em->getConnection()->commit();
} catch (\Exception $e) {
$this->em->getConnection()->rollBack();
throw $e;
}

Related

Why Rollback() function is needed in Laravel?

DB::beginTransaction();
try{
//my logic
if(bad logic) throw new \Exception();
DB::commit();
return true;
}catch (\Exception $e) {
DB::rollback();
return false;
}
So what will happen if bad logic throws new exception? DB::commit() will not be called, so recored will not be saved in database. So why the hell I need to write DB:rollback() at all?

How do I ensure all exceptions in this DB transaction are caught?

I use Yii and recently started using Transactions with try / catch blocks.
Here's how the code looks right now:
$dbConnection = Yii::app()->db();
try {
$transaction = $dbConnection->beginTransaction();
$dbConnection->createCommand("SELECT * from table_1")
->queryAll();
$transaction->commit();
} catch (Exception $ex) {
$transaction->rollback();
}
Suppose there's an exception with the DB (it's come up while unit-testing), I'm unable to rollback because the PHP dies with a fatal $transaction undefined error.
I'd rather not include isset() checks everywhere..
Is there a simpler way to make this work?
You can check if the exception is an instance of CDbException
$dbConnection = Yii::app()->db();
try {
$transaction = $dbConnection->beginTransaction();
$dbConnection->createCommand("SELECT * from table_1")
->queryAll();
$transaction->commit();
} catch (Exception $ex) {
if ($ex instanceof CDbException)
{
// handle CDBException
// ...
}
$transaction->rollback();
}

PDO throwing errors to my class?

I can enable PDO to throw error as exceptions to PDOException class.
Can I make it to throw it to my exception class?
Right now I have to code like this:
try {
$sql = "query";
$zap = $pdo->query($sql);
$sql_d=array();
if ($zap->execute($sql_d)) {
}
} catch (\PDOException $e) {
throw new dbException($zap->errorInfo(), $sql);
}
I want to PDO throw error to dbException class - not PDOException.

How to throw custom error in PHP mysql

$row=mysql_fetch_array($result, MYSQL_ASSOC) or die(mysql_error())
In this code the mysql_error will be displayed if mysql_fetch_array fails. Is there any way to throw that mysql_error to reload a page instead.
P.S.: I know mysql has been depreciated. Its an old project so please don't suggest in your answers to use mysqli and PDO. I am well aware of it. Please tell me the way to throw reload page as error.
You could use try catch for exception handling in PHP
http://www.php.net/manual/en/language.exceptions.php
try
{
// do something that can go wrong
}
catch (Exception $e)
{
throw new Exception( 'Something wrong', 0, $e);
}
OR
try
{
// do something that can go wrong
}
catch (Exception $e)
{
if ($e instanceof CustomException)
{
// do something
}
else if ($e instanceof OtherException)
{
// do something
}
else
{
// do something
}
}
Or
try
{
}
catch (CustomException $e)
{
}
catch (OtherException $e)
{
}
catch (Exception $e)
{
}

Yii transaction not rollbacked

i have the following piece of code and i pass it some data to generate an exception and test if the transaction rollback is happening. It seems it's not and i'm not sure why.
can someone help me? thanks
$transaction = Yii::app()->db->beginTransaction();
try {
//.....
//call private methods
$category = MyController::saveCategory($params);
$test_saved = MyController::saveTest($params);
MyController::saveCommunity($param); // here is an exception and it should rollback the transaction but it doesn't
$transaction->commit();
} catch(Exception $e) {
$transaction->rollback();
throw new Exception($e);
}
private function saveCommunity($param){
$community = new Community();
$community->user_id = $user_id;
$community->name = $name;
$community->id = 71; // this is a duplicate primary key and will generate an exception
try{
$community->save(false, null);
}catch(Exception $e){
throw $e;
}
return $community;
}
(mysql tables are set to innodb)
Try changing your code responsible for exception throwing:
try{
$community->save(false, null);
}catch(Exception $e){
throw $e;
}
to something like:
if(!$community->save(false, null))
throw new Exception('Error saving');
And remove the exception throwing here:
} catch(Exception $e) {
$transaction->rollback();
//throw new Exception($e);
}
By default pdo doesn't throw exceptions and just ignores errors.
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Categories