diff --git a/README.md b/README.md
index fd9b12028..55846ce82 100644
--- a/README.md
+++ b/README.md
@@ -164,6 +164,10 @@ dialect for the target database.
| `teaql-utils`, `teaql-utils-json` | Framework-neutral utility abstractions |
| `teaql-utils-reflection`, `teaql-utils-spring` | Optional reflection- and Spring-backed utility implementations |
+For business IDs, use the context-owned `BusinessIdService` and explicit
+`context.ensureSchema()` lifecycle. The [focused runtime example](examples/business-id-runtime/README.md)
+documents the preferred contract and the deprecated legacy boundary.
+
## Framework Notes
### Spring Boot and SQLite
diff --git a/examples/business-id-runtime/README.md b/examples/business-id-runtime/README.md
index 0a95d559f..78eb2d4c8 100644
--- a/examples/business-id-runtime/README.md
+++ b/examples/business-id-runtime/README.md
@@ -6,6 +6,26 @@ daily sequence, controlled Context date, root scoping, Mutation Ledger retry
stability, explicit SQLite schema setup, cross-instance allocation, restart,
strong `OrderNumber` parsing, and immutable established aggregates.
+The preferred contract is `context.businessIds().ensure(context, definition,
+domainRootKey, aggregateType, slot)`. Install the allocator with
+`TeaQLRuntime.builder().businessIdInfrastructure(allocator)`, then explicitly
+call `context.ensureSchema()` before allocation. The business date comes from
+the context's `BusinessClock`; tests can replace that clock. The JDBC allocator
+uses root/aggregate/namespace/date scoping, optimistic allocation, bounded
+retry, and a range error when the configured digits are exhausted.
+
+`BusinessIdGenerator`, `UserContext.generateBusinessId(...)`,
+`InMemoryBusinessIdGenerator`, and `JdbcBusinessIdGenerator` are deprecated
+compatibility APIs. They retain the old `PREFIXyyyyMMddNNNN` format and separate
+sequence namespace; do not mix their sequence state with `BusinessIdService`.
+The legacy JDBC implementation no longer creates a table in its constructor or
+during allocation. If an existing integration must keep it, register it as a
+`BusinessIdSchemaContributor` on the context and call `context.ensureSchema()`
+explicitly. Both legacy implementations now use `context.businessDate()` and
+reject an exhausted digit range. Repository source and generated-template
+search found only the public API and tests using the legacy path; external
+consumers have not been inventoried and must be treated as unknown.
+
```bash
mvn -pl examples/business-id-runtime -am \
-Dtest=BusinessIdRuntimeExampleTest \
diff --git a/teaql-business-id-jdbc/pom.xml b/teaql-business-id-jdbc/pom.xml
index ab12a1352..211605ce1 100644
--- a/teaql-business-id-jdbc/pom.xml
+++ b/teaql-business-id-jdbc/pom.xml
@@ -31,6 +31,11 @@
teaql-sql-portable
+
+ io.teaql
+ teaql-runtime
+ test
+
io.teaql
teaql-sqlite
@@ -50,4 +55,16 @@
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+
+ --add-reads io.teaql.businessid.jdbc=io.teaql.runtime
+
+
+
+
+
diff --git a/teaql-business-id-jdbc/src/main/java/io/teaql/businessid/jdbc/JdbcBusinessIdGenerator.java b/teaql-business-id-jdbc/src/main/java/io/teaql/businessid/jdbc/JdbcBusinessIdGenerator.java
index 843f321cf..ccf37d6ae 100644
--- a/teaql-business-id-jdbc/src/main/java/io/teaql/businessid/jdbc/JdbcBusinessIdGenerator.java
+++ b/teaql-business-id-jdbc/src/main/java/io/teaql/businessid/jdbc/JdbcBusinessIdGenerator.java
@@ -4,26 +4,27 @@
import io.teaql.core.Entity;
import io.teaql.core.TeaQLRuntimeException;
import io.teaql.core.UserContext;
+import io.teaql.core.businessid.BusinessIdErrorCode;
+import io.teaql.core.businessid.BusinessIdException;
+import io.teaql.core.businessid.BusinessIdSchemaContributor;
import io.teaql.core.meta.EntityDescriptor;
import io.teaql.core.meta.PropertyDescriptor;
import io.teaql.core.sql.portable.TeaQLDatabase;
import io.teaql.core.utils.StrUtil;
-import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.logging.Level;
-import java.util.logging.Logger;
/**
- * JDBC implementation of BusinessIdGenerator.
- * Uses a dedicated sequence table {@code teaql_biz_sequence} to generate unique IDs.
+ * Legacy JDBC business ID format, retained for existing callers. Prefer the
+ * context-owned BusinessIdService and JdbcBusinessIdAllocator for new models.
+ * Schema creation is explicit through context.ensureSchema().
*/
-public class JdbcBusinessIdGenerator implements BusinessIdGenerator {
+@Deprecated
+public class JdbcBusinessIdGenerator implements BusinessIdGenerator, BusinessIdSchemaContributor {
- private static final Logger LOG = Logger.getLogger(JdbcBusinessIdGenerator.class.getName());
+ private static final int MAX_ATTEMPTS = 100;
private final TeaQLDatabase database;
private final String sequenceTable;
@@ -33,20 +34,19 @@ public JdbcBusinessIdGenerator(TeaQLDatabase database) {
}
public JdbcBusinessIdGenerator(TeaQLDatabase database, String sequenceTable) {
- this.database = database;
+ this.database = java.util.Objects.requireNonNull(database, "database");
+ if (sequenceTable == null || !sequenceTable.matches("[A-Za-z][A-Za-z0-9_]*")) {
+ throw new IllegalArgumentException("Invalid Business ID table name: " + sequenceTable);
+ }
this.sequenceTable = sequenceTable;
- ensureSequenceTable();
}
- private void ensureSequenceTable() {
+ @Override
+ public void ensureSchema(UserContext context) {
String ddl = "CREATE TABLE IF NOT EXISTS " + sequenceTable + " ("
+ "sequence_key VARCHAR(100) PRIMARY KEY, "
+ "current_value BIGINT NOT NULL)";
- try {
- database.execute(ddl);
- } catch (Exception e) {
- LOG.log(Level.FINE, "teaql_biz_sequence table may already exist: " + e.getMessage());
- }
+ database.execute(java.util.Objects.requireNonNull(context, "context"), ddl);
}
@Override
@@ -60,67 +60,101 @@ public String generateBusinessId(UserContext context, Entity entity, EntityDescr
String prefix = parts[0].trim();
int length = parts.length > 1 ? Integer.parseInt(parts[1].trim()) : 6;
- String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
+ String dateStr = java.util.Objects.requireNonNull(context, "context")
+ .businessDate().format(DateTimeFormatter.BASIC_ISO_DATE);
String sequenceKey = prefix + ":" + dateStr;
- long seq = nextSequence(sequenceKey);
+ if (length < 1 || length > 18) {
+ throw new IllegalArgumentException("Legacy Business ID digits must be between 1 and 18");
+ }
+ long maximum = 1;
+ for (int i = 0; i < length; i++) maximum = Math.multiplyExact(maximum, 10);
+ long seq = nextSequence(context, sequenceKey, maximum - 1);
- return String.format("%s%s%0" + length + "d", prefix, dateStr, seq);
+ return String.format(java.util.Locale.ROOT,
+ "%s%s%0" + length + "d", prefix, dateStr, seq);
}
- private long nextSequence(String sequenceKey) {
- AtomicLong result = new AtomicLong(-1);
-
- database.executeInTransaction(() -> {
- Number dbCurrent = null;
+ private long nextSequence(UserContext context, String sequenceKey, long maximum) {
+ RuntimeException lastConflict = null;
+ for (int attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
+ List