0

When I am trying to update row with MySQL database. I am randomly getting MySQLTransactionRollbackException exception. For that I have written retry code in

catch (MySQLTransactionRollbackException  ex ) {
   //Retry Logic
} 

But I am using both MySQL and SQL Server databases. So I don't know how to handle this specific exception for SQL Server database? I am not sure even SQL Server database is throwing this kind of exception or it is throwing different exception for this same reason.

6
  • 1
    Depends what kind of technology you're using, but i think in general, mssql drivers just through some generic exception and don't try to be "clever", so you'll have to check the error message or error state/number to figure out if it was deadlock. You might wanna check why you get so many deadlocks =) Commented Apr 12, 2024 at 9:01
  • Yes.. But I am not able to get this kind of exception every time So I have blindly added try catch around my code where it is throwing the exception for MySQL.. Commented Apr 12, 2024 at 10:41
  • Deadlocks occur due to problems in the SQL queries and connection management issues. They occur because the queries executed by two different connections need access to rows locked by the other so neither one can proceed. The longer a connection or transaction remains, the longer locks are held, the higher the chance of deadlocks. There's no magic solution beyond keeping connections and transactions as short as possible, which is why you always see connections created in using blocks right before they're used and closed immediatelly Commented Apr 12, 2024 at 12:07
  • The optimistic concurrency model introduced in the late 1990s and used in ADO.NET since 2001 reduced deadlocks dramatically, because no connection or locks are held between the time an application reads data and the time it stores any changes. If you get deadlocks it means the application and queries don't work that way, taking and holding excessive locks Commented Apr 12, 2024 at 12:13
  • This is not because of specific transaction implemented in the code.. This is throwing exception internally.. Commented Apr 12, 2024 at 12:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.