Transactional Patternsのロゴ

Transactional Patterns

コミュニティ
fatonh
transactional-patterns

Use when managing Doctrine transaction boundaries, flush strategy, optimistic/pessimistic locking, or ensuring consistency across multiple writes in Symfony. Use when the task mentions transaction, flush, locking, concurrency, race condition, or unit of work.

概要

公開者fatonh
リポジトリsymfony-skills
スキル名transactional-patterns
スター
36
フォーク
3
同梱ファイル
指示のみ
ライセンスMIT
リンク
  • Markdown形式の指示

    モデルが必要に応じて読み込むSKILL.mdファイルです。関連するリクエストがあったときだけトークンを消費します。

  • どのLLMでも利用可能

    AIスキルはプロバイダー固有のコードではなく、通常のMarkdownなので、GPT、Claude、Gemini、Grok、ローカルモデルで利用できます。

  • 指示だけで完結

    モデルに必要なものはすべて指示に含まれているため、別途同期するファイルはありません。

  • オープンソース

    fatonhがGitHubで公開しています。インストールする前にソースを確認してください。

インストール

AIスキルTransactional PatternsをTypingMindにインストールすれば、どのLLMでも利用できます。SKILL.mdを読み取れる別のAIエージェントに追加することもできます。

1

TypingMindにインストール

TypingMindはGitHubフォルダから直接スキルをインストールします。SKILL.mdを読み取り、リソースファイルをまとめてローカルに保存します。

  1. アプリを開き、「プラグイン」→「スキル」に移動します。
  2. 「GitHubからインストール」を選択します。
  3. 以下のスキルフォルダのURLを貼り付けて確定します。
    https://github.com/fatonh/symfony-skills/tree/main/skills/transactional-patterns
  4. スキルを利用したいチャットごとに、そのスキルを有効にしてください。
「プラグイン」→「スキル」→「スキルを追加」→「GitHubのURLから」を選択し、フォルダのURLを貼り付けて「続行」を押します。
2

別のAIエージェントにインストール

Agent Skills形式を読み取れるAIエージェントなら、このスキルを利用できます。フォルダをそのAIエージェントのスキルディレクトリにコピーしてください。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/fatonh/symfony-skills.git /tmp/symfony-skills
mkdir -p .claude/skills
cp -r /tmp/symfony-skills/skills/transactional-patterns .claude/skills/transactional-patterns
コピー後、新しいスキルを読み込むためにClaude Codeを再起動してください。

TypingMindで使用

TypingMindのチャットでTransactional Patternsを有効にするだけで、あとはモデルが対応します。名前と説明がシステムプロンプトに含まれ、関連するリクエストがあると、モデルが指示の全文を自動で読み込みます。手動で呼び出す必要はなく、実際に使用されるまでトークンを消費しません。

関連するリクエストがあると、モデルが自動的にTransactional Patternsを読み込みます。

どのAIモデルでも利用可能

AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、Transactional Patternsは作成時に想定されたモデルに縛られません。TypingMindに一度インストールすれば、ご自身のAPIキーを使って、GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama、またはご自身で実行するローカルモデルで利用できます。

  • 必要なときだけ読み込み

    システムプロンプトには名前と説明だけが含まれます。関連するリクエストが初めてあったときに指示を取得するため、未使用のスキルにはコストがかかりません。

  • チャットの途中でモデルを切り替え

    スキルはコードではなく指示なので、モデルを切り替えてもそのまま使えます。次のモデルも同じSKILL.mdを読み込みます。

スキルの指示

モデルが読み込むSKILL.mdの内容です。スキルはモデルが従う指示なので、インストールする前に確認してください。

Transactional Patterns (Doctrine + Symfony)

One flush per request/use case

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.

php
// ✅ 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
}

Explicit transactions for multi-step consistency

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:

php
$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.

Optimistic locking (the default for web apps)

Two users edit the same record; the second overwrites the first ("lost update"). Guard with a #[ORM\Version] column.

php
#[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.

php
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.

Pessimistic locking (only for true contention)

