spring data mongodb connect to multiple database instance for each tenant - java

I have a web application which uses Spring and connects to mongdb using spring data mongodb.
I want to extend it by connecting to multiple database instance for each tenant. There are some solutions which are available on stackoverflow. One was mentioned on Multi-tenant mongodb database based with spring data where the code was on github on the following link-
https://github.com/Loki-Afro/multi-tenant-spring-mongodb
I have a question regarding this? Will this approach work for concurrent requests?
I am also doing the same thing, autowiring the MongoTemplate and Repository.
Since these beans are singletons, they will be the same for each request. Then how will the database change for different request and what happens when multiple requests are being served in parallel?

Related

Any way to specify Spring Data endpoint programatically at runtime?

I am forced to discover my database endpoint at run-time. Specifically I have to discover cassandra contact points (not nodes, the actual contact points) via a web service before I can connect to cassandra. There is currently no way for me to pre-load the contact points into a properties file prior to launch.
I am currently discovering end-points and then manually initializing the db driver, and it's cumbersome and error-prone.
I would really, really like to use a JPA-like model for data access, and I'm fond of using Spring Data for straight sql, but the only ways I can find to initialize Spring data are through spring configuration initialization.
Is there a way to delay initialization of spring data, overwrite initialization, or in any other way start up spring data after i have retrieved the contact points from an httpclient call?

Azure Cosmos DB multitenancy Java SDK

We are currently looking into best practices concerning Cosmos DB and its Java SDK in combination with multitenancy. We have a simple Spring Boot based Java service that is providing REST interfaces for CRUD operations on data in a Cosmos DB using the SQL API. Now we need to introduce multitenancy into it. The tenant classifier will be read from a JWT when receiving a REST request and be available in the requests Thread Local context.
Current considerations are to have the data of one tenant in either a separate container or a separate database under one database account. As described in the MS document Multitenancy and Azure Cosmos DB.
Having all data in one container using the tenant classifier as a partition key will not work due to the amount of data, plus we need separation of the data to prevent the noisy neighbour problem.
Are there any best practices on how to design the data access layer - also in regards of performance pitfalls?
Our understanding at the moment
Use one CosmosDBClient per application (as the initialization takes time)
Container names are defined on entity level (#Container())
The official sample create individual repositories and database configs Azure Spring Boot Cosmos Samples which sounds wrong to me
Basically
Should we create on CosmosDB client and dynamically (how?) set the container name or database name before executing a CRUD operation?
Should we create separate clients for every tenant and re-use those instances?

spring-mvc: How to use remote API as an Spring datasource/repository/DAO?

I am trying to build Spring MVC application which has multiple sources of data:
Relational Database (I am using JPA for this. no issues in it)
Remote RestAPI coded in custom swagger and reading it via custom APIClient
Remote RestAPI reading it via OkHttpClient returning JSON String
Assumptions:
Both two remote RestAPIs have different implementation to fetch data
Each remote RestAPI can connect to more than 30+ links in single transaction to fetch data depending upon logic
Both client implementation are singleton by the vendor hence not thread safe ie. swagger APIClient and OkHttpClient (i have plan to maintain instance of client at each link level POJO)
Question:
Is there any way to manage these RestAPI datasources as an datasource in structured way like JPA Repository for RDBMS database

Update database using events in Springboot

I have a requirement to update a database using events in Springboot microservice. Each microservice has its own persistent layer. Microservices are communicating with each other using REST API's.
Scenario:
I have two microservices - Vendor microservice with vendor DB and Order microservice with order DB. When a request is received by the vendor microservice, it will update the vendor Db and also add an order in the order DB and all this should be done in one transaction.
I cannot use a REST API for calling the vendor service to update the order. If any transaction fails, everything should be rolled back. How can I achieve this using events or something similar?
You can use a message queue like Kafka or RabbitMQ. Because you faced an issue known as two-phase commit. You can use transactional outbox pattern that is widely used in projects which consist of microservices.
Spring already provides a consistent programming model for transactions. You can use
#Transactional
to achieve this.
You can read more about transactional annotation in the official documentation
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Transactional.html

How to manage transaction across micro services using 2 phase commit protocol?

I have two micro services. One is in spring boot and other is using simple jdbc apis to perform database operation. I have to perform database operation using these two services.
(i.e.
Insert using jdbc service,
Insert using spring-boot service,
Insert using jdbc service).
These operation should follow acid property.
I have tried saga pattern using axon framework, it is working fine but I want to do it by using 2pc protocol. I have tried 2pc protocol in jdbc service but it is working only for transactions happened only in this service. I also used atomikos framework in spring service, it is working for this service only.
Is there any way to co-ordinate javax.transaction and springframework.transaction ?
For the scenario described in the question, you should try using Oracle Transaction Manager for Microservices (MicroTx). It is a free product that comes with a transaction manager and client library for microservices written in Java and node.js. With this, you can create XA (2PC) transactions involving multiple microservices. Oracle MicroTx - https://www.oracle.com/database/transaction-manager-for-microservices

Categories

Resources