vtajzich / spring-hibernate4-multitenant

Example of multitenant usage - hibernate 4 & spring 3

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

spring-hibernate4-multitenant

Example of multitenant usage - hibernate 4 & spring 3

What it can do for you

Multitenant support offer to have multiple DBs or Schemas for "tenants". It is transparent from application point of view.

Options

  • hibernate.multiTenancy
  • DATABASE
  • SCHEMA
  • DISCRIMINATOR - it is planned for Hibernate 5

These two classes must be implemented by you

  • hibernate.tenant_identifier_resolver - it must return tenant id for current thread. Typically from request header?

  • hibernate.multi_tenant_connection_provider - it must return connection for given tenant indentifier (result of hibernate.tenant_identifier_resolver)

Quite interesting parts in code

  • sessionFactory

  • hibernateProperties

    • it can be set using map, so you can set bean for some hibernate properties (e.g. hibernate.tenant_identifier_resolver)
    • hibernate.hbm2ddl.auto cannot be used or it will fail
  • webSessionTenantIdentifierResolver

  • there is scope set to request so every http request to server will create new instance of this class but

  • you have to use

<aop:scoped-proxy/>

to let spring create a proxy around this bean in order to provide new instance every time

  • for request scoped bean you can use
@Autowired
private HttpServletRequest request;

About

Example of multitenant usage - hibernate 4 & spring 3


Languages

Language:Java 100.0%