my dog learned polymorphism*
The moose likes JDBC and the fly likes How to implement pessimistic locking using plain JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to implement pessimistic locking using plain JDBC " Watch "How to implement pessimistic locking using plain JDBC " New topic
Author

How to implement pessimistic locking using plain JDBC

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I'am working on a typical workflow application .
Records can be added ,Updated , sanctioned, Authorized,Deleted & viewed in the system.
Business rules are :-
1)Only fresh & Sanctioned records can be modified
2)Only Sanctioned records can be Authorized
3)Only non Authorized records can be updated
4)Only non Authorized records can be Cancelled
5)Only non Authorized records can be Deleted.
Now assuming i want to Delete a non Authorized record,I populate my transaction screen in Delete mode.The transaction screen's fields are populated from the database. I DO NOT HOLD ANY LOCKS ON THE RECORD ! CAN THIS BE DONE USING JDBC ALONE / OR BY ANY MODIFICATIONS TO THE QUERY WHICH IS USED TO FETCH THE VALUES TO POPULATE THE TRANSACTION SCREEN ??
Presently what is happening is that before I Delete this record if another user
Authorises the same record , my application logic goes for a toss.
This is because at the time both the records were
selected from the search screen , they were both
fresh records & both Deletion and Authorization was possible .

How do i take care of this scenerio ?
I know that i could take care in the Delete Query by using the not in Keyword & specify the required record status there .Also a time stamp approach (Optimistic Locking approach) could be used .

But is there a cleaner way to do this ....
I repeat ..is there a way to lock a row using only JDBC.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to implement pessimistic locking using plain JDBC