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
Во время выполнения они заходят в методА (для того чтобы получить id из базы) и получается так, что пара энтити имеют одинаковый id- what does that mean? Do you write something to the database and want to get the createdid? Then the sameidcan not be.Synchronized не помогает, так как потоки не наследники одного класса и не зависят друг от друга- is this anything at all connected, and here issynchronizedand inheritance? - 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 writingentity1: id=1, entity2: id=2, entity3: id=3, entity4: id=4, entity5: id=5writes soentity1: id=1, entity2: id=2, entity3: id=2, entity4: id=3, entity5: id=4and 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