Skip to content

DoctrineTransactionMiddleware support for closed connections  #45510

Closed
@marc-mabe

Description

@marc-mabe

Description

One of my message bus commands could run for quite some time and write to DB only at the last bit of processing.
Because of that I would like to close the DB connection before processing the long running job and reopen connection (and repoen transaction) afterwards to write my results to DB.

The problem is now that DoctrineTransactionMiddleware doesn't check if there is an active transaction and in case of exception it tries to rollback a non active transaction which fails in Doctrine\DBAL\ConnectionException with message There is no active transaction..

It would be very welcome if the DoctrineTransactionMiddleware would support this behavior and check if the connection and transaction is still active before trying to roll back.

Example

class MyCommandHandler implements MessageSubscriberInterface {
    public function __construct(
        private EntityManagerInterface $em,
        private Processor $longRunningProcessor,
    ) {
    }

    public function handle(MyCommand $command): void {
        // read from DB
        $entity = $this->em->find(/* ... */);

        // close DB connection before long running processing
        $this->em->getConnection()>close();

        $result = $this->longRunningProcessor->process($entity);

        // Reconnect and restart DB transaction
        $this->em->beginTransaction();

        $entity->setResult($result);
        $this->em->persist($entity);
        // probably do other DB write operations
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions