Swapnil Sonawane
Swapnil Sonawane

Reputation: 1455

mysql row locking and realize lock when my code want

I am using mysql in my java base web app. I want to lock particular row so that user can read row but can not update and delete. Row is locked still my code want. that means my code itself unlock the row.

Is it possible in mysql to lock particular row in such manner.

Upvotes: 1

Views: 1654

Answers (1)

Santosh
Santosh

Reputation: 17903

I am not sure what is your use case and why do you want to EXPLICITLY lock a row programmatically. Generally this happens under the hood when you use JDBC/Transaction APIs.

Quoting @Keith Randall from the link shared above by @ Mithun Sasidharan,

"If you have an index on tblAreas.AreaID, then any transaction that includes WHERE tblAreas.AreaID in (...) will lock the index for those entries"

This will generally use and Optimistic Locking. What you are interested seems is Pessimistic locking.

In MySQL, one way to do is to use SELECT FOR UPDATE statement which will lock the selected rows for you. Please refer to the respective documentation for the usage.

Upvotes: 1

Related Questions