Jun 12th, 2009, 05:39 PM
Spring and Multi-tenant approaches
I'm wondering if someone could point me to some resources regarding the development of multi-tenant applications with Spring and Hibernate. I've searched around in a few places, and I'm not coming up with much at all. To be clear, there are plenty of articles about multi-tenant architectures, and plenty of articles about Spring/Hibernate, but I've not found anything that ties the two together. Any suggestions?
Nov 26th, 2009, 07:52 PM
Multi-Tenant Websites made my work easyer, unfrotunaly never found a corelation Spring/Hibernate.
Dec 31st, 2009, 12:22 PM
I did a prototype using spring and Hibernate.
Using spring you can set up multiple data sources, you can inject data source based on user logged in and based on user belongs to which customer.
Jul 29th, 2010, 12:03 AM
Do you have any code examples of this?
Apr 1st, 2011, 05:44 AM
You don't need multiple datasources to do that. In fact, it is possibly to do query rewriting right before Hibernate sends a generated SQL statement off to your database.
Implementing that query post-processor, you have to have some sort of context from which you can get a specific schema name. In PostgreSQL that will be the "public" schema by default but using multiple schemas together with query rewriting, you can support multi-tenancy by rewriting the schema name.
Let C12345 be the tenant id, all occurrences of "public" get rewritten in your queries.
- public.customers ---> C12345.customers
Maybe you have to tell Hibernate to incorporate schema names in generated queries.
Apr 8th, 2011, 11:43 AM
How to tell Hibernate to incorporate schema names in generated queries ?
We also use native queries as well. I have searched through forums and couldn't find any where on how incorporate schema names ?
Jun 15th, 2011, 04:35 PM
Hibernate 4.0 just went beta with multi-tenancy support for schema's. You may want to look into that.
Aug 2nd, 2011, 01:19 PM
one way I've leveraged Spring in a multi-tenant environment is with Spring Security. The app exposed web services only; each request was a new thread. When the caller authenticated (in our case just HTTPS & basic auth), the current user information is associated with the thread as a thread local. So now anywhere in the code you know who the authenticated user is. Each user is associated with a tenant. There are plenty of ways to skin the cat at this point, given that each thread is associated with a particular tenant. You can do things like query rewriting as someone else mentioned ... or not exactly rewriting in that your queries can all be parameterized to take a tenant code.
Aug 15th, 2011, 06:31 PM
You could take a look at the Grails plugins for multi tenancy, grails is a rapid development framework with Groovy on top of Spring/Hibernate/etc. One general comment on developing a multi-tenant application would be to look at ways of running multiple instances of the app on a cloud infrastructure, such as Cloud Foundry, Google App Engine, or Amazon Beanstalk. In that case, you might have some front controller that provides a unified front-door to tenants logging in for the first time, but then serves everything for an individual tenant to their individual app instance in the cloud.
The reason for doing something like this would be to move the compartmentalization of tenants out to the extreme boundries, so you could reliably develop your app without having to worry about gotchas where one tenant accidently gets access to another tenants data via a software defect, or one tenant can't use all the system resources for another tenant. From a systems engineering perspective, there are a lot of things to consider for a multi-tenant system.
Tags for this Thread