Code RoomMoney transfer hangs
HardPrep Room Coding #2268

Money transfer hangs

Code reviewDatabases & SQLConcurrencySenior–Staff~28 min

Review this Java money-transfer method.

This runs on many threads; transfers go in both directions between the same accounts.

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 28 min
Mark a line and say what kind of problem it is.0 findings
1void transfer(Connection conn, long fromId, long toId, long amount) throws SQLException {
2 conn.setAutoCommit(false);
3 lockAndDebit(conn, fromId, amount); // SELECT ... FOR UPDATE on fromId, then UPDATE
4 lockAndCredit(conn, toId, amount); // SELECT ... FOR UPDATE on toId, then UPDATE
5 conn.commit();
6}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.