For short critical sections with real contention (inventory decrement, seat booking), lock the row at read time inside a transaction:

php
$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 to

  • A flush() 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).
  • Avoid triggering 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).

Cross-service consistency: outbox, not distributed transactions

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.

Gotchas

  • Agent calls flush() inside a loop — flush once after all persist() calls.
  • Agent puts flush() / transaction logic in a controller, repository, or entity — own it in the service.
  • Agent uses manual beginTransaction()/commit()/rollback() and forgets the rollback path — use wrapInTransaction().
  • Agent ignores concurrent edits — add #[ORM\Version] and map OptimisticLockException to 409.
  • Agent reaches for pessimistic FOR UPDATE locks by default — use optimistic locking unless there's real contention.
  • Agent relies on flush($entity) to scope the commit — ORM 3 flushes the whole UoW.
  • Agent opens a transaction spanning an external HTTP/queue call — use the outbox pattern + async dispatch.
  • Agent calls flush() inside a Doctrine lifecycle listener — defer to postFlush/after commit.

よくある質問

AIスキル「Transactional Patterns」では何ができますか?

Use when managing Doctrine transaction boundaries, flush strategy, optimistic/pessimistic locking, or ensuring consistency across multiple writes in Symfony. Use when the task mentions transaction, flush, locking, concurrency, race condition, or unit of work.

TypingMindでTransactional Patternsを使うメリットは何ですか?

一度インストールすれば、どのモデルでも使えるからです。Transactional Patternsはプロバイダー固有のコードではなく、通常のMarkdownで記述されているため、同じスキルをGPT-5、Claude、Gemini、Grok、ローカルモデルで利用できます。チャットの途中でモデルを切り替えても、そのまま使い続けられます。TypingMindは自分のAPIキーで動作するため、シート単位のサブスクリプションではなく、プロバイダーに直接料金を支払います。スキルとチャットは自分のストレージに保持されます。

TypingMindにTransactional Patternsをインストールするにはどうすればよいですか?

TypingMindで「プラグイン」→「スキル」→「GitHubからインストール」を開き、https://github.com/fatonh/symfony-skills/tree/main/skills/transactional-patternsを貼り付けてください。TypingMindがSKILL.mdを読み込み、チャットごとに有効にできるスキルとしてインストールします。

Transactional Patternsを使えるAIモデルはどれですか?

TypingMindに接続したどのモデルでも利用できます。AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、GPT、Claude、Gemini、Grok、ローカルモデルのいずれも、リクエストが合致したときにこのスキルを読み込めます。

Transactional Patternsは何個のAIモデルで使えますか?

数に制限はありません。スキルに対応したモデルであれば、GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llamaなど、どのモデルでもTransactional Patternsを利用できます。すべてTypingMind上で、自分のAPIキーを使って利用できます。

AIスキル「Transactional Patterns」は無料ですか?

はい。fatonhがGitHubでMITライセンスの下で公開しています。支払いが必要なのは、利用したトークンに対する、ご自身が契約するAIプロバイダーへの料金だけです。

AIスキルとは何ですか?

AIスキルは、特定のタスクの実行方法をAIモデルに教える、再利用可能な指示のセットです。オープンなAgent Skills形式に従い、名前と説明を含むSKILL.mdファイルに加え、モデルが必要とするスクリプト、テンプレート、参照ファイルで構成されます。モデルはリクエストがスキルに合致したときだけ指示を読み込むため、インストール済みのスキルも、使うまでは費用がかかりません。

AIスキルは、プラグインやMCPサーバーとどう違いますか?

プラグインやMCPサーバーは、モデルが呼び出せる新しいツールを提供します。ツールとは、どこかで実行され、結果を返すコードです。一方、AIスキルは、タスクへの取り組み方、従うべき手順、望ましい出力の例といった知識や進め方をモデルに伝えます。スキルは通常のMarkdownで記述されているため、サーバー、APIキー、実行環境は不要で、どのモデルでも利用できます。

すべて見る

今すぐ自分専用のAIワークスペースを作成

ニュースレターに登録すると、新機能や今後のプレゼント企画のお知らせが届きます👇