Sequence Repositories
OpenFrame Documentation
Get the most out of OpenFrame · Book 30 minutes with Conrad, 25 years in IT. He'll show you what Mingo and Fae can take off your list.
OpenFrame Documentation
Sequence Repositories provide monotonic, database-backed integer counters for the MongoDB persistence layer. The module exposes one sequence operation, getNextValue(sequenceName), through the shared SequenceRepository contract and selects exactly one implementation at application startup according to tenant-isolation configuration.
The implementation is supplied by the OpenFrame OSS Library artifact com.openframe.oss:openframe-data-mongo-sync at version 7.0.11. It runs as part of this repository's services rather than being implemented in this repository.
A caller supplies a logical sequence name. The active repository locates that sequence's TenantSequence document, atomically increments its value field, creates the document when it is absent, and returns the incremented value. The use of MongoDB findAndModify with returnNew and upsert means the returned value is the post-increment value from the same database operation.
The module deliberately changes its scope, not its public operation, between deployment modes:
DefaultSequenceRepository is active when openframe.tenant-isolation.enabled is false or unspecified. A sequence is identified by its name alone, providing one counter per name for a single-tenant or OSS deployment.SequenceRepositoryImpl is active only when openframe.tenant-isolation.enabled is true. It delegates to TenantAwareMongoTemplate, which automatically adds the current tenant scope to queries for TenantSequence and stamps the tenant when an upsert creates the document. This gives each tenant an independent counter for the same sequence name.flowchart TD
Application["Service code"] --> Contract["SequenceRepository contract"]
Property["Tenant isolation property"] --> Selection{"Tenant isolation enabled?"}
Selection -->|"false or missing"| DefaultRepository["Default Sequence Repository"]
Selection -->|"true"| TenantRepository["Tenant Sequence Repository"]
Contract --> DefaultRepository
Contract --> TenantRepository
DefaultRepository --> MongoTemplate["MongoTemplate"]
TenantRepository --> TenantTemplate["Tenant Aware Mongo Template"]
MongoTemplate --> Collection["TenantSequence collection"]
TenantTemplate --> Collection
Spring evaluates the conditional repository annotations during bean creation. The conditions are complementary: the default implementation matches an absent property as well as an explicit false value, while the tenant-aware implementation requires explicit true. Consequently, normal configuration produces one active SequenceRepository bean rather than two competing implementations.
DefaultSequenceRepository is the repository for non-isolated deployments.
MongoTemplate directly.TenantSequence by name only.value by one through an atomic findAndModify operation.SequenceRepositoryImpl is the tenant-aware counterpart.
Its increment operation has the same query, update, upsert, return, and debug logging behavior as the default repository. The difference is the persistence adapter: TenantAwareMongoTemplate applies tenant scoping to the query and the tenant-stamping callback assigns the tenant identifier when an upsert creates a TenantSequence. The repository therefore does not manually construct a composite key or manually set a tenant field.
flowchart LR
Caller["Caller requests next value"] --> Name["Sequence name"]
Name --> ActiveRepository["Active sequence repository"]
ActiveRepository --> Query["Match sequence name"]
ActiveRepository --> Increment["Increment value by one"]
TenantRepository["Tenant-aware mode"] -.-> Scope["Template adds tenant scope"]
Scope -.-> Query
Query --> AtomicOperation["Find and modify with upsert"]
Increment --> AtomicOperation
AtomicOperation --> UpdatedDocument["Updated TenantSequence"]
UpdatedDocument --> ReturnedValue["Return incremented value"]
In tenant-aware mode, the template's scope is part of the database operation before the document is located or created. In default mode, no tenant condition is added. In both modes, a sequence name must be treated as the stable logical identity of the counter: changing it starts or addresses a different counter.
TenantSequence document, so callers do not need a separate read after allocation.Sequence Repositories are part of the synchronous Mongo persistence library consumed by the OpenFrame OSS Tenant services. The shared data layer also provides Mongo configuration, tenant-aware persistence infrastructure, and domain-specific repositories. This module is narrowly responsible for allocating counters; it neither defines the business entities using those counters nor exposes an HTTP or messaging interface.