Question

I am using sequence for saving domain object instance in my oracle database. I have a sequence for each table in the database. when I use the save functionality on User or Resource for example, It created a new resource on first try but the ID used was 70 ? the sequence shows the proper next number - 42 since max id in the table is 41. why was id=70 used to insert the new resource?

Also from the next try all inserts fail with this error

org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; SQL [insert into GRARESOURCE (decomm
issioned, disabled, criticality, resourceClass, resourceGroupId, resourceName, ownerId, resourceSegmentId, resourceTypeId, riskSco
re, targetIP, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]; constraint [GRA.SYS_C0012183]; nested exception is org.hibernate.e
xception.ConstraintViolationException: Could not execute JDBC batch update

Not sure whats wrong because this is happening after code restructuring ...where we moved code into new packages..

Edit: I found out the cause, see my response..Thanks guys

Was it helpful?

Solution 4

I realised the main cause of the problem. I had previously used the SequencePerTableOracleDialect, I got that from one of the previous posts by Burt Beckwith. The dialect creates a new sequence for each table when the application is starting up, similar to domain class converted to table. The dialect also makes sure that every tables id sequence is managed through its sequence only and a common sequence is not used for all inserts in the database (which is the default strategy) During the code restructuring I had removed the custom dialect and was using the default 10g dialect.

Thats what was causing the problem!

I see the next number in the tables associated sequence's next value field, that where I come to know that the next val =42 for resource_sequence which is right since max(id) in resource table is 41.

Thanks a lot guys for the insight which in some way help me recollect the real cause! for those who need to know more about the custom dialect, its Here

OTHER TIPS

Sequences can waste numbers, i.e. every record in a table will not necessarily be one after the next without gaps. In other words, the fact that yours jumped to 70 even though the previous DB record had ID=41 does not indicate a problem.

since max id in the table is 41

Sequences don't see the max value of the table and get the next one. They store the current sequential number and you will use the next value.

You can check the actual number with:

select mysequence.currval from dual

Your problem is with the GRA.SYS_C0012183 constraint. See what this checks, and if this is your primary key maybe you can use some more readable name, like MY_TABLE_PK...

Is SYS_C0012183 the primary key constraint? Is it defined no just the id column (which is, I assume, the column populated by the sequence)?

  • How do you determine that next value of the sequence is 42? Are you running a separate query for that?
  • Are you expecting the sequence to give you gap-free numbers? If so, that is a problem with your expectations. Sequences return distinct values but they do not guarantee that there will be no gaps (in fact, you can be guaranteed that there will be gaps because the database shut down or the sequence was purged from the shared pool or a transaction was rolled back).
  • How do you use the sequence? Is there a trigger on the table? Or is Hibernate configured to use the sequence? If Hibernate is configured to use the sequence, post that configuration.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top