Forums Register Login

HELP - JDBC Connection & locking

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
"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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
yeah, but ... what would PIE do? Especially concerning this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4329 times.
Similar Threads
[URLYBird] Locking Test Cases
GUI display & booking questions
Does the lock associate session ID or customer ID?
Row locking using oracle database
Will this locking mechanism be allowed?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:12:46.