We're going to assume that id
is the PRIMARY KEY of the table, or that it's a UNIQUE KEY.
The most likely suspect is that the "random" number is not unique. Likely, Java is using a psuedo-random number generator, which actually generates a repeatable sequence of numbers from the same seed.
I'd recommend a number generator that generates unique values, rather than random values, since the same random value is bound to reappear at some point.
If that ever happens, the set of 48 rows gets updated with the "random" value, but a subsequent query to retrieve those rows, based on that "random" value is also going to pick up rows that were previously updated to the same "random" value.
With that said, with the SQL statement you show, there might be a small window for a "collision" if two of these statements run at the exact same time. (That inline view gets materialized into a temporary MyISAM table, then the outer query runs. I'm not sure if the inline view query obtains an exclusive or intent-exclusive lock on the rows in tracking records. But the symptom of a collision there would be a subsequent query not finding 48 rows with the specified set
value, because another query over-wrote the value with it's own value.
It's very unlikely you've discovered a bug in MySQL DATETIME handling. That code has been exercised for years, and it's unlikely a bug has been introduced there. (I've used it for better than a decade (versions 3.23, 4.x, 5.1, 5.5) and I've never encountered a bug with DATETIME. (I have been introduced, rudely, to documented behavior. But never encountered a real bug.)
Note that the DATE_SUB
function is not necessary; you could get the same result with:
NOW() - INTERVAL 1 HOUR
Note that NOW()
gets evaluated at the start of the statement or block; we prefer to use it over other functions primarily because it's replication safe. The value of evaluated at the time of original execution is preserved in the binary log, so that the same value can be applied on the replica database(s). Other functions that return the current time get re-evaluated each time the function is called, and the value is not preserved for replication.