Changing a secondary datastore connection at runtime/programmatically

How do I change a secondary datastore at runtime/programmatically? My users need to choose from which datastore they want to work with. The database schemas are identical.

There is no built-in easy way for this.
However, I think it still can be done:

  • CUBA gets the DataSource objects from JNDI, where they normally created by the application server. For Tomcat, data sources are defined in context.xml file.
  • You could create your own connection pools and register in JNDI with the required names, see cuba.dataSourceJndiName_<data-store> property in app.properties file.
  • You could replace your connection pools in JNDI or reconfigure/restart them for different databases when needed.

I didn’t test this approach, but if I had such requirements I would try to go this way.