DB2 Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDB2 Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 13th, 2007, 02:25 AM
asc asc is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 95 asc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 20 m 6 sec
Reputation Power: 5
Row-Level Locking

Hello!

I'm new to DB2 and I have a locking problem: I am writing a Java client that uses a DB2 backend which is accessed via JDO.

What I want to do is to lock a row for read and write access. I.E. Client_1 locks a row, reads the row and updates some data. While Client_1 has the lock, no other client should be able to read or change the row. Since the JDO locking methods failed, I would like to do it using SQL statements directly.

The first thing I tried was a "select id from some_table where id=some_value for update" statement, but I got a JDO error message, saying that the clause "for update" was used for a read-only cursor.

Any ideas, what I have to do to acquire and release the lock?

Reply With Quote
  #2  
Old April 15th, 2007, 11:41 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
I will address this from a Java perspective since that is what you are using. You need to start a Transaction in the java database Connection class. See the setAutoCommit() method. You can set the Transaction Isolation Level in your Connection object when you connect to DB2. Please read the setTransactionIsolation() method description.

Note that, I believe this will 'lock' the record of that table for the row you are reading, but only for other connections that have started a transaction. Regular connections started without a transaction, from how I understand it, will still happily read your locked row, but should not be able to update it. There is no way, from how I understand it, to lock a row in a table so that all other connections cannot read it if those connections are not in a transaction.


EDIT: if you are using something like Apache JDO, then you will have to see the documentation for that API to help you understand how to do the things I mentioned in my post.
__________________
~Nemi

Before posting did you try:
[ Javadocs | Google ]

Last edited by Nemi : April 15th, 2007 at 11:44 AM.

Reply With Quote
  #3  
Old April 20th, 2007, 01:51 AM
asc asc is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 95 asc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 20 m 6 sec
Reputation Power: 5
Helllo Nemi!

Thank you for your reply. I have tried what you suggested but it did not work. As it turned out this is due to a bug in the JDO implementation in connection with a DB2 backend.

I solved the problem by implementing my own locking mechanism.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDB2 Development > Row-Level Locking


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway