Reputation: 571
I am using hibernate and a version column is provided for the hibernate locking purposes. The problem is that the app will update an entry often enough that the Java's int
limit is reached by the version column. It is possible that the int
limit of the MySQL is also reached.
Is there a way to make the version roll back to zero once it reaches any limit (Java's or MySQL's)?
Sure I can just enlarge the data type to be long. But it is just delaying the inevitable.
Edit: I googled around and found this annotation: @OptimisticLock(excluded=true). Link: http://bit.ly/nczCx1 It seems that it can theoretically work but I haven't successfully use it. Does anyone know how to properly use this annotation?
Upvotes: 3
Views: 2285
Reputation: 92437
The version number is only relevant for time another user is editing the same object. Once all open editings are finished, you could start again with version 1 and have the same safety (of course Hibernate wouldn't reset the version to 0).
See The best way to map an entity version property with JPA and Hibernate
Upvotes: 0
Reputation: 16732
Okay, so you've reached the limit of integer, fair enough. When you increase it to, let say 'long' you have another four bytes available for you. Which is more than enough (it still just delays the inevitable, sure).
You can reach the old limit (of 2**32 updates) exactly 2**32 times, before it starts to overflow again. Lets assume it takes 1 seconds to have that many updates (I guess it took you longer), then it would take you another 2**32 seconds to (or about 136 years) to overflow a long.
But I don't know if there's a different elegant solution, but if there isn't I wouldn't waste the time for such details.
Upvotes: 2
Reputation: 571
@OptimisticLock(excluded=true) works! I just forgot to make sure that I put it on every updated properties. It disallows the version number to be incremented as promised.
Example:
@Entity
@Table(name="some_table")
public class SomeEntity extends BaseEntity {
//... some code
@Column
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@OptimisticLock(excluded=true)
private DateTime lastUsed = new DateTime();
//... some code
}
This way, even if the lastUsed properties is updated (and persisted), the version would not increase.
Upvotes: 0