Home > Back-end >  JPA @Version behavior when data is changed from unmanaged connection
JPA @Version behavior when data is changed from unmanaged connection

Time:05-05

Enabling @Version on table Customer when running the tests below

@Test
public void actionsTest1 () throws InterruptedException {
    CustomerState t = customerStateRepository.findById(1L).get();
    Thread.sleep(20000);
    t.setInvoiceNumber("1");
    customerStateRepository.save(t);
}

While actionsTest1 is sleeping, I run actionsTest2 which updates the invoice number to 2.

@Test
public void actionsTest2 () throws InterruptedException {
    CustomerState t = customerStateRepository.findById(1L).get();
    t.setInvoiceNumber("2");
    customerStateRepository.save(t);
}

When actionsTest1 returns from sleeping it tries to update too, and gets a ObjectOptimisticLockingFailureException

Works as expected.

But if I run actionsTest1 and while it is sleeping I open a SQL terminal and do a raw update of

update customer 
set invoice_number='3' where id=1

When actionsTest1 returns from sleeping, its versioning mechanism doesn't catch the case and updates the value back to 1.

Is that expected behavior? Does versioning work only with connections managed by JPA?

CodePudding user response:

Is that expected behavior?

Yes.

Does versioning work only with connections managed by JPA?

No, it also works when using any other way of updating your data. But everything updating the data has to adhere to the rules of optimistic locking:

  1. increment the version column whenever performing any update
  2. (only required when the other process also want to detect concurrent updates): on every update check that the version number hasn't changes since the data on which the update is based was loaded.

CodePudding user response:

It works as expected. If you do a update manually, you have to update your version as well.

If you using JPA with @Version, JPA is incrementing the version column.

To get your expected result you have to write the statement like this

update customer set invoice_number='3', version=XYZ (mabye version 1) where id=1
  • Related