@Version a powerful annotation to handle shared resources

Problem Statement : Two instances of a system attempt to update the same shared resources simultaneously. It is crucial to prevent the loss of changes made by the first instance during the update process.

To ensures concurrent updates are handled gracefully, and changes made by the first instance are not overridden by the second instance. When an instance retrieves a resource for modification, it checks the version number.

Once data is saved to database we can update multiple times but we don’t know exactly how many times data has been modified or updated.so, if we will go for @version automatically it will increase version number if object get modified.

Add @VersionĀ entity class automatically it will add column called ‘version’

or can be defined like

@Version
@Column(name = “version”)
private long version;

if more than two resource try to update same object it throws Exception

org.springframework.orm.ObjectOptimisticLockingFailureException: Object of class [com.library.Bill] with identifier [43]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.library.Bill#43]

Leave a Reply

Your email address will not be published. Required fields are marked *