r/SpringBoot • u/Fast_Seaworthiness43 • Jan 04 '26
Question Case is being created twice by calling the API twice in Spring/Java
0
Upvotes
r/SpringBoot • u/Fast_Seaworthiness43 • Jan 04 '26
1
u/DominusEbad Jan 04 '26
Uhhh... why is all of that logic happening within an @Transactional? That's going to cause issues, especially since you are making HTTP requests within that transaction (including retries and sleeps). Always keep the scope of a @Transactional very brief. All I see in your method is one .save() call, so you probably don't need the @Transactional at all.
Also, the Thread.sleep() is another potential issue. You are already returning a 202 Accepted, so why not return that earlier and run the tasks on an async thread instead? You can also use TaskScheduler instead of the sleep.
I don't have an exact answer to your question, but cleaning up the glaring issues might help pinpoint where your issue is.
1
u/Dry_Try_6047 Jan 04 '26
This is a mess and there has to be a lot you arent showing / arent mentioning.
As a starting point: why are you using an executor service to submit a future, and then immediately making a blocking call to get the result. Keep it simple stupid: this should be single threaded, at least to debug. If it works single threaded, then you know it's a multithreading issue.
1
u/BikingSquirrel Jan 04 '26
Not willing to dig through the code on mobile, but one underlying issue seems obvious: you miss a unique constraint on some unique identifier. If you had that, it wouldn't be possible to create the entity twice. In the end this identifier needs to be provided by the client.