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

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

Related

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

Handle transactions in Spring Cloud Microservices without using MQ

I know about the 3 phase commit and SAGA pattern(doing transactions async in a distributed system using MQ). But it would involve using an MQ which makes me think of approach being taken mostly in SOA using ESB. Is there any way in which consistency can be achieved in between transactions in microservices using SpringCloud's inbuilt functionality.

Distributed transaction among services in a microservice system, using spring-cloud

The current situation
Got a project using: spring-boot, spring-cloud, postgresql, as a microservice system.
There are 2 services, say SA and SB, they operate on 2 RDBMS databases respectively, say DA and DB.
Now, there is an operation contains 2 sub steps:
Http client would make a request to service SA, to save a record RA, into DA.
Then, SA send a request to service SB, to save a record RB, into DB.
As a whole, the 2 sub steps should either both commit, or both rollback.
Analysis
If move both operations into a single service, then could use Spring's distributed transaction to sovled it with JTA (based on 2PC protocol).
But here, the 2 operations are in 2 services, and they communicated via http REST protocol. Maybe could use mq + compensation to solve this, but I am not sure is there a better approach.
The questions are
In this case, does JTA (based on 2PC protocol) still work?
If not, what is the preferred solution?
Possible solutions I can guess:
Refactor code to move the 2 operations into a single service.
Implementat the mq + compensation architecture to support this.
Maybe this project is helpful for you https://github.com/apache/servicecomb-pack
Apache ServiceComb Pack is an eventually data consistency solution for micro-service applications. ServiceComb Pack currently provides TCC and Saga distributed transaction co-ordination solutions by using Alpha as a transaction coordinator and Omega as an transaction agent

Spring Boot - Central db Connection for multiple microservices application

Hello stackoverflowers!
So I am working on spring boot microservices and following scenario came up:
I have follwing services:
user-services
admin-services
financial-services
and couple more..
I have spring-cloud where all my apps are reading configs from in combination with Eureka as discovery and Zuul as gateway servers.
Here's the scenario, the database is only one (very old legacy one) which we can't break into multiple dbs per microservice app (as per true microservice concepts). Now when all these apps will be started (most probably 2 instances per service) so there will be around 30 opened connections on the db. Is there a way to use a common database layer since there is only one db and all the apps configured to that layer?

spring data mongodb connect to multiple database instance for each tenant

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?

Categories

Resources