Avoid race condition in accessing db data without synchronized in java?
NickName:H. Shahan Ask DateTime:2017-11-07T19:11:41

Avoid race condition in accessing db data without synchronized in java?

List<String> Codes= req.getCodes();
List<String> existingCodes = helper.getExistingCodes(Codes);
Codes.removeAll(existingCodes);
for (String Code : Codes)
{
  addCode(Code);
}

I am getting a request containing different codes to be inserted in DB in "Codes" list, but meanwhile I have to check no duplication so I am getting the existing codes from DB in "existing codes" and then just removed the duplication and add new codes in DB with function addCode(), now the problem is when multiple threads will access this I m passing the same code for each thread. Thread 1 got an existing code null and moved towards adding a new code but before that thread 2 comes and again gets existing codes which are again null and it will be a move to add the same code again and here the problem comes.

I have written the "merge into" query for adding a code but it isn't working any other solution without using synchronized in java???

Copyright Notice:Content Author:「H. Shahan」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/47156465/avoid-race-condition-in-accessing-db-data-without-synchronized-in-java

More about “Avoid race condition in accessing db data without synchronized in java?” related questions

Avoid race condition in accessing db data without synchronized in java?

List&lt;String&gt; Codes= req.getCodes(); List&lt;String&gt; existingCodes = helper.getExistingCodes(Codes); Codes.removeAll(existingCodes); for (String Code : Codes) { addCode(Code); } I am get...

Show Detail

Java avoid race condition WITHOUT synchronized/lock

In order to avoid race condition, we can synchronize the write and access methods on the shared variables, to lock these variables to other threads. My question is if there are other (better) ways...

Show Detail

Can reflection API cause race condition in synchronized block

If there are multiple threads accessing a synchronized block,we know that race condition will not occur.But if we use Reflection API to change an instance variable,will it cause race condition.If i...

Show Detail

Java AWT drawImage race condition - how to use synchronized to avoid it

After many hours of debugging and analysis, I have finally managed to isolate the cause of a race condition. Solving it is another matter! To see the race condition in action, I recorded a video ...

Show Detail

Avoid Data Race condition in swift

I am getting race conditions in my code when I run TSan tool. As same code has been accessed from different queues and threads at the same time that's why I can not use Serial queues or barrier as ...

Show Detail

Private constructor to avoid race condition

I am reading the book Java Concurrency in Practice session 4.3.5 @ThreadSafe public class SafePoint{ @GuardedBy("this") private int x,y; private SafePoint (int [] a) { this (a[...

Show Detail

Private constructor to avoid race condition

I am reading the book Java Concurrency in Practice session 4.3.5 @ThreadSafe public class SafePoint{ @GuardedBy("this") private int x,y; private SafePoint (int [] a) { this (a[...

Show Detail

Volatile and Synchronized to Solve Race Condition: Singleton Member Field

I was having some problem trying to understand and fix errors reported from Fortify scan. I have this class: public class DaoImpl extends BaseDaoImpl { private static volatile String sNric; ...

Show Detail

Data race and Race condition during lazy initializing a SIngleton

While reading Java Concurrency in Practice book by Brian Goetz, I came across Data races and Race conditions. Data races A program is said to have a data race, and therefore not be a "properly

Show Detail

Race condition in synchronized arraylist

I am enclosing processing of list of objects in a synchronized block, but ended up in race condition. Am I using the synchronized block wrongly or is there any other problem with my code? The list is

Show Detail