| @@ -450,10 +450,20 @@ open class ItemsService( | |||||
| val avg = row["avgUp"] as? BigDecimal ?: return@mapNotNull null | val avg = row["avgUp"] as? BigDecimal ?: return@mapNotNull null | ||||
| id to avg.toPlainString() | id to avg.toPlainString() | ||||
| }.toMap() | }.toMap() | ||||
| // Use JDBC UPDATE instead of load+save: avoids ObjectOptimisticLockingFailureException when the same | |||||
| // Items row was already updated earlier in this transaction (e.g. M18 PO sync) or concurrently. | |||||
| idList.forEach { itemId -> | idList.forEach { itemId -> | ||||
| val item = itemsRepository.findById(itemId).orElse(null) ?: return@forEach | |||||
| item.averageUnitPrice = avgByItem[itemId] | |||||
| itemsRepository.saveAndFlush(item) | |||||
| val avg = avgByItem[itemId] | |||||
| jdbcDao.executeUpdate( | |||||
| """ | |||||
| UPDATE items | |||||
| SET AverageUnitPrice = :avg, | |||||
| modified = NOW(), | |||||
| version = version + 1 | |||||
| WHERE id = :id AND deleted = 0 | |||||
| """.trimIndent(), | |||||
| mapOf("avg" to avg, "id" to itemId), | |||||
| ) | |||||
| } | } | ||||
| } | } | ||||