Change db at runtime

Hi everyone, is there a way to change the current database at runtime? thanks

That sounds like a strange thing to want to do. Can you describe what you’re actually trying to achieve in a bit more detail?

i am trying to build a multi tenancy application so the user needs to change various dbs when using the application.

I don’t know your requirements, but typically in multi-tenant applications a single user should absolutely not be able to access other tenants’ information; it could even be seen as a critical flaw. But you can do that, it’s just a matter of how you build your database access code.

There’s nothing special for Vaadin with regards to multi-tenancy so any generic examples, e.g. https://www.baeldung.com/multitenancy-with-spring-data-jpa, apply just as well.

I may have expressed myself badly, in this application there are multiple databases (same schema, different data) and each user can freely connect to each of them. Users can have different roles and permissions on each of these databases.

That doesn’t sound like a problem. The shared schema doesn’t matter; just think of it like connecting to multiple different databases.

Sounds like a small varaition to the middle option in the article that I shared. Just store TenantContext in the session based on which database the user selects.

It’s probably clearer for the user if they select which databse to use when they log in but you can also let them change on-the-fly as long as you make sure all parts of the UI react to the change so that users won’t accidentally see some data from their previous selection.