Questions tagged [locking]

Locking allows different types of resources to be used exclusively by one process at a time.

For more information see Locks in Java

8782 questions
74
votes
5 answers

Does lock() guarantee acquired in order requested?

When multiple threads request a lock on the same object, does the CLR guarantee that the locks will be acquired in the order they were requested? I wrote up a test to see if this was true, and it seems to indicate yes, but I'm not sure if this is…
Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
73
votes
3 answers

lock inside lock

I'm wondering if this construction will cause an error: lock(sync) { // something lock(sync) { //something lock(sync) { //something } } } I've run this code, and it seems fine, but maybe in some circumstances an error…
reizals
  • 1,245
  • 1
  • 14
  • 21
73
votes
6 answers

Is there a "try to lock, skip if timed out" operation in C#?

I need to try to lock on an object, and if its already locked just continue (after time out, or without it). The C# lock statement is blocking.
gil
  • 2,248
  • 6
  • 25
  • 31
73
votes
5 answers

What's the difference between PESSIMISTIC_READ and PESSIMISTIC_WRITE in JPA?

I have read the article Locking and Concurrency in Java Persistence 2.0, and run the sample application. But I still can't realize the difference between PESSIMISTIC_READ and PESSIMISTIC_WRITE. I tried to modify the code, and where the code using…
paka
  • 739
  • 1
  • 5
  • 3
72
votes
8 answers

Detecting locked tables (locked by LOCK TABLE)

Is there a way to detect locked tables in MySQL? I mean tables locked by the LOCK TABLE table WRITE/READ command. (Note that readers interested in detecting named locks acquired with GET_LOCK should read Show all current locks from get_lock…
Marcin
  • 5,469
  • 15
  • 55
  • 69
70
votes
2 answers

Using WITH NOLOCK Table Hint in Query Using View - Does it Propagate Within the View?

If a "WITH NOLOCK" query hint is used on a View in SQL Server, does it propagate that hint to the view definition itself, even if NOLOCK is NOT used for the raw tables in the View definition? The reason to need this is that sometimes the support…
Turnkey
  • 9,266
  • 3
  • 27
  • 36
70
votes
7 answers

Command-line tool for finding out who is locking a file

I would like to know who is locking a file (win32). I know about WhoLockMe, but I would like a command-line tool which does more or less the same thing. I also looked at this question, but it seems only applicable for files opened remotely.
JesperE
  • 63,317
  • 21
  • 138
  • 197
70
votes
7 answers

Show all current locks from get_lock

Is there any way to select / show all current locks that have been taken out using the GET_LOCK function? Note that GET_LOCK locks are different from table locks, like those acquired with LOCK TABLES - readers who want to know how to see those locks…
braunbaer
  • 1,113
  • 1
  • 9
  • 20
69
votes
23 answers

Simple Java name based locks?

MySQL has a handy function: SELECT GET_LOCK("SomeName") This can be used to create simple, but very specific, name-based locks for an application. However, it requires a database connection. I have many situations like: someMethod() { // do…
worpet
  • 3,788
  • 2
  • 31
  • 53
69
votes
4 answers

Destination Array not long enough?

I have a class with the following method: public List bikesCopy { get { List bs; lock (_bikes) bs = new List(_bikes); return bs; } } Which makes a copy of another list, private List
Geert
  • 795
  • 1
  • 5
  • 7
67
votes
9 answers

Why can't we lock on a value type?

I was trying to lock a Boolean variable when I encountered the following error : 'bool' is not a reference type as required by the lock statement It seems that only reference types are allowed in lock statements, but I'm not sure I understand…
Otiel
  • 18,404
  • 16
  • 78
  • 126
67
votes
1 answer

Optimistic locking in MySQL

I can't find any details on optimistic locking in MySQL. I read that starting a transaction keep updates on two entities synced, however - it doesn't stop two users updating the data at the same time causing a conflict. Apparently optimistic locking…
Green Acorn
  • 852
  • 2
  • 10
  • 14
65
votes
3 answers

SQL Server - How to lock a table until a stored procedure finishes

I want to do this: create procedure A as lock table a -- do some stuff unrelated to a to prepare to update a -- update a unlock table a return table b Is something like that possible? Ultimately I want my SQL server reporting services…
Greg
  • 45,306
  • 89
  • 231
  • 297
65
votes
5 answers

What is (are) difference between NOLOCK and UNCOMMITTED

I use SQL Server 2012. I write two queries but what is a different between NOLOCK and UnCommitted ? SELECT lastname, firstname FROM HR.Employees with (READUNCOMMITTED) SELECT lastname, firstname FROM HR.Employees with (NoLock)
Ardalan Shahgholi
  • 11,967
  • 21
  • 108
  • 144
65
votes
4 answers

To what level does MongoDB lock on writes? (or: what does it mean by "per connection"

In the mongodb documentation, it says: Beginning with version 2.2, MongoDB implements locks on a per-database basis for most read and write operations. Some global operations, typically short lived operations involving multiple databases, still…
bodega
  • 1,005
  • 1
  • 9
  • 7