Support Article
Violation of PRIMARY KEY constraint 'PK_PC_HISTORY_WORK'
SA-863
Summary
As records are imported, a note record is made in a history table with a timestamp in the primary key. The primary key of the history record uses a timestamp for making the keys unique. If two records are added within the same millisecond, the keys will not be unique. This fails the primary key constraint which includes the time.
Error Messages
SQLState 23000, Error code 2627: com.ibm.websphere.ce.cm.DuplicateKeyException: Violation of PRIMARY KEY constraint 'PK_PC_HISTORY_WORK'. Cannot insert duplicate key in object 'dbo.PC_HISTORY_WORK'.
Steps to Reproduce
1. Add a history record using getCurrentTimeStamp to provide the unique key.
2. Add another history record within the same millisecond also using getCurrentTimeStamp to provide the unique key.
Root Cause
The root cause of this problem is a defect in user code. Using the function getCurrentTimeStamp to provide keys to the history records is not robust. As this only provides uniqueness if no two records are created within the same millisecond, when two history records are created within the same millisecond, you get a PRIMARY KEY constraint violation
Resolution
As a local-change, switch user code to use getCurrentTimeStampUnique which ensures that unique keys are always created.
Published November 20, 2015 - Updated October 8, 2020
Have a question? Get answers now.
Visit the Collaboration Center to ask questions, engage in discussions, share ideas, and help others.