0

I have got a question about multiple db support of Hibernate.

I have 1000s of customers distributed on multiple db servers. Each server has got around 500 Dbs. So every customer has its own database (the schema of all are exactly the same, they just differ in content of course).

Is there a way to make Hibernate switch databases on the fly? All research resulted in solutions where you already know all the databases which are used (like 2 or 3 different ones), but none would help us with our dynamic problem.

Bacteria
  • 8,406
  • 10
  • 50
  • 67
  • So you have multiple, independent databases, with different contents. Why do you want to switch? Is this some kind of failover mechanism? Or you want to search on multiple databases? – Koshinae Nov 12 '15 at 09:47
  • No, its like all customers are working parallel on the same server-project. And every customer should of course only get results out of his own database and not from the database of another customer. – Fighter_MV Nov 12 '15 at 09:49
  • So the session decides which db they are connecting to? – Koshinae Nov 12 '15 at 09:51
  • Yes, exactly. Thats how it should work. – Fighter_MV Nov 12 '15 at 10:11
  • Well, you could build a sessionfactory map for all db-s, and look it up for the users, and get a connection from it. On a sidenote: if you have the very same schema for all, why don't store the data in a way they can only fetch their own? (eg. abstraction through views or procedures) then you could even cluster the database nodes. – Koshinae Nov 12 '15 at 10:24
  • Okay, thank you - we will try it with the map. :) – Fighter_MV Nov 12 '15 at 10:28
  • https://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch16.html – Alan Hay Nov 12 '15 at 10:35
  • @AlanHay Thanks, I was unsuccessfully looking for that in the dark side of my brain... :) – Koshinae Nov 12 '15 at 11:35
  • Alan thanks a lot!!! Thats exactly what we need – Fighter_MV Nov 12 '15 at 12:49
  • I strongly second @Koshinae : Your problem isn't the database connection. You have a severe problem with your data models. It is rather easy to make sure that each customer can only see "their" data, but giving each customer their own database will cause problems on multiple layers, such as connection, scalability, ease of maintenance and whatnot. I'd strongly recommend to get back to the drawing board before proceeding. – Markus W Mahlberg Nov 14 '15 at 10:35
  • @MarkusWMahlberg you realise I'm not the OP? – Koshinae Nov 14 '15 at 14:27
  • @Koshinae Absolutely, that's why I second you ;) – Markus W Mahlberg Nov 14 '15 at 15:12
  • @MarkusWMahlberg Ah, sorry, then I just forgot to read carefully. – Koshinae Nov 14 '15 at 15:21

0 Answers0