Specification
Each tenant has their own database which handles users in greater detail, and there needs to exist a central database which handles:
Tokens (OAuth2)
Users (limited level of detail)
Mapping users to their database
Problem
I've found solutions for multi-tenancy which allows me to determine the datasource depending on the user. However, I'm not sure how I can also link certain crud repositories to this central datasource, and others to variable datasources.
Another solution involved updating the properties file, and using a configuration server (i.e. via git) to trigger #RefreshScope annotated configs. Though I'm not sure if this can work for Datasources, or if this could cause problems later on.
Extra Context
I'm using Spring Boot and Hibernate heavily in this project.
This blog gives a very good tutorial on how to do it.
After a lot of research it looks like hibernate just isn't built for doing that, but by manually writing the schema myself I can inject that into new tenant databases using native queries.
I also had a problem with MS Server DBs, as they don't allow simply appending ;createDatabaseIfNotExist to the JDBC URL, which meant even more native queries (Moving the project over to use MySQL anyway, so this is no longer a problem.)
Related
I'm planning to work on a multi-tenancy application and for now I'm just looking at different implementations on the web to understand the requirements needed to implement such task.
Hibernate + Spring boot are the technologies I'm planning to use.
From my readings, all the different tutorials are using the same approach which is to declare the data sources in a config file so that session factories are launched with the boot of the application, but I really want to have a higher level of the app, where I can add tenants dynamically and input their data sources informations.
This way the application can get the information of the new tenant without the need to touch the config files and re-boot the app.
I thought about having a separate database where I can store my tenants data source credentials or something like that. Can you give me another approach to solve this requirement or a link to an existing implementation that I can refer to.
Thanks
I got similar requirements in the past.
I implemented DataSource proxy class. The class has tenant resolver and a map of simple DataSources. All the places where we need a DataSource use the proxy.
On any method call e.g. getConnection() it resolves tenant, check whether the map contains already created DataSource (if not a new DataSource is created for the tenant and stored in the DB). Then the same method of real DataSource from the map is invoked.
Tenant resolver is ThreadLocal based where tenant value is stored in a filter (got tenant from request header) and used in the DataSource proxy.
What you need to do is using dynamic datasource routing of Spring Framweork via AbstractRoutingDataSource. This answer explains all for you.
In my question.I implements MultiTenantConnectionProvider and CurrentTenantIdentifierResolver.And use DataSourceLookup to choose datasource by tenant.This links is helpful to me.
Here is a full working example of a database-per-tenant multitenat app I built using Spring Boot 2, Spring JPA (Hibernate), Spring Security 5 running on MySQL.
I have explained how it all works and have shared the entire code too.
Take a look here.
I'm building a fairly simple Spring Boot application which needs some configuration to be set (and regularly updated as part of it's use). I'd like to create a simple admin interface and first-run wizard to set/update this configuration.
I'd like a way to easily read and write these configuration values within the application and have them persist. I would like to avoid the overhead of a database for 5-10 configuration strings. There is some good documentation on externalising configuration in Spring Boot but it doesn't talk about how this config could be updated and persisted by the app.
Options I have come up with:
There is a write option with Spring configuration that I'm not aware of (this would be awesome)
Don't use the Spring Boot configuration functionality, instead use apache commons configuration (or similar??) to read and write to a file which lives in a location specified by an environment variable
as per option 2 but use HSQL, H2 or Derby as a file-based database
Thanks for any suggestions as to how best to achieve this.
I'm implementing a multi-tenant system using Spring where each tenant has its own database. I have everything up and running.
I've extended "AbstractRoutingDataSource" and overridden "determineCurrentLookupKey" to determine which connection to use via the users domain/tenancy.
The "AbstractRoutingDataSource" is instantiated when the app loads and all the possible database connections are set there.
Here's my question -
Is there a way of dynamically adding additional connections to the AbstractRoutingDataSource? I want to be able to add additional tenants without restarting.
Any help would be greatly appreciated
Thanks
I am too late to this thread, but recently I needed to build a Saas style multi-tenant web app. Tenants needed to be added dynamically without requiring a server restart. So I shared my learnings here with a complete working example.
Yes You can do So, Here is complete project which explains very nicely about your requirement:
Multi-tenancy: Managing multiple datasources with Spring Data JPA
Is it possible to use Hibernate with Annotations, without Spring framework for a command line application.
Right now i have spring with hibernate configured for command line application.I have main function which received data and it calls other (namely 'storeDB') object to get AbstractApplicationContext context and save the data.
Like to bring up the hibernate first ( bring up hibernate framework,
connecting to DB) and like to wait for a socket to process the data.
Can it be configured through java configuration than XML
configuration
and is it possible do in Hibernate(with annotation) without Spring?
Yes of course you can:
Use Hibernate annotations (without XML configuration) to setup your database connections and map
your entities to your database, for further information about it take a look at:
Hibernate with Annotation.
TutorialsPoint Hibernate - Annotations tutorial.
Use only Hibernate without Spring in a Java Application to manage your persistence strategies, because they are two independents frameworks:
You can see from the Hibernate Definition that:
Hibernate ORM enables developers to more easily write applications whose data outlives the application process. As an Object/Relational Mapping (ORM) framework, Hibernate is concerned with data persistence as it applies to relational databases (via JDBC).
And from the Spring project definition that:
The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is infrastructural support at the application level: Spring focuses on the "plumbing" of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.
Conclusion:
As you can see Hibernate and Spring are two differents frameworks that handles differents levels of Java Applications.
I have a requirement where i need to configure a Spring based application to work with two databases. We have two databases, one that we use to keep the live data and the other database is used as a datawarehouse and contains archived data (which has the exact structure as the Live db).
To keep it simple, assume that there is a request to search for a product. What the application should do is to search for the product details in the Live database and if not found it will check the archive database.
If i need to configure such a setup, do i still need to configure to datasources and would the search code have to use the first datasource to check the live database and if not found it will run another query using the archive database?
The above is probably doable but i am wondering whether there is a better way of doing this. For example, is it possible for the application to work on a single datasource even though behind the scenes it actually works with two databases?
The application is based on Spring, JPA/Hibernate, SOAP and Mysql database and Jboss 7 as the application server.
Any examples showing how this is configured using Spring and Jboss would be very useful.
Thanks
Spring has exactly what you want - the AbstractRoutingDataSource. See this blog post on how to use it. In your case, you need to switch the datasource during one request, so you'll need to have 2 transactions, switching the datasource between them by changing the datasource indicator on the ThreadLocal:
For these DAOs, demarcate the wrapping Service-layer either with distinct packages, class names, or method names
Indicate to Spring that the Service-layer method calls should run in their own transactional contexts by annotating with #Transactional(propogation=Propogation.REQUIRES_NEW)
Create an Aspect (using AspectJ annotation #Aspect) to fire around the service-layer method calls (using #Around) to set the ThreadLocal value before the method call, and to unset it afterwards
In the #Controller, simply call the Service-layer methods. The Aspect will take care of setting the values to indicate which datasource to use, and the AbstractRoutingDataSource will use that datasource in the context of each transaction.