Skip to content

rrezartprebreza/spring-boot-skills / skills/spring-boot-3/multi-tenancy

multi-tenancy

Verified

Use when implementing tenant resolution, database or schema isolation, tenant-aware JPA, reactive tenant context, migrations, caching, jobs, or authorization in Spring Boot 3.

templates/TenantContext.java

View on GitHub
package com.example.tenancy;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Objects;
import java.util.UUID;

/** Servlet/blocking example only. Reactor applications must use Reactor Context instead. */
public final class TenantContext {
    private static final ThreadLocal<TenantId> CURRENT = new ThreadLocal<>();
    private TenantContext() {}

    public static TenantId required() {
        TenantId tenant = CURRENT.get();
        if (tenant == null) throw new IllegalStateException("Tenant context required");
        return tenant;
    }

    public static Scope open(TenantId tenant) {
        TenantId previous = CURRENT.get();
        CURRENT.set(Objects.requireNonNull(tenant));
        return () -> {
            if (previous == null) CURRENT.remove();
            else CURRENT.set(previous);
        };
    }

    public static Runnable capture(Runnable task) {
        TenantId tenant = required(); // Capture at submission, never resolve on a pooled worker.
        return () -> {
            try (var ignored = open(tenant)) {
                task.run();
            }
        };
    }

    public static String orderCacheKey(UUID id) {
        String tenant = Base64.getUrlEncoder().withoutPadding()
            .encodeToString(required().value().getBytes(StandardCharsets.UTF_8));
        return "orders:v1:tenant:" + tenant + ":order:" + id;
    }

    @FunctionalInterface
    public interface Scope extends AutoCloseable {
        @Override void close();
    }
}
deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall