I am using hibernate for data persistence with Spring.
Scenario is
Time- 00 Hour:01 Minute:01 Second:100 ms
- Request-1 received to persist a Person with Name "abc", Last Name "Xyz" (Consider this session 1)
- DB is looked up if the same record exist
- There is no record found
- Object gets created, Hibernate generates unique ID for the record
Time- 00 Hour:01 Minute:01 Second:102 ms
- Request-2 received to persist a Person with Name "abc", Last Name "Xyz" (Consider this session 2)
- DB is looked up if the same record exist
- There is no record found
- Object gets created, Hibernate generates unique ID for the record
Time- 00 Hour:01 Minute:01 Second:103 ms
- Session 1 persist the record with saveOrUpdate();
Time- 00 Hour:01 Minute:01 Second:104 ms
- Session 2 persist the record with saveOrUpdate();
As both sessions are generating unique identity, hibernate is treating them as separate object and persisting in DB. but this, later on, causes issues in application.
I have unique index too but that includes the Id field also, so DB is also unable to treat them as unique record.
Suggest ways to avoid duplicate insertion.