Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I lock database records from Java?

Tags:

java

sql

mysql

dbf

I'm developing a database program in Java with dbf. I need to know how to lock the database records from the Java side.

Example: we have a database table cheques with 5 records in the table (record 1 through 5). There are 2 users, user-1 and user-2. user-1 accesses record 1 and user-2 tries to access record 1 at the same time. I want to lock record 1 to prevent access to that record by user-2 while user-1 is accessing it. How do I do this in Java?

like image 512
Mahesh Avatar asked Jul 02 '13 14:07

Mahesh


Video Answer


1 Answers

In case of MySQL, you can use, SELECT FOR UPDATE statement for locking the records. Lock will not be released until the transaction completes or rolls back. More on the same here.

like image 132
Darshan Mehta Avatar answered Oct 23 '22 12:10

Darshan Mehta