java 45 lines · 8 steps

Optimistic locking with a version column in JDBC

A repository detects concurrent writes by matching a version number on update, rejecting stale changes.

Explained by highlit
1public class AccountRepository {
2 
3 private final DataSource dataSource;
4 
5 public AccountRepository(DataSource dataSource) {
6 this.dataSource = dataSource;
7 }
8 
9 public Account findById(long id) throws SQLException {
10 String sql = "SELECT id, balance, version FROM accounts WHERE id = ?";
11 try (Connection conn = dataSource.getConnection();
12 PreparedStatement ps = conn.prepareStatement(sql)) {
13 ps.setLong(1, id);
14 try (ResultSet rs = ps.executeQuery()) {
15 if (!rs.next()) {
16 throw new NoSuchElementException("Account not found: " + id);
17 }
18 return new Account(
19 rs.getLong("id"),
20 rs.getBigDecimal("balance"),
21 rs.getLong("version")
22 );
23 }
24 }
25 }
26 
27 public void update(Account account) throws SQLException {
28 String sql = "UPDATE accounts SET balance = ?, version = version + 1 "
29 + "WHERE id = ? AND version = ?";
30 try (Connection conn = dataSource.getConnection();
31 PreparedStatement ps = conn.prepareStatement(sql)) {
32 ps.setBigDecimal(1, account.getBalance());
33 ps.setLong(2, account.getId());
34 ps.setLong(3, account.getVersion());
35 
36 int affected = ps.executeUpdate();
37 if (affected == 0) {
38 throw new OptimisticLockException(
39 "Account " + account.getId() + " was modified concurrently; "
40 + "expected version " + account.getVersion());
41 }
42 account.setVersion(account.getVersion() + 1);
43 }
44 }
45}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1A version column lets you detect concurrent edits without holding a lock for the whole read-modify-write cycle.
  2. 2When an update's WHERE clause fails to match, an affected-row count of zero is your signal that the row changed underneath you.
  3. 3Try-with-resources guarantees connections, statements, and result sets close even when queries throw.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Optimistic locking with a version column in JDBC — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code