Advanced Usage
This chapter covers the runtime capabilities that go beyond "switching databases with @DS". Ordered by how often they are used:
| Topic | Description |
|---|---|
| Adding/removing datasources at runtime | add and remove datasources while running — the foundation of multi-tenant systems |
| Operations endpoint and metrics | an Actuator endpoint listing datasources and groups, plus connection pool Micrometer metrics |
| Graceful shutdown | wait for active connections before closing, avoiding request failures during a release |
| Dynamic datasource resolution | decide which database to use from header / session / SpEL |
| Database password encryption | ENC(...) for url / username / password |
| Startup init scripts | run schema and data scripts per database |
| Lazy datasource startup | create the connection pool only on first use |
| Startup without datasources | start with no datasource configured at all, adding them later |
| Manual datasource switching | switch by hand with the toolkit when annotations do not cover the case |
| Manual multi-datasource injection | what to do when you need to own the DataSource bean yourself |
| Multi-tenancy in practice | four progressively stricter isolation models |
Want to drop annotations entirely? See the no-annotation approach.