1

I have a situation maintaining a legacy project, using JSF / Primefaces / Hibernate, the database is DB2, the original code was migrated from Delphi to Java, but keeping the database structure since it came from a vendor (we can't change it). There are some tables used to fetch a sequential id (SELECT MAX and UPDATE after that).

The table structure has a composite key (year and number), the issue today is: we select the max number based on the year from a param table (which holds the "next sequential" value). Sometimes users using concurrently get the same number, causing errors when trying to persist duplicated keys.

I tried to implement a Hibernate Interceptor to fetch and set the value during the onSave method, but I was unable to make it avoid the duplicated keys issue (Tried using it as SessionFactory-scoped). Also I tried to make the methods syncronized, but it didn't work also.

Is there a way to prevent this duplicated key issue (programmatically, without the need of changing the database) using Hibernate features?

Thanks in advance!

Selaron
  • 6,105
  • 4
  • 31
  • 39
nortontgueno
  • 2,553
  • 1
  • 24
  • 36
  • How do you manage transactions in your application? BTW: your issue is not related to JSF. – Selaron Jan 04 '19 at 10:47
  • Transactions are managed inside the `save` method, with `session.beginTransaction()` on the start of the method and `session.getTransaction().commit();` at the end – nortontgueno Jan 04 '19 at 10:51
  • This question regarding sequence generation strategies may have answers and links interesting to you: https://stackoverflow.com/questions/10041938/how-to-choose-the-id-generation-strategy-when-using-jpa-and-hibernate – Selaron Jan 04 '19 at 10:53

0 Answers0