跳至内容
LoadUp Components Database

LoadUp Components Database

MyBatis-Flex and Spring JDBC support for persistent IDs, PostgreSQL transactions, sequence allocation and one fixed business schema per deployment.

Maven

<dependency>
    <groupId>io.github.loadup-cloud</groupId>
    <artifactId>loadup-components-database</artifactId>
</dependency>

The component uses the shared Spring Clock and supplies a JDBC transaction manager for a single data source, the fixed schema target and sequence/ID configuration. It brings mybatis-flex-spring-boot4-starter, registers framework mappers and configures the shared Flex conventions. Applications configure their own data source and PostgreSQL driver.

MyBatis-Flex is the default ORM for simple aggregate reads. Annotate infrastructure data objects with com.mybatisflex.annotation.Table; the annotation processor generates a Tables class, per-entity TableDef classes and a @Mapper interface under the module’s infrastructure.table and infrastructure.mapper packages. Gateway implementations extend ServiceImpl<Mapper, DO> (and therefore IService<DO>) so standard reads, paging and logical deletion come from the framework; complex conditional writes remain explicit parameterized SQL.

The component applies the logical-delete convention through a MyBatisFlexCustomizer: the deleted column is the logic-delete flag, normal rows use false, deleted rows use true, and mapper reads, updates, counts and deleteById automatically add or apply that condition. Paging defaults are 20 items per page with a maximum of 100.

Explicit SQL must still set IDs, audit timestamps and deleted, and must filter deleted rows itself. Business tables do not carry a tenant_id column: a deployment serves one tenant and every table lives in that deployment’s schema. PostgreSQL schemas use boolean deleted.

Fixed schema per deployment

One deployment serves one tenant. loadup.database.schema is the only application source of the target schema; the connection pool, transaction manager and Flyway all use it. Schema names are validated against [a-z_][a-z0-9_]{0,62}, and the shared public schema plus the reserved pg_ prefix are rejected.

loadup:
  database:
    schema: customer_a_payment
spring:
  datasource:
    url: ${PAYMENT_JDBC_URL}
    username: ${PAYMENT_JDBC_USERNAME}
    password: ${PAYMENT_JDBC_PASSWORD}

Startup behavior:

  • FixedSchemaEnvironmentPostProcessor applies the schema as the pool’s physical-connection setting and fails startup when an explicit Hikari schema, JDBC currentSchema, search_path option or init SQL names a different schema.
  • FixedSchemaVerifier opens one connection and checks current_schema() before workers, schedulers or scanners start. A missing schema does not fall back to public.
  • Flyway uses the same value as defaultSchema/schemas, so the migration target cannot drift from the runtime target.
  • There is no tenant map, no default-tenant-id, no per-request SET search_path and no routing DataSource. Data isolation is enforced by the fixed connection configuration and database role privileges, not by request headers.

Background workers (Task, Scheduler, Transfer) run without any tenant context against the same application data source. Rows do not store a tenant.

Non-relational resources use deployment-fixed namespaces: lock and async-result keys use their configured namespace, DFS object paths use the configured bucket/prefix, and internal service calls use DeploymentIdentity.

Repository query style

Use generated MyBatis-Flex mappers and QueryWrapper for simple aggregates (one or two fixed predicates), paging and logical deletion. Use JdbcClient in the infrastructure gateway when a query has three or more predicates, optional filters, FOR UPDATE, IN lists, or requires explicit column lists and locking semantics. JdbcClient keeps SQL in the infrastructure layer with named parameters and clear locking/pagination semantics. Current examples: LoginLogGatewayImpl and InboxGatewayImpl.

Generated artifacts use the LoadUp database processor. It includes the official MyBatis-Flex processor as a transitive build dependency and owns the single generation configuration. Entity modules do not keep mybatis-flex.config files or run synchronization scripts. The root POMs select LoadUp’s processor entry explicitly, alongside MapStruct and Spring Boot configuration metadata processing, so the upstream processor is not also discovered and executed.

Import the framework BOM for processor versions. BOM import does not inherit compiler-plugin configuration; independent repositories configure the compiler plugin in their own parent POM. Runtime database configuration and annotation processing remain separate dependencies.

Flyway support is optional; consumers using migrations must include Flyway and its PostgreSQL database module. Migration scripts assume a fresh PostgreSQL schema.

classpath:db/migration scans matching locations across the classpath, including dependency JARs; it does not isolate the application. Configure loadup.flyway.locations with the application namespace and only approved framework module namespaces, such as classpath:db/migration/pay/admin and classpath:db/migration/framework/upms. Component templates remain under db/schema; freeze imported initial snapshots and append reviewed migrations for upgrades instead of overwriting existing versions.

Business IDs and database sequences

