Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-user Datasources - Spring + Hibernate

I'm writing a web app that supports multiple users. Each user has their own database - using H2. all database schemas are the same.

I wish to use Spring + Hibernate for this application.

So I'm stuck at how to associate a user's database with that user - maybe associated it in the HTTPSession, and extend spring's AbstractRoutingDataSource? but wouldn't this effect Hibernate's cache? Another way is to have a SessionFactory with each datasource, even though every datasource's schema is the same... so I see that as a waste.

Anyways selecting the datasource needs to be dynamic - they can't be pre-configured in context files, as each new user will have its own database created. Is there any existing frameworks/solutions?

I don't know too much about Hibernate Shards, maybe that works?

like image 733
Dzhu Avatar asked Oct 04 '10 07:10

Dzhu


People also ask

Can you use spring and Hibernate together?

We can simply integrate hibernate application with spring application. In hibernate framework, we provide all the database information hibernate. cfg.

Can we use multiple datasource in spring boot?

Spring Boot provides first-class support to the Spring JPA that makes it easy to access the database with little boilerplate code by using Spring Repositories feature. Spring Boot does not provide an out of the box solution in case our application needs multiple DataSources (e.g. multi-tenant system).


2 Answers

I might be wrong about the (strict) need to have one SessionFactory per database, as suggested by some resources:

Dynamic DataSource Routing

I'll take some time to re-read everything tomorrow (I didn't get all the details to be honest) and to fully understand the implications of such a setup (although it seems clear that it will break the second-level cache). I'll come back on this later.


I'm writing a web app that supports multiple users. Each user has their own database - using H2. all database schemas are the same.

I wonder how this will scale... How many users do you have? How do you run H2, what mode?

So I'm stuck at how to associate a user's database with that user - maybe associated it in the HTTPSession, and extend spring's AbstractRoutingDataSource?

You'll have to build a SessionFactory per user and associate it to the logged user (in a Map, using the login as key) and then obtain a Session from a given SessionFactory. Binding the lifecycle of the SessionFactory to the HTTP session seems to be a good idea (to save some memory) but I am not sure Spring will be very helpful here. I might be wrong but a variation of the HibernateUtil class and a fully programmatic approach looks easier. I'm not sure you'll need multiple connections per user by the way.

but wouldn't this effect Hibernate's cache?

What cache?

Another way is to have a SessionFactory with each datasource, even though every datasource's schema is the same... so I see that as a waste.

Oh, it's a waste, but that's what you want to do (one database per user). And you don't have the choice (you need one SessionFactory per datadabase). Why do you need one database per user actually? Are you sure this is a wise decision? As already hinted, this means much troubles, won't scale well, adds complexity, etc. Why not using a single database and associating data to the user?

Anyways selecting the datasource needs to be dynamic - they can't be pre-configured in context files, as each new user will have its own database created. Is there any existing frameworks/solutions?

Not to my knowledge. Which is also why I think you'll have to do everything programatically.

I don't know too much about Hibernate Shards, maybe that works?

Given the dynamic needs of your application, I don't see how it could help.

like image 74
Pascal Thivent Avatar answered Oct 07 '22 04:10

Pascal Thivent


This may help you:

  • Dynamic Datasource via Spring using HotSwappableTargetSource
  • Hibernate + Spring using multiple datasources?
like image 31
jmj Avatar answered Oct 07 '22 05:10

jmj