Skip to content

Commit

Permalink
fix(orm): make sure persistence is always closed on flush
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Mar 26, 2024
1 parent ab12802 commit 015d90a
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions packages/orm/src/database-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ import { getClassState, getInstanceState, getNormalizedPrimaryKey, IdentityMap }
import { getClassSchemaInstancePairs } from './utils.js';
import { HydratorFn } from './formatter.js';
import { getReference } from './reference.js';
import { DatabaseErrorInsertEvent, DatabaseErrorUpdateEvent, onDatabaseError, UnitOfWorkCommitEvent, UnitOfWorkEvent, UnitOfWorkUpdateEvent } from './event.js';
import {
DatabaseErrorInsertEvent,
DatabaseErrorUpdateEvent,
onDatabaseError,
UnitOfWorkCommitEvent,
UnitOfWorkEvent,
UnitOfWorkUpdateEvent,
} from './event.js';
import { DatabaseLogger } from './logger.js';
import { Stopwatch } from '@deepkit/stopwatch';
import { EventDispatcher, EventDispatcherInterface, EventToken } from '@deepkit/event';
Expand Down Expand Up @@ -540,44 +547,38 @@ export class DatabaseSession<ADAPTER extends DatabaseAdapter = DatabaseAdapter>
this.currentPersistence = this.adapter.createPersistence(this);
}

if (!this.rounds.length) {
//we create a new round
this.enterNewRound();
}
try {
if (!this.rounds.length) {
//we create a new round
this.enterNewRound();
}

//make sure all stuff in the identity-map is known
const round = this.getCurrentRound();
if (this.withIdentityMap) {
for (const map of this.identityMap.registry.values()) {
for (const item of map.values()) {
round.add(item.ref);
//make sure all stuff in the identity-map is known
const round = this.getCurrentRound();
if (this.withIdentityMap) {
for (const map of this.identityMap.registry.values()) {
for (const item of map.values()) {
round.add(item.ref);
}
}
}
}

if (this.eventDispatcher.hasListeners(DatabaseSession.onCommitPre)) {
const event = new UnitOfWorkCommitEvent(this);
await this.eventDispatcher.dispatch(DatabaseSession.onCommitPre, event);
if (event.stopped) return;
}

//we need to iterate via for i, because hooks might add additional rounds dynamically
for (let i = 0; i < this.rounds.length; i++) {
const round = this.rounds[i];
if (round.isCommitted() || round.isInCommit()) continue;
if (this.eventDispatcher.hasListeners(DatabaseSession.onCommitPre)) {
const event = new UnitOfWorkCommitEvent(this);
await this.eventDispatcher.dispatch(DatabaseSession.onCommitPre, event);
if (event.stopped) return;
}

try {
//we need to iterate via for i, because hooks might add additional rounds dynamically
for (let i = 0; i < this.rounds.length; i++) {
const round = this.rounds[i];
if (round.isCommitted() || round.isInCommit()) continue;
await round.commit(this.currentPersistence);
} catch (error) {
this.rounds = [];
this.currentPersistence.release();
this.currentPersistence = undefined;
throw error;
}
} finally {
this.currentPersistence.release();
this.currentPersistence = undefined;
this.rounds = [];
}

this.currentPersistence.release();
this.currentPersistence = undefined;
this.rounds = [];
}
}

0 comments on commit 015d90a

Please sign in to comment.