locking

Does PESSIMISTIC_WRITE lock the whole table?

一个人想着一个人 提交于 2019-12-04 04:20:28
Just to be sure that I correctly understand how things work. If I do em.lock(employee, LockModeType.PESSIMISTIC_WRITE); - will it block only this entity ( employee ) or the whole table Employees ? If it matters, I am talking about PostgreSQL . Anton Malyshev It should block only the entity. PostgreSQL hibernate dialect adds for update in case of write locks: https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQL81Dialect.java#L549 (newer versions just use the same implementation) for update is treated row-wise by PostgreSQL: https:/

MySQL from the command line - can I practically use LOCKs?

旧城冷巷雨未停 提交于 2019-12-04 04:00:01
I'm doing a bash script that interacts with a MySQL datatabase using the mysql command line programme. I want to use table locks in my SQL. Can I do this? mysql -e "LOCK TABLES mytable" # do some bash stuff mysql -u "UNLOCK TABLES" The reason I ask, is because table locks are only kept for the session, so wouldn't the lock be released as soon as that mysql programme finishes? [EDIT] nos had the basic idea -- only run "mysql" once, and the solution nos provided should work, but it left the FIFO on disk. nos was also correct that I screwed up: a simple " echo X >FIFO " will close the FIFO; I

Entering in block with an Intrinsic Lock

谁都会走 提交于 2019-12-04 03:54:41
问题 I don't see how the following code produces output that appears to contravene the definition of an object lock. Surely only one thread should be allowed to print the "acquired lock" message yet they both do? class InterruptThreadGroup { public static void main(String[] args) { Object lock = new Object(); MyThread mt1 = new MyThread(lock); MyThread mt2 = new MyThread(lock); mt1.setName("A"); mt1.start(); mt2.setName("B"); mt2.start(); try { Thread.sleep(2000); } catch (InterruptedException e)

How to deal with locks (JPA)?

北城余情 提交于 2019-12-04 03:45:04
According to the Java Persistent/Locking wikibooks *, the best way to deal with locks is to report the Optimistic Lock Error/Exception to the user. The problem is that it's not scalable. Suppose that I have many users who are likely to cause a lock with the same action. The user does not care about the lock error message. In a nutshell : The best way is to disable all locks ? The best way is to report to the user the error lock message ? But the user must retry his action until it will work ! The best way is to retry the transaction until there's no lock ? * Handling optimistic lock exceptions

C# lock(mylocker) not work

折月煮酒 提交于 2019-12-04 03:39:57
I have many web service call (asychronous), in callback, I will plot result to Excel. I want to synchronize the plot method. So I use the following, however, from I track down in Visual Studio, every time, lock(locker) is successful, and there are many threads running clearcommentIfany, plot. I can't figure out why this is not working as expected! Thanks private readonly object locker = new object(); void ProcessPlot() { lock (locker) { Debug.WriteLine("currentThreadID: " + Thread.CurrentThread.ManagedThreadId); //Helper.Dispatcher.Invoke(new AddClearCommentDelegate(ClearCommentIfAny));

SQLServer lock table during stored procedure

雨燕双飞 提交于 2019-12-04 03:28:20
I've got a table where I need to auto-assign an ID 99% of the time (the other 1% rules out using an identity column it seems). So I've got a stored procedure to get next ID along the following lines: select @nextid = lastid+1 from last_auto_id check next available id in the table... update last_auto_id set lastid = @nextid Where the check has to check if users have manually used the IDs and find the next unused ID. It works fine when I call it serially, returning 1, 2, 3 ... What I need to do is provide some locking where multiple processes call this at the same time. Ideally, I just need it

'Deadlock' with only one locked object?

落爺英雄遲暮 提交于 2019-12-04 03:17:01
问题 I am having a problem with multi threading in C#. I use an event to update a label in a form from another thread, for which I need to use the Invoke() command of course. That part is also working fine. However, the user can close the form and here the program can crash if the event is sent at an unfortunate time. So, I thought I would simply override the Dispose() method of the form, set a boolean to true within locked code, and also check that boolean and invoke the event in locked code.

What's blocking “Select top 1 * from TableName with (nolock)” from returning a result?

倾然丶 夕夏残阳落幕 提交于 2019-12-04 03:13:28
I'm currently running the following statement select * into adhoc..san_savedi from dps_san..savedi_record It's taking a painfully long time and I'd like to see how far along it is so I ran this: select count(*) from adhoc..san_savedi with (nolock) That didn't return anything in a timely manner so for the heck of it I did this: select top 1 * from adhoc..san_savedi with (nolock) Even that seems to run indefinitely. I could understand if there are millions of records that the count(*) could take a long time, but I don't understand why selecting the top 1 record wouldn't come back pretty much

Locking on field or local variable?

萝らか妹 提交于 2019-12-04 02:59:46
问题 After I read this question with an answer from Marc.... I sometimes see people locking on a local variable. Is this code broken? public void Do() { object o = new Object(); lock (o) { ... } } I believe object o = new Object(); should be outside the method as a Field . Since each thread is getting a new instance of o , there will be multiple locks. What am I missing here? Shouldn't it lock on fields in this specific case? 回答1: I believe object o = new Object(); should be outside the method as

Locking mechanisms for shared-memory consistency

怎甘沉沦 提交于 2019-12-04 02:59:33
I'm developing a mechanism for interchanging data between two or more processes using shared memory on linux. The problem is some level of concurrency control is required to maintain data integrity on the shared memory itself, and as I'm specting that sometime or another my process could be killed/crash, common lock mechanisms dont' work because they could left the memory in a "locked" state and right after dying, making other processes hung waiting for the lock to be released. So, doing some research I've found that System V semaphores have a flag called SEM_UNDO wich can revert the lock