You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This will effective load or refresh the bean and obtain a database row lock using select for update
try (Transactiontransaction = DB.beginTransaction()) {
// fetch something that relates to what we want to lockOrderorder = DB.find(Order.class, o0.getId());
// we have a reference bean (only has id) but could be partial or fully loadedCustomercustomer = order.getCustomer();
// load and obtain database row lock// load the customer bean using select for update// bean is now loaded and database row lock held until commitdatabase.lock(customer);
...
transaction.commit();
}
More fully explained with asserts
try (Transactiontransaction = DB.beginTransaction()) {
LoggedSql.start();
Orderorder = DB.find(Order.class, o0.getId());
assertorder != null;
Customercustomer = order.getCustomer();
assertEquals(customerId, customer.getId());
assertTrue(DB.beanState(customer).isReference());
DB.lock(customer); // load the customer bean using select for update// bean is now loaded and database row lock held until commitassertFalse(DB.beanState(customer).isReference());
assertThat(customer.getName()).isNotNull();
List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(2);
assertThat(sql.get(0)).contains("from o_order");
assertThat(sql.get(1)).contains("from o_customer t0 where t0.id = ? for update");
transaction.commit();
}
The text was updated successfully, but these errors were encountered:
rbygrave
changed the title
ENH: Add lock(bean) method as convienence for DB pessimistic locking
ENH: Add lock(bean) method as convenience for DB pessimistic locking
Sep 9, 2021
This will effective load or refresh the bean and obtain a database row lock using
select for update
More fully explained with asserts
The text was updated successfully, but these errors were encountered: