ホーム 全セッション S06

競合時の Err の返却

12 / 20 目安 1分

ページ一覧

解決策: 取得した後、保存の前に別の依頼が先に診察を開始していた競合を、Err で返す実装を確認します。

予約を取得した後、保存の前に、別の依頼が先に診察を開始していた場合を、競合と呼びます。examinationStartedStore は、保存の時点で予約が CheckedIn でなければ、競合として AppointmentConflict を Err で返します。

状態と監査記録を1回で保存する

修正後: 競合

  1. ユースケースが予約を取得した後、保存の前に、別の依頼が先に診察を開始していました。データベースの予約はすでに InExamination で、audit_logs には先の依頼の ExaminationStarted が保存済みです。
  2. transaction の中で再取得した予約の current.kind は、CheckedIn ではありません。
  3. current が CheckedIn ではないため、何も書き込まずに err で AppointmentConflict を返します。current === undefined(予約が存在しない)のときも、同じ AppointmentConflict を返します。関数は例外を投げずに完了するため transaction は確定しますが、書き込みの前に返すため、状態も監査記録も増えず、audit_logs は先の依頼の1行のままです。

呼び出し元のコード

修正前 examples/session-06/src/useCase/startExamination.ts:69-70 呼び出し元
await deps.stateStore.save(event.aggregateState);
await deps.eventLog.append(event);
修正後 examples/session-06-solution/src/useCase/startExamination.ts:89 呼び出し元
.andThrough(deps.examinationStartedStore.store)

実装のコード

修正前 examples/session-06/src/adaptor/secondary/sqlite/appointmentStore.ts:182, 100-102 実装
save: async (appointment) => saveState(database, appointment),
…
executor
  .insert(appointmentsTable)
  .values(row)
修正前 examples/session-06/src/adaptor/secondary/sqlite/appointmentStore.ts:185, 118, 120 実装
append: async (event) => appendAudit(database, event),
…
executor.insert(auditLogsTable).values(toAuditRow(event)).run();
…
throw new AppointmentPersistenceError("append-audit", cause);
修正後 examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:185-188 実装
store: (event) =>
  ResultAsync.fromSafePromise(
    Promise.resolve().then(() => storeAtomically(event)),
  ).andThen((result) => result),
修正後 examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:148-149 実装
const storeAtomically = (event: ExaminationStarted) =>
  database.transaction((transaction) => {
    …
修正後 examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:150-151 実装
const current = findInTransaction(transaction, event.appointmentId);
if (current === undefined || current.kind !== "CheckedIn") {
  …
修正後 examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:158-161 実装
const committedEvent = Appointment.startExamination({
  eventId: event.eventId,
  occurredAt: event.occurredAt,
})(current, event.aggregateState.veterinarianId);
修正後 examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:151-156 実装
if (current === undefined || current.kind !== "CheckedIn") {
  return err({
    kind: "AppointmentConflict",
    appointmentId: event.appointmentId,
  } as const);
}
修正後 examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:163, 99-101, 106 実装
saveState(transaction, committedEvent.aggregateState);
…
executor
  .insert(appointmentsTable)
  .values(row)
  …
  .run();
修正後 examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:164, 117, 119 実装
appendAudit(transaction, committedEvent);
…
executor.insert(auditLogsTable).values(toAuditRow(event)).run();
…
throw new AppointmentPersistenceError("append-audit", cause);
修正後 examples/session-06-solution/src/adaptor/secondary/sqlite/examinationStartedStore.ts:149, 165-166 実装
database.transaction((transaction) => {
  …
  return ok(undefined);
});

「保存済み」ははじめからある行、「未確定」は transaction の中だけにある行、「確定」は書き込みが残った行です。

修正前のコードの保存は Promise<void> を返すだけで、この競合を Err で返せません。この教材のコードでは、SQLite が transaction を1つずつ順に実行するため、再取得と書き込みの間に別の依頼の書き込みが入りません。ほかのデータベースで競合を検出する方法は、Final の条件付き更新で扱います。