Rips
Rips

Reputation: 2004

Jpa hibernate sequence pre allocation

I have been going through the JPA documenation and came across pre allocation thing while assiging sequences id .

Though it is very good for obvious performances reasons. But I was thinking about the scenario where it can mess up the things.

As I understand ,when we set the allocation size=100 when using table sequencing or sequencing objects as strategy for generating sequences, the persistence provider like hibernate or Eclipse link will maintain the memory counter and the application code for inserting records wont have to go database for each insert.

When that memory counter reaches 100,database sequence is incremented by 100 and same story continues. All good till now.

But say we get a bug in the production where in we found that records in some of the tables arent being inserted into the DB.

So we prepare the script to insert those records in the DB,now what value we give to the primary keys of the table.If we take the value from the DB,there is a good chance that it will conflict when application tries to insert record using the memory counter and will give SQL exceptions.

Has anybody faced such issue? And what is the best way to handle such situation?

Upvotes: 0

Views: 1301

Answers (1)

JB Nizet
JB Nizet

Reputation: 692181

Just make sure you use the same strategy as your Hibernate generator in the script. For example, if the sequence is at 2300, and the Hibernate generator increments the sequence by 100 and generates the ID 2301 to 2400 without going to the sequence, do the same in your script.

Or stop the application, run your script, and regenerate the sequence with a value that guarantees that the Hibernate generator won't generate an already used ID.

Upvotes: 1

Related Questions