BusinessIdGenerator is the default IdGenerator when Sequence is available. Persistence code explicitly requests an ID when needed; BaseDO and JDBC inserts do not automatically assign one. Sequence is enabled by default. Apply its migration before using the default generator.

Sequence persistence uses Spring JDBC and requires a suitable DataSource and transaction manager. A consumer-provided IdGenerator takes precedence over the default generator.

Default generator metadata uses data version 0, system version 1, event code 00000000, no extension, site 000 and shard 000. Reserve 00000000 for generic persistence IDs; explicit business generation may use registered event codes. All entities using the same default scope share its eight-digit daily capacity. Configure stable site ownership when independent databases issue IDs.

loadup:
  database:
    sequence:
      enabled: true
      step: 10000
      max-scopes: 1024
      prefetch-threads: 2
      queue-capacity: 256
      wait-timeout: 3s
    business-id:
      data-version: 0
      system-version: 1
      event-code: "00000000"
      extension-code: ""
      site-id: 1
      shard: 0

Apply the db/schema/sequence_allocation.sql template before enabling traffic. Copy it into the integrating application’s own classpath:db/migration history (for example via scripts/gen-migrations.py), retaining all other module locations. The component does not create tables on the request path. Use one authoritative allocation database for each encoded site, preserve committed allocation maxima during failover/restoration, and restrict deletion/reset of allocation rows. The migration and allocation SQL target PostgreSQL.

Inject IdGenerator when a persistence operation needs an application-generated ID. Explicit SQL is responsible for setting its ID and audit columns.

An application may supply one IdGenerator bean (for example UuidV7IdGenerator) to replace the default. loadup.database.sequence.enabled=false requires such an alternative because the default generator depends on Sequence. Existing explicitly supplied IDs, including UUIDs assigned by application services, are preserved.

Inject BusinessIdGenerator and reuse immutable metadata where practical:

BusinessIdCommand command = new BusinessIdCommand(
        0, 1, "11000000", "", 1, 440);
String businessId = generator.generate(command);
String[] businessIds = generator.generateBatch(command, 100);
BusinessIdParts parts = formatter.parse(businessId);

Encoding: date (8) + dataVersion (1) + systemVersion (1) + eventCode (8) + extension (0..32) + site (3) + shard (3) + sequence (8). Length is 32..64; numeric fields preserve leading zeros. Date uses the shared Clock and its local zone, captured once per generation call. Business sequence scope is date + versions + event + site, shared across shards/extensions. It allows at most 99,999,999 allocated numbers per scope per day, including unused reservations.

shard is a logical bucket from 000 to 999. The application registers event codes and chooses a stable routing policy, for example customer affinity via a versioned hash. Supply the resolved bucket in the command. For sequence-derived routing, use the numeric Sequence and formatter together; do not invent a second random bucket or assume 1,000 physical tables. A shard field does not provide authorization or tenant isolation.

For numeric sequences, inject Sequence:

SequenceScope scope = new SequenceScope("payment", LocalDate.now(clock), 99_999_999L);
long number = sequence.next(scope);
long[] batch = sequence.nextBatch(scope, 100);

Reuse SequenceScope for hot numeric loops; its cache key is precomputed. A null date creates a persistent scope. Numbers are unique within a scope; different scopes can repeat numbers. Maximum is persisted on first initialization and cannot subsequently change. nextBatch accepts 1..10,000 and may cross noncontiguous segments. Failure returns no partial batch; consumed values are not reclaimed. Business idempotency must reuse an already-persisted ID.

Each scope has an active range and at most one standby range. Prefetch begins at 20% remaining capacity. Warm issuance uses atomic range claims without database access; concurrent cold callers share one allocation task. Database reservations commit independently of business transactions. Transient initialization/deadlock failures retry in fresh transactions with bounded jitter; failed prefetch preserves the current range. On allocation failure a short cooldown avoids repeated database attempts. Queue rejection, timeout, exhausted capacity, conflicting maxima and scope admission failures use DatabaseResultCodeEnum through CommonException.

Scope admission is bounded. At capacity, past-date entries may be discarded; their unused numbers are lost and already-running reservations may finish without becoming visible in the new cache entry. Persistent/current-date scopes remain protected from automatic eviction; additional scopes fail explicitly. Shutdown stops prefetch workers and fails outstanding waits. Size the pool for independent allocation transactions and distinguish cold/refill latency from warm issuance latency.

Shared Boot MeterRegistry, when present, receives loadup.database.sequence.issued, loadup.database.sequence.refill and loadup.database.sequence.failures (bounded reason enum labels for allocation failures). No raw scope/date/customer IDs are metric tags. Throughput is deployment-dependent; gaps are permitted and global chronological ordering is not guaranteed.

Run focused validation using Maven Daemon:

