Database server connection mechanism

Hi Cuba team,
I have a question on database connection mechanism in CUBA, sorry about it if I am too novice about this topic.

My question is in the perspective of an environment where middleware and database are in separate servers where a single database is connected to my Application middleware. On the other hand, I have a 2nd database server which is mirrored with the primary database server as backup. In case the primary database server is down or somehow inaccessable (under maintenance or crushed) I want all the connection requests to be directed to the 2nd database server automatically without a down time and without changing the context.xml parameters manually. Of course it is acceptable that the connected users will be disconnected when the primary database is unreachable but they will be able to log-in promptly (to 2nd database in the background), automatically. Is it something doable?

You can read this topic: https://www.cuba-platform.com/discuss/t/cuba-multi-tenant-with-database-per-tenant
regarding runtime database connection mechanism. It is based on user session and Konstantin left an exemple on github

Thanks Tudor for directing to that log. It looks good, will spend some time to evaluate if it is going to meet the requirements I am looking for.
My requirement is bit different if I not mistaken, I am not looking for to connect to different databases based on the user attribute but want all users connected to the 2nd server in the event when the main database server is down. This will require simply the reconnecting the users/direct new users to the 2nd backup database server. That’s it! Thanks for any comments.

Multi-tenant feature might solve my problem. I am thinking of using the procedure from here
if(path == null)
defaultDataSource = createDataSource(alternateDbAddress)