Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transaction (Process ID) was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim

I have a java program that updates a table in MS SQL. This table is also accessed by web users through a website created in ColdFusion

Recently I have been getting this error when the line:

sql_stmt.executeUpdate("update random_selection "
    + "set forecasted = 1 where "
    + " randnum = " + ora_rs.getString("RANDNUM")
    + " and quarter = " + quarter
    + " and ozip3 = " + ora_rs.getString("OZIP3"));

The CF query that is erroring is :

<cfquery name="submit_forecast" datasource="ttmsdropper" username="#request.db_username#" password="#request.db_password#">
    INSERT INTO forecast_entry
    VALUES (<cfqueryparam value="#currentRecord[8]#">)
</cfquery>

What is causing this error and how can I fix it?

like image 686
Mike Avatar asked Jun 06 '12 13:06

Mike


People also ask

What is deadlock transaction process ID XX?

Transaction (Process ID XX) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction. The system_health is the default extended event session of the SQL Server, and it started automatically when the database engine starts.

How to tell if a transaction is deadlocked?

Unless you had one of the traceflags on or a profiler trace running with the deadlock events, there's no way to tell now what happened. "transaction (Process ID 59) was deadlocked on lock resources with another process and has been choosen as the deadlock victim.Return the transaction"

What is deadlock transaction 59?

Transaction (Process ID 59) was deadlocked on lock communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction Show activity on this post.

Why do I get a deadlock error message?

The error message obviously was indicating a deadlock problem. As a first step, he decided to check the system_health session for the deadlocks. Transaction (Process ID XX) was deadlocked on lock resources with another process and has been chosen as the deadlock victim.


2 Answers

A deadlock occurs when 2 processes try to hit the same data at the same time - both with an equal claim on the data. It's most common when there is a lot of update/insert activity going on (as you describe). The DB system "chooses" one of the transactions to be the "winner".

In some cases deadlocks might be improved or mitigated by indexing but only when selects are involved - a good indexing strategy might improve select performance and make row locking more efficient. However, in the case where the deadlock is coming from an insert contending with an update, indexing will NOT help. Indeed aggressive indexing could degrade the situation since the indexes have to be updated along with the data inserts or updates.

How to solve it greatly depends on your system and what you are trying to do. You have to either minimize the insert/update locking or provide more or faster resources somehow. Bundling inserts together and batching them, more procs or RAM (sometimes - not always), clustering, splitting tables and data, fine tuning parallelism - these could all be viable options. And there's no hard and fast rule.

like image 187
Mark A Kruger Avatar answered Oct 27 '22 10:10

Mark A Kruger


If there are never update to the table ( only inserts ) then you may want to try changing selects to - with no lock or wrapping select in cftransation readuncommited.

As Mark said this is a DB error not ColdFusion and locks are occuing. If there are complicated selects and updates look add adding indexes to the clause columns.

like image 31
Paul Avatar answered Oct 27 '22 10:10

Paul