title: Spring boot jpa multitenancy with dynamic datasources
body: <p>I'm trying to create a multi tenant web application, and found a good tutorial  <a href="http://anakiou.blogspot.it/2015/08/multi-tenant-application-with-spring.html" rel="nofollow">here</a>. This explains me how to configure the MVC to find the new tenant (with <code>CurrentTenantIdentifierResolver</code> and a MultiTenancyInterceptor that extends <code>HandlerInterceptorAdapter</code>), how to configure three different datasources for three different tenants, and how to give the correct datasource to the server at runtime by extending <code>AbstractDataSourceBasedMultiTenantConnectionProviderImpl</code></p>
  
  <p>Now, this has been a great starting point to make me understand how multi tenancy in spring and hibernate work, but I want to push this further and I'd like to make it so that the tenants are completely dynamic, i.e. I don't make assumptions on how many tenants there can be for an application.</p>
  
  <p>This is what I thought:</p>
  
  <ul>
  <li>The application is configured to scan a path (not in the classpath, e.g. /usr/data/config) at boot, and finds various application.properties files under various directories (one directory for each tenant) e.g. tenantA, tenantB, tenantC...</li>
  <li>For each application.properties Spring boot will create a datasource based on that file (that file will only have the boot property spring.datasource.url). Note that it would be great to use spring boot's property since it gives me all the information I need from the single URL, like JDBC class and such.</li>
  <li>I will register each of those datasources in a HashMap (as shown in the previous link)</li>
  </ul>
  
  <p>After that, the basic multitenancy structure is already described in the link aforementioned: everytime an end user makes a request to the browser, the server will elaborate the tenant and give the correct datasource back to find the database that is to be used.</p>
  
  <p>Anyone can point me to some resources, if this has been previously made (I googled a lot but nothing gets me started), or give some advice on which spring classes/configurations to use to achieve this?</p>
  
  <p>Thanks in advance</p>