Transactions in REST [duplicate] - spring

This question already has answers here:
Transactions in REST?
(13 answers)
Closed 7 years ago.
How can I simulate transactions in REST?
I have just developed the back-end with Jersey, Spring, Spring Data for data access, which connects to a MySQL DB. My goal is to show(like a test) that it can handle database transactions, but can't imagine how to do it.

You would test a RESTful endpoint that does a transaction the same way you would test an endpoint that does not do a transaction - by calling it.
Write an integration test (probably Junit, and make sure it uses the #WebAppConfiguration annotation) that calls your RESTful endpoint(s). In your test, inject (#Autowire or #Resource) your service that contains your endpoint. Call the methods on that service (i.e. the endpoint), passing in fake or generated parameters. In your test, look for the expected behavior. For example, if you accessed a PUT endpoint that was supposed to create a book, using the DAO or access object, try to retrieve it from the database. If the DAO can retrieve it, then the endpoint successfully negotiated the transaction. Similarly, set up a test where the transaction should not go through, and therefore be rolled back, and use the DAO to make sure it did not get in the database.

Related

Need to update multiple collection of cosmosdb with spring boot. How to manage the transaction. If anything failed all should be rolled back

How to handle transaction using spring boot and cosmosdb. For example write has to made in mutiple collections, if anyone of them fails then all should be rolled back. It is written in this link that its possible with version 4 api.
How to handle Transaction in CosmosDB - "All or nothing" concept
Any integration example will be helpful.

is it possible to get the same connection from connection pool with the context?

Is it possible to get the same connection from the connection pool with the context?
Basically you can think of this scenario to understand the reason of the question; I have a Spring Boot app, within that Spring Boot app, there are RESTful services. When there is a request to RESTful services, I get the current connection via Aspect programming and I execute a stored procedure to set user details, so that I can use the user details in DB within the connection.
Currently there is an unwanted behavior; and that can occur if the two request to the RESTful services are executed within the same connection. This unwanted behavior happens rarely, so I assumed that two different requests are executed within the same connection.

Spring Boot: how to implement workflow as synchronous REST services integrating external applications

I'm here to ask you for a suggestion on which technology to use to add new features on an existing application based on Spring Boot.
The new features consist of some workflows, exposed as synchronous REST services, that must update the database and call REST services exposed by external applications, which consequently will update their own database.
For example, a service could implement this workflow:
insert rows in database
call the REST service of the application X, which will update its database
update rows in database
call the REST service of the application Y, which will update its database
update rows in database
This workflow must be synchronous and it is started from an human operator that will receive the outcome in few seconds.
If, for example, the step 4) fails, I need to:
make a compensation, calling another REST service of the application X, in order to undo what it made in step 2)
rollback the insert/update made in my database in steps 1) and 3)
Which technology, framework, tool or other would you use? In the past I implemented a similar scenario using Oracle SOA, but in this case I would avoid to introduce a new infrastructure in my application based on Spring Boot.
Thank you
I guess you need to learn a bit more about Spring Framework and Spring Boot.
1.insert rows in database : Spring Data JPA
2.call the REST service of the application X, which will update its database : A Http Client such as RestTemplate or WebClient
3.update rows in database : Spring Data JPA (again)
4.call the REST service of the application Y, which will update its database update rows in database : RestTemplate...
So so and so...
If you want to make a real workflow, you can use Activiti.

Transaction management of JPA and external API calls

I'm new to spring, started using spring boot for the project. We have an use case of implementing database changes and few external API calls as one transaction. Please suggest, is this possible with the spring #transactional?
Do the API calls need to be part of the transaction?
If the answer is no, I would advise to use TransactionTemplate.doInTransaction() leaving the API requests outside of the Tx.
If you need to make the API requests inside a Tx, I would advise against it, you would be locking DB resources for the duration of those requests.
You can also search and find out more about the eventual consistency model.
Using #Transactional for multiple database changes as one transaction is of course doable with the annotation but not so much for the external API calls. You would have to implement some custom logic for that - there would have to be endpoints to undo your last actions and you would have to implement calling them manually in try-catch block for example. For example, if the external API call creates an item there would also have to be an endpoint to delete an item and so on.
So to summarise - using #Transactional annotation for implementing database changes as one transaction is fine, but not enough for external API calls.

Is Spring XA transactions across non-transactional backends feasible at all?

I'm writing an application that has to communicate across 3 different platforms. Two expose their DB via a REST API (no jdbc driver) and one is a native JDBC connection (ex: Derby, MySQL, Oracle, etc).
My problem is that I have no way of assuring any ACID'ity when updating data, given that the three should be updated at the same time.
I've tried reading up on Spring XA but it seems as both 2PC and 1PC require some form of transactional backends. Given that 2 of my 3 destinations are REST APIs, I don't have any transactions. Just a save/update option.
Are there techniques I can use to ensure that the 3 sources are synchronized and that I don't run into inconsistent states if ever a write fails (ie: REST endpoint unavialble, etc)?
A transaction example would be:
Read from DB
Write to REST-1 endpoint
Update DB
Write to REST-2 endpoint
Is there some form of XA I could employ to wrap everything in such a way I can be assured of consistency?

Resources