2

The application has 2 separate databases, say:

  • DB1
  • DB2

and database of different companies are split across these 2 databases, like:

  • DB1
    • Company 1
    • Company 2
    • Company 3
    • Company 4
  • DB2
    • Company 5
    • Company 6
    • Company 7
    • Company 8

How can such a scenario be configured in hibernate? All the examples refer either to Schema based multitenancy or Database multitenancy.

Is there any way to configure such a scenario?

Manish Kothari
  • 1,702
  • 1
  • 24
  • 34
  • Wouldn't it be easier to just go for database multitenancy? The schema based multitenancy works well for smaller datasets, but combining the two isn't really worth doing. Instead you would have `Server1` and `Server2` (provided you have multiple database servers) with each having 4 databases for companies. – Kayaman Aug 02 '17 at 11:30
  • @Kayaman Database structure is already defined, this is an existing application. I cannot change that. – Manish Kothari Aug 02 '17 at 11:46
  • Configure two `DataSource`s, one for DB1 and another for DB2. Create two implementations of [`AbstractRoutingDataSource`](https://spring.io/blog/2007/01/23/dynamic-datasource-routing/) - one that allows selecting the data source for DB1 for the current client; and another that allows selecting the data source for DB2. Lastly, create a third implementation of `AbstractRoutingDataSource` that allows selecting one of the other two `AbstractRoutingDataSource` implementations for the current client. The linked blog has all the information required. – manish Aug 02 '17 at 12:12

3 Answers3

0

To do that you should create two sessionFactory Bean in your config file. and in your DAO layer you call it using qualifier annotation.

0

You're going to have to create your own implementation of MultiTenantConnectionProvider that handles both database and schema level multitenancy. That should be the path of least resistance.

Here's some slightly outdated info about fighting with spring, hibernate and custom multitenancy: Setting up a MultiTenantConnectionProvider using Hibernate 4.2 and Spring 3.1.1

Kayaman
  • 72,141
  • 5
  • 83
  • 121
0

I will get strategy with separate database and in ConnectionProvider override method getConnection() to set schema. I this case I can change databases and select specyfic schema for tenant.

Victor1125
  • 642
  • 5
  • 16