Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.