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.
Entity attempts to be saved into database (as a part of the logic with a multi-threaded execution). That entity has a
nullcharacter (0x00) as a part of it's attribute and I get an exception caused byorg.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0x00because PostgreSQL cannot persist
nullchars, which is OK.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?