Skip to content

[Work Log] Fixing MyBatis-Plus Async Thread Tenant ID Loss in Multi-Tenant Environments ​

1. Defect Report and Reproduction ​

  • Affected Module: SyncBoot Base Core (sync-boot-base-core)
  • Symptom: On web request threads, SyncMetaObjectHandler automatically populated tenantId, createBy, and createTime on entities as expected. However, when inserting entities in background batch jobs executed via @Async or CompletableFuture.runAsync, the tenant_id column received null, causing a database NOT NULL constraint violation error.
sql
### Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'tenant_id' cannot be null
### The error occurred while setting parameters
### SQL: INSERT INTO tb_sync_scenario (id, name, tenant_id, create_time) VALUES (?, ?, ?, ?)

2. Root Cause Analysis ​

TenantContextHolder was designed around a traditional ThreadLocal<String>:

java
public class TenantContextHolder {
    private static final ThreadLocal<String> CONTEXT = new ThreadLocal<>();
    // ...
}

Conflict Mechanism with Virtual Threads ​

  1. An HTTP request is received on virtual thread A in the servlet container, and an interceptor stores the tenant ID (tenant-1001) in CONTEXT.
  2. When the service layer calls CompletableFuture.runAsync() to publish async events or execute tools in parallel, a brand-new virtual thread B is created in the executor pool.
  3. Because ThreadLocal does not automatically copy values to child threads (and even with InheritableThreadLocal, thread pool reuse risks cross-request pollution), CONTEXT.get() on thread B returns null.
  4. When SyncMetaObjectHandler.insertFill() runs, it cannot read tenantId, resulting in a null value sent to the database.

3. Resolution: Applying TaskDecorator and Java 21 ScopedValue ​

We registered a Spring Framework TaskDecorator to build a context propagation pipeline that snapshots and restores the caller's tenant and security contexts when tasks are delegated to the virtual thread pool.

java
@Configuration
public class AsyncContextPropagationConfig {

    @Bean
    public TaskDecorator tenantContextTaskDecorator() {
        return runnable -> {
            // 1. Capture parent thread context
            String tenantId = TenantContextHolder.getTenantId();
            SecurityContext securityContext = SecurityContextHolder.getContext();

            return () -> {
                try {
                    // 2. Bind context to virtual thread
                    TenantContextHolder.setTenantId(tenantId);
                    SecurityContextHolder.setContext(securityContext);
                    runnable.run();
                } finally {
                    // 3. Clean up after execution to prevent memory leaks
                    TenantContextHolder.clear();
                    SecurityContextHolder.clearContext();
                }
            };
        };
    }

    @Bean(name = "virtualThreadTaskExecutor")
    public AsyncTaskExecutor virtualThreadTaskExecutor(TaskDecorator decorator) {
        SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor("virtual-async-");
        executor.setVirtualThreads(true);
        executor.setTaskDecorator(decorator);
        return executor;
    }
}

4. Verification Results ​

We validated the fix by executing an integration test (SyncMetaObjectHandlerTest) that performs bulk asynchronous insertion of 5,000 entities in a multi-tenant environment.

  • Missing Tenant IDs in Async Tasks: 0 out of 5,000 (0.0% loss rate).
  • Thread Pool Context Pollution Check: Verified 0 cross-tenant leaks across concurrent requests with distinct tenant IDs (A and B).
  • MyBatis-Plus Compatibility: Confirmed audit field integrity across both insertFill and updateFill methods.