2

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!

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your answer. However, I stumbled upone this piece of documentation: docs.oracle.com/javase/1.5.0/docs/guide/jdbc/getstart/… Section 5.1.8 Concurrency Types, states that "To allow a higher level of concurrency, an updatable result set may be implemented so that it uses an optimistic concurrency control scheme" m\implying that certain drivers may choose to implement it OOTB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.