This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

AnnouncementAnnouncement Module

Collapse

No announcement yet.

Different Databases for different users to separate data?Page Title Module

Different Databases for different users to separate data?

I am no total newby to Spring, but I am far from being an expert. I hope you can help me.

I am planning a web application that will have a start-page with news, infos and the like.
There will be a login for registered users to access their private data.

How would you suggest I separate the data of the different users?
I thought about including in every object the corresponding user. And then manage the access rights in the spring application.
Then I thought maybe I could use a single database for every user. Then I would not need to manage the access rights. How would I do this in Spring with Hibernate? I used Spring and Hibernate but all my objects were loaded at startup via Hibernate and not when a user logged in...

Single database for every user? What if you have 1000 users? That means 1000 databases? 1 million users = 1 million databases?

Isn't that too much waste of resources? And what database are you going to use? MySQL? You'll probably go for NoSQL type databases then.

I suggest implementing ACL with Spring Security instead of 1 user per database. You can load specific data depending on the authorized user and the access rights given to the object. Maybe you can start by checking the following tutorial:

Comment

Then I thought maybe I could use a single database for every user. Then I would not need to manage the access rights. How would I do this in Spring with Hibernate? I used Spring and Hibernate but all my objects were loaded at startup via Hibernate and not when a user logged in...

Nobody does that. That's a nightmare solution.

You need to implement a session that identifies the user, and an ACL which holds the objects a user is entitled to access. There is no reason why hibernate should fetch all your objects on startup, check your config there is likely an error somewhere, perhaps hidden in your application logic.

Comment

I don't know if this would be the best way to do it, but I would look into developing two separate classes, one to work with each database. That way, anytime you would need to update the data, you could just send the data into each of the classes and each class would update the corresponding database. I know that you would be duplicating some code, and that kind of defeats the purpose of the class, but then again, the customer already is duplicating data in the databases and doesn't want to move to a single database.