문제

I have a classic scenario: two updates to the same record that are based on a previously ready value from this record. I am working under the assumption of optimistic concurrency. It is not hard for me to implement the conditional update by myself- the question is whether I can rely on a certain API from the driver or the DB that will handle it for me?

Naturally, I Googled it, but all I seem to come up with are explanations of WHAT is optimistic concurrency, without mentioning code samples...

I am using JDBC data direct driver.

Thanks!

도움이 되었습니까?

해결책

No, JDBC doesn't have any support for optimistic concurrency. Optimistic concurrency is handled by JPA by using a dedicated version column to the table, and incrementing the version value each time an update is made. The update query looks like this:

update foo set ..., version = version + 1 
where id = :theId and version = :theVersionLoadedInMemory

If executeUpdate() returns 0 instead of 1, it means that someone else deleted the record, or updated it and thus incremented the version.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top