Lost update in wallet withdrawal
Review this Java (JDBC) method that debits a wallet balance.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.
0:00 of about 30 min
Mark a line and say what kind of problem it is.0 findings
1void withdraw(Connection conn, long acctId, BigDecimal amt) throws SQLException {
2 PreparedStatement read = conn.prepareStatement(
3 "SELECT balance FROM accounts WHERE id = ?");
4 read.setLong(1, acctId);
5 ResultSet rs = read.executeQuery();
6 rs.next();
7 BigDecimal bal = rs.getBigDecimal("balance");
8 if (bal.compareTo(amt) < 0) throw new InsufficientFundsException();
9 BigDecimal next = bal.subtract(amt);
10 PreparedStatement write = conn.prepareStatement(
11 "UPDATE accounts SET balance = ? WHERE id = ?");
12 write.setBigDecimal(1, next);
13 write.setLong(2, acctId);
14 write.executeUpdate();
15}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.
Run or narrate your approach, then ask the coach.