Files
lincodex/backend/internal/port/outbound/transaction.go
T

78 lines
3.3 KiB
Go

package outbound
import (
"context"
"errors"
"lincodex/backend/internal/domain"
)
var (
// ErrOutboxEventConflict reports that the event ID, or the same event type
// for the same aggregate version, is already recorded. The surrounding
// transaction is aborted and rolled back.
ErrOutboxEventConflict = errors.New("outbox event already recorded")
// ErrNestedTransaction reports a WithinTransaction call from inside another
// transaction callback. Nested calls would silently use a second connection
// and commit independently of the outer transaction.
ErrNestedTransaction = errors.New("transaction already in progress")
// ErrTransactionConflict reports that PostgreSQL aborted the work with a
// deadlock (SQLSTATE 40P01) or a serialization failure (40001). Nothing
// was committed. The caller may retry the whole operation, ideally with a
// short backoff and a bounded number of attempts; a usecase or job that
// retries must stay idempotent.
ErrTransactionConflict = errors.New("transaction conflict")
)
// Transactor runs a business operation in one database transaction.
type Transactor interface {
// WithinTransaction commits when fn returns nil and rolls back when fn
// returns an error or panics. The error keeps its identity for errors.Is
// and errors.As; a deadlock or serialization failure, from fn or from the
// commit, additionally matches ErrTransactionConflict. A panic is
// re-raised after the rollback. fn must use only the
// UnitOfWork and context it receives and must not retain them after
// returning. Calling WithinTransaction with that context returns
// ErrNestedTransaction. After a database error inside fn the transaction is
// aborted, so fn should return that error.
//
// A network failure during commit leaves the outcome unknown: the
// transaction may or may not have committed. Callers that retry must make
// the operation idempotent, for example through aggregate version checks
// and the outbox uniqueness rules.
WithinTransaction(ctx context.Context, fn func(ctx context.Context, unit UnitOfWork) error) error
}
// UnitOfWork exposes repositories bound to one transaction. Product
// repositories are added here as their usecases appear.
type UnitOfWork interface {
IdentityRepositories
ConsentRepositories
AccountDeletionRequests() AccountDeletionRequests
ConsentRevocationEffects() ConsentRevocationEffects
OrganizationRepositories
AssessmentBankRepositories
Policies() PolicyRepository
Deliveries() DeliveryRepository
MailRequests() MailRequestRepository
MailQuotas() MailQuotaRepository
Outbox() OutboxWriter
ScheduledJobs() JobScheduler
}
// OutboxWriter appends domain events to the transactional outbox. Events become
// visible to the relay only after the surrounding transaction commits.
//
// The outbox rejects a second event of the same type for the same aggregate
// version. That rule is a backstop, not concurrency control: aggregate updates
// must still check the expected version (UPDATE ... WHERE version = $n) so a
// stale writer fails before it records an event.
type OutboxWriter interface {
// Append returns an error wrapping ErrOutboxEventConflict for a duplicate
// and domain.ErrInvalidDomainEvent for an event not built by
// domain.NewDomainEvent.
Append(ctx context.Context, events ...domain.DomainEvent) error
}