The situation is this: in entity, 6 entities are created, which in turn are threads. At runtime, they go into saveTelegram (in order to get the id from the database) and it turns out that the entity pair has the same id, so this part is not written to the table.

Synchronized does not help, since the threads are independent of each other. Among the working solutions to the problem were a pause thread.sleep(1000) and auto-increment the id immediately in the table without going into saveTelegram - but this solution does not work. Are there any other ideas how to solve this problem?


 @Lock(LockType.WRITE) public Telegram saveTelegram(String ioNode, long userId, String functionCode, byte[] payload) throws ProcessException { TelegramId histObj = persistenceManager.findTelegramId(); Long entityId = histObj.getNextVal();//вытаскиваем id из таблицы TelegramId persistenceManager.updateTelegramId(histObj); TelegramHist telegramHist = createTelegram(entityId, ioNode, userId, payload, functionCode); telegramHist = persistenceManager.saveOrUpdate(telegramHistoric);//записываем энтити в другую таблицу return telegramHist; } 

since this problem is solved by thread.sleep(1000) and two entities refer to one ID - I think that this method is not thread-safe. And how to solve this problem I do not know

  • five
    Give your code. From the description nothing at all is unclear. Во время выполнения они заходят в методА (для того чтобы получить id из базы) и получается так, что пара энтити имеют одинаковый id - what does that mean? Do you write something to the database and want to get the created id ? Then the same id can not be. Synchronized не помогает, так как потоки не наследники одного класса и не зависят друг от друга - is this anything at all connected, and here is synchronized and inheritance? - iksuy
  • @iksuy Во время выполнения они заходят в методА .... this means that you need to write entities into the database, and there is a separate table in which id lie and instead of writing entity1: id=1, entity2: id=2, entity3: id=3, entity4: id=4, entity5: id=5 writes so entity1: id=1, entity2: id=2, entity3: id=2, entity4: id=3, entity5: id=4 and to the inheritance account - I thought that it is because of non-inheritance that the threads are independent of each other and do not pay attention to synchronized since everyone has their own “key” - Diana Meissen
  • This is all great, but until you show the code you hardly anyone will help - iksuy

2 answers 2

It is necessary in the method itself to make the possibility of some kind of lock for threads.

One simple and working solution would be to use try-lock ReentrantLock.

 public class MyClass { private final ReentrantLock lock = new ReentrantLock(true); public Integer getMethodA() { lock.lock(); try { /// вот тут получение ID return ID; } finally { lock.unlock(); } 
  • @SenoirPomidor do you believe that this is the best option? - alex safsafsd
  • no, not the most optimal, but the question is not in optimization, but in the decision of access to data for different streams - Senior Pomidor
  • ReentrantLock already tried, this piece of code is simply ignored. I even made @Singleton from the class and @Lock(LockType.WRITE) annotated the @Lock(LockType.WRITE) , but to no avail - Diana Meissen
  • one
    Did you accidentally put @Transactional over the class? show your code better, add to the question body - Senior Pomidor
  • no no, only @Singleton and @AccessTimeout are above the class - Diana Meissen

Well, here inheritance does not play any role at all. The simplest thing is to make the method static and synchronized (static synchronized) and there will be synchronization.