In my spring-hibernate application which has active-mq as well, multiple consumers in the same time access one of the method which provides persistence for logging objects to mysql db for us. Due to the concurrency access of that method, the logic I coded in it fails in some cases. I 'm sharing the logic below:
// check the current sourceId whether is included or not in log table
List<MyLogObject> logs = logService.findBySourceId(currentSourceId);
if(logs.size() == 0){
logService.persist(currentLogObject);
}
As you see, if the multiple consumers access this code block in the same time, the size of log should return 0 for them and thus they persist the same object twice or more.
How can I avoid multiple persistence of an object in case of active-mq and multiple consumer usage.
Is it possible to achieve that by using syncronized feature of java?
Thanks in advance.