Skip to content

Instantly share code, notes, and snippets.

@cocoiti
Created December 10, 2014 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cocoiti/ede09c3ddb612daa0c38 to your computer and use it in GitHub Desktop.
Save cocoiti/ede09c3ddb612daa0c38 to your computer and use it in GitHub Desktop.
<?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