Created
December 10, 2014 11:08
-
-
Save cocoiti/ede09c3ddb612daa0c38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace XaTestBundle\Command; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use XaTestBundle\Doctrine\TransactionManager; | |
class XatestCommand extends ContainerAwareCommand | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function configure() | |
{ | |
$this | |
->setName('xatest:run') | |
->setDescription('xatest') | |
; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$doctrine = $this->getContainer()->get('doctrine'); | |
$first = $doctrine->getConnection('first'); | |
$second = $doctrine->getConnection('second'); | |
$transactionSuccess = new TransactionManager([ | |
$first, | |
$second, | |
]); | |
$transactionSuccess->beginTransaction(); | |
try { | |
$first->insert('transaction1', [ | |
'data' => $first->getXid(), | |
]); | |
$second->insert('transaction2', [ | |
'data' => $second->getXid(), | |
]); | |
$transactionSuccess->commit(); | |
} catch (\Exception $e) { | |
var_dump($e->getMessage()); | |
$transactionSuccess->rollback(); | |
} | |
$transactionFail = new TransactionManager([ | |
$first, | |
$second, | |
]); | |
$transactionFail->beginTransaction(); | |
try { | |
$first->insert('transaction1', [ | |
'data' => $first->getXid(), | |
]); | |
$second->insert('transaction2', [ | |
'data' => $second->getXid(), | |
]); | |
throw new \Exception(); | |
$transactionFail->commit(); | |
} catch (\Exception $e) { | |
$transactionFail->rollback(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment