im dumb
im dumb

Reputation: 73

Entity/row is updated only sometimes with Spring/Hibernate?

I have a table that gets updated (with a timestamp and a user_id) whenever someone turns "on" a device. This device table is also the "core" table that contains device_ids, device_names, etc. I have marked method with @Transactional from Spring, and I have the "devices" table marked as an Entity.

I would expect that when I change the entity, since the method is marked as Transactional, when the method closes, the database would be updated. But it's not (always) happening. I have checked the logs from my server, and see no SQL exceptions. catalina.out looks fine, and so does the MySQL log for errors. What could be the issue here? Should I explicitly save the updated Entity prior to the ending of this method?

Upvotes: 0

Views: 91

Answers (1)

im dumb
im dumb

Reputation: 73

Yeah, so this ended up being a race condition issue with two threads. Since one thread would start a transaction in Spring with the @Transactional annotation, and then another one would start, they both had the same data at one point, but one usually committed later, giving the impression of an issue with the row locking up. There's a reasonable solution somewhere, whether it be forcing your transactions to be of a different isolation level, for example: Serializable for methods touching the same row, or only updating the entity using something like @DynamicUpdate, but they both have pros/cons and both should be considered when trying to fix some weird race condition like what I encountered.

Upvotes: 1

Related Questions