Spring-boot JPA connect to postgres where database and schema are provided at runtime - spring

I am building a multi-tenant application, where the tenant database configuration are stored in Redis. Based on the request, I will query the Redis to get the database and schema configured for that tenant, this application is built on spring boot.
I took a look at spring boot's JPA sample, and did some more google to find a suitable solution for this, unfortunately, I couldn't find one.
Is this really possible to use JPA sample provided here ?
https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-jpa
Please suggest me any other best possible way to tackle this problem
Thanks

Spring Data JPA uses JPA annotations to define domain model. These JPA entities define schema for your database. Obviously you can't change these entities, nor DB schema on runtime.
So no, updating schema on runtime is not possible with Spring Boot + Spring Data JPA combo.
Database connection (e.g. DB URL, username password) could be more flexible. You would need to create DataSource beans programmatically and somehow programatically define which data source to use for each query per tenant. But this wasn't requirement of Spring nor JPA ecosystem, therefore you may face lot of issues.
I was working on such system before, but we were using plain SQL queries via JDBC and were creating DB connection programatically to achieve schema changes on runtime.

Related

Spring WebFlux + reactive DB2 - how to migrate from Hikari datasource and JDBC?

I've migrated an entire project to reactive WebFlux, the only missing thing is the DB queries. At the moment I'm using JdbcTemplate and Hikari Datasources and I need to make DB calls reactive?
I tried to use vertx-db2-client but it seems it does not work fine with Webflux (or I'm not capable to use it correctly).
What options do I have? Can you show any working example with Webflux and DB2?
Thanks
You would need to find that the database DB2 is supported by Spring data reactive libraries. When I look at https://spring.io/projects/spring-data-r2dbc I don't see DB2 being in the list of database they support.

Spring JPA change datasource depending on request header

I developed an application with spring-data-rest.
I love it and it works like a charme.
What I want to do (to implement one backend for multiple customers) is to change the datasource of my repository depending on an apikey which is sent in a custom request header.
The connection info (url, credentials, database) can be retrieved from an external microservice which manages all the database configurations.
The idea is to retrieve all available database connections on startup and store them in a map with apikey as key and the connection info as value.
I´m not clear about how I can change the datasource of my repo for each incoming request at runtime.
Any ideas?
Depending on your JPA provider, what you are wanting will be multi-tenancy support.
For Hibernate, there is a nice multi-tenancy API available that plugs in with Spring very nicely for configuring what data source to use. MultiTenantConnectionProvider and CurrentTenantIdentifierResolver for some API details.
I finally found a solution using the AbstractRoutingDataSource.
This article saved my day. A really easy to use and to understand solution.
http://fizzylogic.nl/2016/01/24/Make-your-Spring-boot-application-multi-tenant-aware-in-2-steps/

Connecting Spring to Existing JDBC Connection

I've followed this tutorial through Spring's docs. It's nice, but I can not find anything online (and current) that shows how to connect to an existing DataSource through JDBC using Spring. Where can I configure this connection? This tutorial only shows how to create one (but where exactly is it even doing that?).
From your how-to link, you can see:
Spring Boot spots H2, an in-memory relational database engine,
and automatically creates a connection. Because we are using
spring-jdbc, Spring Boot automatically creates a JdbcTemplate.
The #Autowired JdbcTemplate field automatically loads it and
makes it available.
You can bypass the default DataSource by adding #EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) to your Application class. This link provides more detail on setting up a DataSource in Spring Boot.
...and the other sections are pretty useful too!
You are using Spring boot which contains embedded databases, in this case H2 database (check line "Spring Boot spots H2, an in-memory relational database engine, and automatically creates a connection.")
To provide custom connection and configure DataSource see this section https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

Spring JPA with Jersey

I am writing a rest service using Jersey. The data access layer will be developed using Spring JPA. I will be interested to know how can I pass database properties to the Repository layer.
take a look at #Blaise Doughan's answer to a similar question
How to insert data from database with Web Service in java using JAX - RS

Spring Security default database authentication schema

I've been working with the default database model provided by Spring Security to authenticate users. I've realized that Spring Security looks for these tables in the default schema configured for the database engine.
Now, suppose the following:
You are working with PostgreSQL and you have 2 schema: schemaOne and schemaTwo, where schemaOne is configured as the dafault one for the engine. Then, suppose that you have an application that uses schemaTwo, then you will need Spring Security database model to be in that schema. So, once you try to run your application, Spring Security will try to look for the authentication model in schemaOne.
So, my question is: is there a way to push Spring Security to use a different schema than the default one configured for the database engine?.
Thank you.
Well, you can configure custom JDBC queries, for example:
With XML - override authorities-by-username-query, etc.
With JavaConfig - set authoritiesByUsernameQuery(String), etc.
But, I don't think you can just set the schema name.

Resources