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?
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.
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"
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With