/symfony-skills:transactional-patternsfrom symfony-skills
Manages Doctrine transaction boundaries, flush strategy, and optimistic/pessimistic locking in Symfony. Activates on mentions of transaction, flush, locking, concurrency, race condition, or unit of work.
Ships in symfony-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Doctrine's Unit of Work batches changes; flush() is the commit boundary. Call it once, at the end of the use case, in the service layer — not after every persist(), and never in controllers, repositories, or entities.
// ✅ GOOD — service owns the boundary, single flush
public function placeOrder(CreateOrderRequest $request): Order
{
$order = Order::create($request->customerEmail);
foreach ($request->items as $line) {
$order->addItem(Uuid::fromString($line->productId), $line->quantity);
}
$this->inventory->reserve($order); // also just persists, doesn't flush
$this->em->persist($order);
$this->em->flush(); // one commit for the whole use case
return $order;
}
// ❌ BAD — flush in a loop: N transactions, partial writes on failure
foreach ($lines as $line) {
$em->persist($line);
$em->flush(); // commits each line separately
}
When several operations must succeed or fail together (and a single flush() isn't enough — e.g. raw DBAL + ORM, or you need to react before commit), wrap them:
$this->em->wrapInTransaction(function () use ($order, $payment): void {
$this->em->persist($order);
$this->em->persist($payment);
// any exception in here rolls everything back
});
wrapInTransaction() (Doctrine ORM 3) begins, flushes, commits, and rolls back on exception. Prefer it over manual beginTransaction()/commit()/rollback() so you can't forget the rollback path.
Two users edit the same record; the second overwrites the first ("lost update"). Guard with a #[ORM\Version] column.
#[ORM\Version]
#[ORM\Column(type: Types::INTEGER)]
private int $version = 1;
On flush, if the version in the DB changed since you loaded the entity, Doctrine throws OptimisticLockException. Map it to 409 Conflict.
try {
$this->em->flush();
} catch (OptimisticLockException) {
throw new ConcurrentModificationException($order->getId()); // -> 409
}
This needs no DB locks and scales — preferred for typical request/response flows.
For short critical sections with real contention (inventory decrement, seat booking), lock the row at read time inside a transaction:
$this->em->wrapInTransaction(function () use ($id): void {
$product = $this->em->find(
Product::class,
$id,
LockMode::PESSIMISTIC_WRITE, // SELECT ... FOR UPDATE
);
$product->decrementStock(1); // no one else can read-for-update until commit
});
Hold the lock for the shortest possible time; long-held row locks cause contention and deadlocks. Don't use pessimistic locking as a default — it serializes access.
flush() only what you mean toflush() persists every managed entity with pending changes, not just the one you pass. Don't rely on flush($entity) to scope it (removed in ORM 3).flush() inside Doctrine lifecycle callbacks or event listeners mid-request — it reorders the UoW and causes subtle bugs. Defer side effects to postFlush/after commit (see domain events in domain-driven-design).When a write must also notify another system, don't open a transaction across HTTP/queue boundaries. Persist an outbox row in the same transaction, then dispatch asynchronously (see messenger-async). This keeps the DB write atomic and the side effect eventually consistent.
flush() inside a loop — flush once after all persist() calls.flush() / transaction logic in a controller, repository, or entity — own it in the service.beginTransaction()/commit()/rollback() and forgets the rollback path — use wrapInTransaction().#[ORM\Version] and map OptimisticLockException to 409.FOR UPDATE locks by default — use optimistic locking unless there's real contention.flush($entity) to scope the commit — ORM 3 flushes the whole UoW.flush() inside a Doctrine lifecycle listener — defer to postFlush/after commit.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
doctrine, symfony, transaction-management, locking, flush-strategy
17 skills in this plugin
/api-platform-resourcesUse when exposing data through API Platform 4 — defining #[ApiResource], operations, DTO resources, state providers/processors, serialization groups, filters, and pagination. Use when the task mentions API Platform, ApiResource, state processor, or exposing an entity as a REST/GraphQL endpoint.
/bundle-organizationUse when structuring a Symfony project's src/ directory, organizing code into modules, configuring services, or deciding where a class belongs. Covers the modular monolith layout, service configuration, and config/ conventions for Symfony 7 (Flex, no app bundle).
/console-commandsUse when creating or refactoring Symfony console commands — never extends Command. On Symfony 8.1+ put #[AsCommand] on a method of a service class (even for a single command); on 7.x use the invokable attribute class. Also covers typed CLI inputs via value resolvers and #[MapInput] DTOs. Use when the task mentions console command, #[AsCommand], bin/console, #[Argument], #[Option], or a CLI task.
/doctrine-migrationsUse when changing the database schema in Symfony — generating Doctrine migrations, writing reversible up()/down(), data migrations, and deploy-safe (zero-downtime) changes. Use when the task mentions migration, schema change, ALTER TABLE, or doctrine:migrations.
/doctrine-ormUse when creating Doctrine ORM 3 entities, associations, repositories, enums, or UUID identifiers in Symfony. Defines entity conventions, attribute mapping, fetch strategy, and repository structure. Use when the task mentions entity, Doctrine, repository, association, or mapping.
/doctrine-query-optimizationUse when writing Doctrine queries, fixing N+1 problems, paginating large result sets, building read models, or tuning DQL/query builder performance. Use when the task mentions N+1, slow query, pagination, DQL, hydration, or read model.
npx claudepluginhub fatonh/symfony-skills --plugin symfony-skillsOther plugins' skills for the same job