Code Room
Code reviewHardcr-g204
Subject Transaction isolationLevel Senior–Staff~28 minCommon in Databases & SQL interviewsIndustries Software development

Question

Review this Java (JDBC) reconciliation routine that asserts debits equal credits.

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 reconcile(Connection conn, long batchId) throws SQLException {    conn.setAutoCommit(false);    long debits = scalar(conn,        "SELECT COALESCE(SUM(amount),0) FROM ledger "        + "WHERE batch_id = ? AND kind = 'D'", batchId);    long credits = scalar(conn,        "SELECT COALESCE(SUM(amount),0) FROM ledger "        + "WHERE batch_id = ? AND kind = 'C'", batchId);    if (debits != credits) {        alert("Imbalance in batch " + batchId);    }    conn.commit();}
Run or narrate your approach, then ask the coach.