6
votes

The JPA specification (2.1) says that:

The following types are supported for version properties: int, Integer, short, Short, long,Long, Timestamp

What is the expected behavior after a @Version property overflows?

2
I guess this is an academical question, since even if you have 1 billion updates per day it would take about 25 million years to get an overflow with Long...Puce
Note that the version property may be short, in which case it only takes 32k updates.Javier
why not try it, see what datastore type a "short" will create, and then do an INSERT of the max of a short + 1 ... does the datastore throw an SQLException?Neil Stockton
I would not recommend setting your @Version property yourself. Be prepared for some unexpected behaviour.EasterBunnyBugSmasher

2 Answers

4
votes

I would expect it to work correctly. No error occurs on overflow and version++ != version. It will result in the risk of overwriting updates if you're using short and have 65536 transactions with updates on this entity before the first one finishes.

Edit: when you use @Version, then update queries will not look like this:

update person set surname = ? where id = ?

but like this:

update person set surname = ?, version_field = ? where id = ? and version_field = ?

now JDBC will return the update count upon completion. If no update has been made, then the JPA implementation will assume that no data with the specified id and version was found -> Exception.

0
votes

I can confirm EasterBunnyBugSmasher's answer on Hibernate 5. Defined a byte @Version field, once reached 128 went around to -127 and kept looping.

(I have no reputation to comment)