The 30 second answer
Error 1205 means this session was the deadlock victim and its whole transaction has already been rolled back. I catch that error, retry the entire unit of work, and pause briefly with a randomized delay before the retry so the other session can finish and release its locks. If I ignore 1205, the application may keep going as if the writes were committed.
When SQL Server chooses a victim, it has already rolled back that transaction and released its locks. The error message tells the client to rerun the transaction. Any application that sends queries can be chosen, so the code that starts the unit of work must handle 1205. If it does not, it can continue as if the work succeeded.
I catch error 1205, retry the whole transaction from the beginning, and pause briefly first. The docs recommend a short random pause so the other session can commit and so both sides are less likely to deadlock again on the retry. Their example is a random delay between one and three seconds; I treat that as guidance, not a fixed SLA. Retrying only the last statement is not enough, because earlier statements in the same transaction were rolled back too.
In Java, SQL Server surfaces the error number on SQLException.getErrorCode().
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
final class DeadlockRetry {
boolean tryBookSeats(Connection connection) throws SQLException {
try {
connection.setAutoCommit(false);
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("UPDATE Seats SET Status = 'Held' WHERE Id = 'C7'");
statement.executeUpdate("UPDATE Seats SET Status = 'Held' WHERE Id = 'C8'");
}
connection.commit();
return true;
} catch (SQLException e) {
connection.rollback();
if (e.getErrorCode() == 1205) {
return false; // caller retries the whole transaction after a short random pause
}
throw e;
}
}
}
Sources (2)
- Microsoft Learn — Deadlocks Guidelearn.microsoft.com
- Microsoft Learn — MSSQLSERVER_1205learn.microsoft.com