473,409 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

JDBC deadlock on unrelated records

Hi all,

I'm regularly getting the "deadlocked..you're the victim" message when two
threads work on a table at the same time via JDBC. The two threads don't
update the same records. I suspect the cause is related to index or page
locks, and/or the fact that the UPDATE statements are doing table scans
because there's no index on the primary key (no, I'm not the DBA!)

1. Thread A selects some records to play with:

select top 50
I.*,M.this,F.that
from APInvoice I
inner join M ...
inner join F ...
where I.CmStatus = 'O'

(There is an index on CmStatus + another column called CmTime, so I expect
this index to be used. It's also the only index on the table.)

2. Gradually, within the same transaction that did the SELECT, thread A
updates all 50 selected records:

update APInvoice
set CmStatus = 'S',
CmTime = getdate()
where itemid = (an ID number from the select in step#1).

(There is no index on itemid, which is the unique primary key column)
(There is an index on CmStatus + CmTime).

3. While step#2 is running, thread B starts and does a single update.
That's all that thread B does:

update APInvoice
set CmStatus = 'C',
CmTime = getdate()
where itemid = (an ID number that is NOT in the list of records being used
by thread A)

I consistently get deadlocks whenever thread A & B run at the same time.

To resolve ths problem, I'm looking at the following actions:
1. Create a unique primary index on ITEMID.
2. Add an "(UPDLOCK)" or "FOR UPDATE" to the SELECT statement.

Are these reasonable things to try? Is there something else I should be
doing?
Sep 6 '05 #1
1 2213
Adding a primary key is certainly the best place to start, and would be
better than using query hints. Without a primary key, MSSQL has no way
to know that itemid is unique, so each process must always scan the
whole table, in case there are more rows with that itemid. Even if each
process is looking for a different itemid, they can't guarantee a
consistent update without locking the whole table, which seems to be
what is causing your problem - each process acquires more and more
locks until they run into a deadlock.

With the key, MSSQL will know that each process can lock only one row,
so if the itemid values are different there should be no problem. Even
if they are the same, process B will simply wait for the row or key
lock from process A to be freed, because it knows it doesn't need to
look for any more rows.

And of course adding a primary key is essential for data integrity, as
well as making your data model more explicit.

Simon

Sep 6 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Howie Goodell | last post by:
Hello -- I am trying to optimise a JDBC connection and an Oracle 9i database for reading millions of records at a time to a 1 Gig PC running Win2K or XP. Two questions: 1. Does anyone have...
2
by: Maggie Pong | last post by:
Hi, I had a deadlock situation on SQLServer 2000. When I look at the Locks / process ID screen on Enterprise Manager, for one of the session, I see the object being one table and the index is...
5
by: Steve | last post by:
Hi; I went to the microsoft site to try to find a guide to the error messages that the jdbc drivers give ( for sqlserver 2000 ). I had no luck. Does anyone know if there is such a guide? ...
1
by: bogachkov | last post by:
Hello Joe Over the past several years, I have found your responses to jdbc usage/driver related issues to be extremely helpful. I am sure that you're very busy so I will make my question as...
3
by: Daniel Chou | last post by:
Hello, We are runnig DB2 on AIX and have DB2 deadlock problem. Is there any way or any tool to find out which SQL statements cause the deadlock? Thanks a lot.
3
by: deegs_ca | last post by:
Here is my jdbc info url = jdbc:db2://LXPP04913:60000/TEST driver = COM.ibm.db2.jdbc.net.DB2Driver For some reason as I'm importing data via a jdbc client Db2 gets through a few rows and then...
3
by: Kirsty Ryder | last post by:
Hi I have two tables containing largely unrelated data. Eventually I want a report that lists the contents of each table simultaneously in date order as it were. I think this is better explained...
5
oll3i
by: oll3i | last post by:
my librarybean package library.ejb; import java.sql.*; import javax.ejb.*; import library.common.*; @Stateless @Remote
0
by: halex | last post by:
Hello, I am having deadlock problem when I have a lot of visitors on my website at the same time. I am using NetTiers templates to generate C# classes for accessing DB layer and problem is in my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.