locking

MySQL gap locking

旧巷老猫 提交于 2021-02-05 11:57:17
问题 I have a table called tree_nodes , in there I store a ... tree representation of my nodes. My topic tree is created in the context of a holder. So, there is a column called holder_id . I have a transactional method that is doing several reads from this and other tables in order to determine what nodes should be added/removed from the tree. My application is clustered and is highly concurrent, so, the in-app code mutexes not gonna work. My relational DB is MySQL. Here is what I wanted to do, I

Locking and concurrency with MySQL

扶醉桌前 提交于 2021-02-05 07:27:25
问题 I'm currently using Mysql with InnoDB storage engine for all tables. So, I'm wondering if this is a real problem and if there's a solution for it. For example, I will charge a user using a database transaction: 1. check his balance 2. subtract his balance 3. credit this balance somewhere 4. commit What would happen if an update happens just after #1 and before 2 & 3. If the user withdraws or purchases something else that results his balance to be zero. This would have led to to lose that

PostgreSQL: How to select and update from a table as if it were a queue

假如想象 提交于 2021-01-29 20:51:37
问题 I'd like several processes to pull from a table as if it were a queue. The requirements: Order of the items is important, and must be specified in the query Each process (of which there are an unknown amount) can get chunks of N items at a time Each item must only be processed once I don't want to lock the table (for performance reasons) I have a working solution, but would like some other opinions. First attempt: UPDATE foo SET should_select=0 FROM (SELECT * FROM foo WHERE should_select=1

Java- Allow one thread to update a value, others to wait and skip critical section

不羁岁月 提交于 2021-01-29 18:01:21
问题 Hi I have a situation in which I have to allow only one thread to say update a variable. There is a trigger, which might invoke multiple threads to update this variable, however the update should happen only once by the first thread whichever arrives at the critical section. Ideally the flow should be like follows: Thread-1; Thread-2 and Thread-3 are invoked to update a variable in critical section guarded by a lock or a mutex Critical section using this guard allows only one thread to enter,

C# How To Achieve Monitor.Enter/Exit With Task Based Async

限于喜欢 提交于 2021-01-29 06:38:37
问题 This is my code that works. I wouldn't have to do this if it weren't task based async, but using Monitor.Enter/Exit results in this problem Object synchronization method was called from an unsynchronized block of code. Exception on Mutex.Release() People have mentioned using AutoResetEvent, and SemaphoreSlim, but I'm not quite sure which pattern fits. private bool fakelock; internal async Task<byte[][]> ExchangeCore(byte[][] apdus) { if (apdus == null || apdus.Length == 0) return null; List

C# How To Achieve Monitor.Enter/Exit With Task Based Async

烂漫一生 提交于 2021-01-29 06:22:12
问题 This is my code that works. I wouldn't have to do this if it weren't task based async, but using Monitor.Enter/Exit results in this problem Object synchronization method was called from an unsynchronized block of code. Exception on Mutex.Release() People have mentioned using AutoResetEvent, and SemaphoreSlim, but I'm not quite sure which pattern fits. private bool fakelock; internal async Task<byte[][]> ExchangeCore(byte[][] apdus) { if (apdus == null || apdus.Length == 0) return null; List

LockService: How do I lock a Google sheet from within a script?

孤者浪人 提交于 2021-01-28 18:42:51
问题 I'm using a Google spreadsheet to store contact info I collect from a website (GDPR compliant, don't worry) and I synchronize this sheet with Mailchimp using a custom App script, which runs in the background. More in detail, I'm using a temporary sheet as a buffer which is filled with the data users send via the site's forms (I'm using Contact Form 7 Google Sheets Connector). My script runs each time the event INSERT_ROW is triggered in the "buffer" sheet. It analyzes the data, stores them in

Implementing a Starve method (“Unrelease”/“Hold”) for SemaphoreSlim

瘦欲@ 提交于 2021-01-28 18:41:49
问题 I'm using a SemaphoreSlim with a FIFO behaviour and now I want to add to it a Starve(int amount) method to remove threads from the pool, sort of the opposite to Release() . If there are any running tasks, they will of course continue until they are done, since for the moment the semaphore is not keeping track of what is actually running and "owes" the semaphore a release call. The reason is that the user will dynamically control how many processes are allowed at any time for a given semaphore

Strange behavior of StampedLock with Thread class

泄露秘密 提交于 2021-01-28 18:16:13
问题 I'm running this code in IntellijIDEA Community on Windows import static java.lang.Thread.sleep; public class Main { public static void main(String[] args) { StampedLock lock = new StampedLock(); Thread th = new Thread(() -> { long stamp = lock.tryOptimisticRead(); try { System.out.println("Optimistic Lock Valid: " + lock.validate(stamp)); sleep(1); System.out.println("Optimistic Lock Valid: " + lock.validate(stamp)); sleep(2); System.out.println("Optimistic Lock Valid: " + lock.validate

Is it possible that a file lock never gets released?

流过昼夜 提交于 2021-01-28 12:11:14
问题 Given the following file lock request: FileLock lock = null; try { lock = randomAccessFile.getChannel().lock(0, Long.MAX_VALUE, mode.shared); // work with file } finally { if (lock != null) { lock.release(); } } The targetted OS being MS Windows, is there any chance the finally block won't be ever executed, and thus the lock never released? For example, what if the JVM crashes? How to handle such a no-owner-lock? 回答1: When the process exits, any OS automatically releases all resources