Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
Explaning Optimistic Locking Failed Exception after PSQL exception

I have a fallowing scenario that I want to understand clearly so I can fix it in a proper way.

Some entity have to be persisted to PostgreSQL database using hibernate. This is the course of events that happens before the errors.

  1. Entity attempts to be saved into database (as a part of the logic with a multi-threaded execution). That entity has a null character (0x00) as a part of it's attribute and I get an exception caused by

    org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0x00
    

    because PostgreSQL cannot persist null chars, which is OK.

  2. That same entity is then modified, but now in a single-threaded environment after the end of a parallel execution of the above mentioned logic and is persisted again to update the database.

    Now I get an Optimistic Locking Failed exception saying that entity has already been saved and that the object is "Stale".

    org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [com.yc.isystem.business.Offer] with identifier [691909329]:
         optimistic locking failed; nested exception is
         org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.yc.isystem.business.Offer#691909329]
    

Can someone explain what happens in the background and how can I prevent this from happening?

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!