-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transaction + Generator #8
Comments
I have not encountered any issues with generators myself. Transactions or not. However, you will have to use savepoints with Firebird. This is achieved by calling If you are using Symfony, you can add a compiler pass to ensure this. Something like: use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class SetupDoctrinePass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$entityManager = $container->get('doctrine.orm.default_entity_manager');
$entityManager->getConnection()->setNestTransactionsWithSavepoints(true);
}
} It's old syntax/code. May be different in Symfony 4.x. What version of kafoso/doctrine-firebird-driver are you using? Things to considerAnnotations could be incorrectIf you are using PHP annotations, it should look something like this: use Doctrine\ORM\Mapping as ORM;
class Message
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\SequenceGenerator(sequenceName="message_seq", initialValue=1, allocationSize=100)
*/
protected $id = null;
//...
} Could you please provide a sample snippet of your annotations? Database trigger at faultThere may be a database trigger, which causes the conflict. |
Thanks for atention; I use:
But the problem remains Code where start Transaction.
|
I dont have trigger...in my database. |
Thanks. I can't really see what goes on inside your service class ( I've made a quick test and it does work for me. SQL: RECREATE TABLE SEQUENCEGENERATORTABLE (
id INT NOT NULL,
PRIMARY KEY(id)
);
CREATE SEQUENCE GEN_SEQUENCEGENERATORTABLE_ID;
ALTER SEQUENCE GEN_SEQUENCEGENERATORTABLE_ID RESTART WITH 0; Doctrine Entity: <?php
namespace Kafoso\DoctrineFirebirdDriver\Test\Resource\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="SEQUENCEGENERATORTABLE")
*/
class SequenceGeneratorEntity
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="SEQUENCE")
* @ORM\SequenceGenerator(sequenceName="GEN_SEQUENCEGENERATORTABLE_ID", initialValue=1, allocationSize=100000)
*/
private $id = null;
/**
* @return null|int
*/
public function getId()
{
return $this->id;
}
} PHP test: ...
public function testCanPersistMultipleInTransaction()
{
$albumA = new Entity\SequenceGeneratorEntity;
$albumB = new Entity\SequenceGeneratorEntity;
$albumC = new Entity\SequenceGeneratorEntity;
$this->_entityManager->beginTransaction();
$this->_entityManager->persist($albumA);
$this->_entityManager->persist($albumB);
$this->_entityManager->persist($albumC);
$this->_entityManager->flush();
$this->_entityManager->commit();
$rows = $this->_entityManager->getConnection()->fetchAll("SELECT * FROM SEQUENCEGENERATORTABLE");
$expected = [
['ID' => 1],
['ID' => 2],
['ID' => 3],
];
$this->assertSame($expected, $rows);
$this->assertSame(1, $albumA->getId());
$this->assertSame(2, $albumB->getId());
$this->assertSame(3, $albumC->getId());
}
... Test passes. A side note
|
Dear friend, First of all I would like to thank you for "A side note" because you are absolutely right. With the test you presented me, I identified where the problem happens.
Persistence Method
Watching, I realized that I'm flushing every time I save. What is wrong.
Remove the flush from the persistence method.
Amazing that the problem repeats with the generator. I used
To inject EntityManager in my controlller. What is the best practice to Inject? |
I see that you are flushing everything. I.e. no argument in the Could you try either of these two things and report back what you find: 1: Flush each entity separately ...
$entity = new MyEntity;
$em->persist($entity);
$em->flush($entity);
... 2: Flush all newly created entities by passing array ...
$entityA = new MyEntity;
$em->persist($entityA);
$entityB = new MyEntity;
$em->persist($entityB);
$em->flush([$entityA, $entityB]);
... |
Dear Friend; I appreciate your time and all the tips. But as for the generator problem I couldn't solve it. So I made a trigger to check the generator, and correct if necessary. Thank you very mutch. |
Hello,
Dear friend, I have a business class that saves twice, two records in the same table, so the generator should add twice. But it has only one more number. All in the same transaction.
Example: generator = 234
register 235 save and 236 save, but generator show me 235.
I have a problem when saving next register. duplicate primary key.
The text was updated successfully, but these errors were encountered: