← Back to context

Comment by Someone

6 days ago

> if (!tx.commit())

https://docs.oracle.com/javase/8/docs/api/java/sql/Connectio...:

  void commit()
     throws SQLException

⇒ this code I won’t even compile for the java.sql.Transaction” class that is part of the Java platform.

(I think having commit throw on error is fairly common. Examples: C# (https://learn.microsoft.com/en-us/dotnet/api/system.data.sql...), Python (https://docs.python.org/3/library/sqlite3.html#sqlite3.Conne...))

I wasn't thinking of JDBC SQL transactions specifically, but sure, different APIs can denote retriable transaction failures differently. Instead of:

  if (!tx.commit()) { continue; }

you do:

  try { conn.commit(); } catch (SQLTransactionRollbackException e) { continue; }

and the principle still applies. The simplest solution still involves a `continue` within the `finally` block.

Whether it's a good idea to actually do this directly using SQL connections is another question .. SQL databases usually use pessimistic locking, where the transaction failures are actually "deadlocks" that are preferably avoided through careful ordering of operations within the transaction (or more commonly, YOLOing it using an isolation level that allows read anomalies). Without going into all the details, this probably has a large influence over the design of the SQL APIs you're referring to.