Code Room
Code reviewHardcr-g644
Subject Database deadlockLevel Senior–Staff~28 minCommon in Databases & SQL · Concurrency interviewsIndustries Software development

Question

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.

Talk through your review
Code to reviewjava
void transfer(Connection conn, long fromId, long toId, long amount) throws SQLException {    conn.setAutoCommit(false);    lockAndDebit(conn, fromId, amount);   // SELECT ... FOR UPDATE on fromId, then UPDATE    lockAndCredit(conn, toId, amount);    // SELECT ... FOR UPDATE on toId, then UPDATE    conn.commit();}
Run or narrate your approach, then ask the coach.