aspose file tools*
The moose likes JDBC and the fly likes HELP - JDBC Connection & locking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "HELP - JDBC Connection & locking" Watch "HELP - JDBC Connection & locking" New topic
Author

HELP - JDBC Connection & locking

David Swan
Ranch Hand

Joined: Oct 08, 2003
Posts: 33
Hello,
I dont think i understand JDBC correctly. I want to lock a record so that if i issue a statement like (select * from customer where customerID='123') then it will lock this record so that if another user wanted to select the same customer ID record they would be prevented from doing so. Can anyone tell me how to do this OR if is even possible???
Cheers,
Dave


SCJP, SCWCD, SCBCD
john smith
Ranch Hand

Joined: Mar 04, 2004
Posts: 75
I don't know if you should be trying to lock records on select - you are not changing the data in anyway so why lock it?
JDBC doesn't do anything to lock records directly, you need to check the docs of your DB to find out when and what it will lock.
If you are worried about data changing in the schema while a client is trying to update it, use a locking pattern such as Optimistic Locking and JTA transactions. (There's one explained here http://www.javaworld.com/javaworld/jw-07-2001/jw-0713-optimism.html)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

"cluck cluck",
We're pleased to have you here with us in the JDBC forum, but there are a few rules that need to be followed and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
Thanks!
bear
JDBC Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dana Hanna
Ranch Hand

Joined: Feb 28, 2003
Posts: 227
Transaction type SERIALIZABLE will lock records that have been read while a transaction is open. You specify it at the createStatement or executeQuery - I can't remember which. Or maybe it's at the connection level... Whatever.
Either way, that's what it's for - to lock read records while a transaction is open so that they don't change.
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Transaction type SERIALIZABLE will lock records that have been read while a transaction is open
.
Actually, what I believe it means is that the DBMS will not allow a value to be accessed until the transaction is committed.

I want to lock a record so that if i issue a statement like (select * from customer where customerID='123') then it will lock this record so that if another user wanted to select the same customer ID record they would be prevented from doing so

The Connection.TRANSACTION_SERIALIZABLE flag will not allow dirty reads while during a transaction. But this would be of no help during a simple query of the table. If the auto-commit is set to true, then each statement is a transaction, so locks are only held during one statement. Yes, you can manipulate the auto-commit during a series of update/delete statements until you commit the transaction or rollback the transaction.
But, I believe the type locking mechanism you are wanting, will have to be developed at the program level.
My $0.02
Craig
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HELP - JDBC Connection & locking
 
Similar Threads
[URLYBird] Locking Test Cases
Does the lock associate session ID or customer ID?
GUI display & booking questions
Row locking using oracle database
Will this locking mechanism be allowed?