mvnd clean test -pl loadup-components/data/loadup-components-database -am \
  -Dtest=BusinessIdTest,SegmentSequenceTest,DatabaseSequenceIT \
  -Dsurefire.failIfNoSpecifiedTests=false -Dspotless.skip=true -Dspotbugs.skip=true

The IT requires Docker and uses real PostgreSQL through the framework Testcontainers annotation. Consumer failover/restore rehearsals and P95/P99 capacity measurements are separate production acceptance work.

持久化基类

使用本组件的 io.github.loadup.components.database.dataobject.BaseDO。它保留 id、createdAt、updatedAt、deleted 字段以及 MyBatis-Flex @Id(keyType = KeyType.None) 元数据;标识仍由应用显式提供,迁移不改变 JSON、数据库列或逻辑删除行为。

Deployment identity is supplied by runtime DeploymentDataScope from the existing loadup.database.multi-tenant.default-tenant-id property (default __default__). It does not choose schemas; default-schema, enabled, required and schema validation remain database responsibilities. Non-Web applications need no Servlet entry.


设计与实现

Scope

loadup-components-database is the runtime integration for MyBatis-Flex and Spring JDBC. It owns persistence conventions, generated mapper/table definitions, ID/sequence allocation, Flyway defaults and schema-per-tenant routing seams. Applications own datasource selection, JDBC drivers and deployment migrations.

Spring Data JDBC is not part of the component. Simple aggregate reads use generated MyBatis-Flex mappers and ServiceImpl/IService; complex queries, locks, conditional writes and batches use parameterized Spring JDBC.

Runtime flow

DO extends BaseDO
      │
      ├─ generated Mapper / ServiceImpl(IService) ── simple reads, paging, logical delete
      ├─ QueryWrapper ───────────────────────────── fixed predicates and ordering
      ├─ JdbcClient gateway ─────────────────────── complex/paginated/locking SQL
      ├─ IdGenerator ────────────────────────────── application-assigned business IDs
      └─ explicit audit fields ──────────────────── createdAt / updatedAt / deleted

BaseDO lives in loadup-components-database as io.github.loadup.components.database.dataobject.BaseDO and is shared by persistence adapters. Its id is marked with @Id(keyType = KeyType.None), so MyBatis-Flex never generates or replaces an application-assigned identifier. Business tables do not contain tenant_id. Schema-per-tenant is the target isolation boundary, but runtime datasource routing and per-tenant Flyway orchestration are not implemented; the current application serves the configured default schema.

Common fields

Java field Database column Type Behavior
id id VARCHAR(64) Assigned explicitly or by IdGenerator; Flex key type None
createdAt created_at TIMESTAMP(6) Set on insert
updatedAt updated_at TIMESTAMP(6) Set on insert and every update
deleted deleted BOOLEAN FALSE normal, TRUE logically deleted; configured through MyBatisFlexCustomizer

Timestamps use the framework clock and the JVM system default time zone.

Generated sources

loadup-components-database-processor delegates generation to the official MyBatis-Flex processor in entity modules. For each @Table entity it generates:

  • {entityPackage.parent}.mapper.XxxMapper annotated with @Mapper and extending BaseMapper<XxxDO>;
  • {entityPackage.parent}.table.XxxTableDef and an aggregate Tables class with type-safe QueryColumn constants.

The database auto-configuration scans io.github.loadup for BaseMapper subtypes so framework mappers are visible outside the consuming application’s auto-configuration packages. Mapper beans are lazy; a mapper is only materialized when a gateway injects it and a single DataSource exists.

MyBatisFlexCustomizer installs the shared conventions on the Flex global configuration: logical-delete column/values, banner suppression and pagination defaults. Entity classes without a deleted column are unaffected.

Because gateways mix mapper reads with explicit JDBC writes in the same transaction, the MyBatis local cache scope is STATEMENT. A session-scoped cache would return stale entities after an explicit JDBC update that MyBatis cannot observe.

APT build policy ownership

Runtime customization stays in MyBatisFlexAutoConfiguration. The sibling database-processor module owns the single configuration resource under src/main/resources/META-INF/loadup/database/. Its official Flex processor dependency is transitive on the processor path; business POMs declare only the LoadUp database processor for Flex.

LoadUp writes the fixed policy into the current compilation’s CLASS_OUTPUT before initializing the upstream generator. stopBubbling=true prevents ancestor or module-local policies from changing generation. Configuration load/write failures abort compilation. The same mechanism handles main and test compilation outputs, without source-tree copies, sync scripts or Maven resource download steps.

The parent compiler configuration explicitly selects LoadUp, MapStruct and Spring Boot metadata processors. This is necessary because the upstream JAR also registers its own processor through SPI; automatic discovery would run both generators. The processor module disables annotation processing while compiling itself and never depends on the runtime database component. Framework entity modules declare it provided/optional as a reactor dependency, so -am and parallel builds order the processor before its consumers without exporting it at runtime.

Schema-per-tenant isolation

The target tenant isolation is physical: each tenant owns a PostgreSQL schema. Current deployments use one configured schema. TenantContext/DatabaseTenant compute the trusted tenant/schema, but do not switch JDBC connections. Public business DTOs and repository contracts do not expose tenant routing parameters; internal application transaction helpers may explicitly carry trusted deployment context, which is not a business-table tenant column.

request / worker entry
        │
        ▼
WebMVC DeploymentScopeFilter / worker entry ── binds ExecutionContext
        │
        ▼
AbstractRoutingDataSource (planned) ── determineCurrentLookupKey() = TenantContext.tenantId()
        │
        ├─ schema mode: per-tenant DataSource (same DB, different currentSchema)
        └─ database mode: independent DataSource
        │
        ▼
single-tenant repository / explicit SQL (no tenant predicate)

Design rules:

  • loadup.database.default-schema defaults to public; DatabaseTenant.currentSchema() validates [a-z_][a-z0-9_]{0,62} before any connection is selected.
  • enabled=false always resolves the configured default tenant/schema. enabled=true, required=true fails closed when the execution context has no tenant.
  • When enabled and a context tenant is present, currentSchema() validates that tenant directly as the schema string; it does not map the default tenant ID back to default-schema. This computation is not active datasource routing.
  • The current phase serves only the default schema; routing, tenant registry lookup, per-tenant pools and per-tenant Flyway are deliberately deferred.
  • Schema mode uses one pool per tenant so pooled search_path state cannot leak between tenants. Database mode is an alternative target for the same routing key.
  • The tenant must be bound before the transaction starts. WebMVC DeploymentScopeFilter binds the default tenant in the current phase; TaskWorker and SchedulerTemplate bind their trusted tenant at the worker entry point. Schema resolution remains in DatabaseTenant.
  • Per-tenant Flyway: run the same schema-agnostic migrations with defaultSchema/schemas set to the tenant schema. A control-plane tenant registry maps tenant codes to schema names; business tables never carry a tenant column.

Non-relational resources are outside schema isolation: Redis lock/async-result keys, DFS object paths and RPC identity continue to use TenantUtil/MDCUtils.

Stage 1B removed MultiTenantAutoConfiguration and the optional Web starter. Runtime owns the existing deployment tenant property and supplies DeploymentDataScope; DatabaseTenant consumes it without Servlet types. WebMVC owns DeploymentScopeFilter at HIGHEST_PRECEDENCE +10, after ExecutionContextFilter, with REQUEST/ASYNC/ERROR dispatch. Source changes and regression sources are complete; builds and tests have not run.

Repository query style

Generated MyBatis-Flex mappers and QueryWrapper are used for simple aggregates (one or two fixed predicates), paging and logical deletion. JdbcClient is used in infrastructure gateways for complex queries: three or more predicates, optional filters, FOR UPDATE, IN lists, or explicit column lists. JdbcClient keeps SQL in infrastructure with named parameters and clear locking/pagination semantics.

Mappers expose tenant-free, single-tenant signatures. Gateways and facades do not accept tenant identifiers. Logical deletion is applied by the Flex global configuration for mapper operations and must be written explicitly in raw SQL.

Flyway

Flyway support is optional; consumers using migrations include Flyway and its PostgreSQL module. Migration scripts are schema-agnostic (no schema-qualified names) and assume a fresh schema. LoadUpFlywayAutoConfiguration applies the configured default schema from loadup.database.default-schema; per-tenant execution is a deployment/routing responsibility. Applications must select explicit migration namespaces: their own directory and approved db/migration/framework/<module> locations. The generic classpath migration root also discovers dependency JAR resources and is not an isolation mechanism. One application retains one Flyway history across its selected locations; published versioned migrations are immutable.

ID generation and sequences

IdGenerator supplies application-assigned IDs; BusinessIdGenerator is the default when Sequence is available. Sequence persistence uses Spring JDBC and an independent REQUIRES_NEW transaction. Allocation rows are per schema and use a unique scope_key; business rollback does not reclaim issued numbers. See README for encoding, capacity and operational rules.

Background workers

Task and Transfer workers scan only the schema they run in. A worker row has no tenant column; tenant/schema binding happens at the worker entry point. Deploy one worker per tenant schema (or a dispatcher that iterates schemas and switches context) when multiple tenants are active.

Deployment identity is supplied by runtime DeploymentDataScope from the existing loadup.database.multi-tenant.default-tenant-id property (default __default__). It does not choose schemas; default-schema, enabled, required and schema validation remain database responsibilities. Non-Web applications need no Servlet entry.