Domanda

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!

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top