locking

database record locking

£可爱£侵袭症+ 提交于 2020-01-14 10:46:20
问题 I have a server application, and a database. Multiple instances of the server can run at the same time, but all data comes from the same database (on some servers it is postgresql, in other cases ms sql server). In my application, there is a process that is performed which can take hours. I need to ensure that this process is only executed one at a time. If one server is processing, no other server instance can process until the first one has completed. The process depends on one table (let's

Postgres pg_try_advisory_lock blocks all records

回眸只為那壹抹淺笑 提交于 2020-01-14 10:43:23
问题 I'm using pg_try_advisory_lock() in Postgres. Next two queries lock more than one records in table1 : 1) SELECT a.id FROM table1 a JOIN table2 b ON a.table1_id = b.id WHERE table2.id = 1 AND pg_try_advisory_lock('table1'::regclass::integer, a.id) LIMIT 1; but SELECT a.id FROM table1 a JOIN table2 b ON a.table1_id = b.id WHERE table2.id = 1 returns one record. 2) SELECT a.id FROM table1 a JOIN table2 b ON a.table1_id = b.id JOIN table3 c ON b.table2_id = c.id WHERE table3.id = 1 AND pg_try

How do I conditionally lock in Java?

元气小坏坏 提交于 2020-01-14 05:03:14
问题 I have been making use of Java's synchronized blocks to make parts of my code thread safe. I am porting a data structure to java that can usually use synchronized blocks, but I don't always know how to use them in a typical Java way. Here is an example of one scenario: myMethod (Bool useLock) { if (useLock) { //locks the following section of code until unlocked. lockObject.lock(); } //do more stuff.... if (useLock) { //unlocks exclusive control of code. lockObject.unlock(); } } How do I do an

PHP, mysqli, and table locks?

那年仲夏 提交于 2020-01-13 20:41:17
问题 I have a database table where I need to pull a row, test user input for a match, then update the row to identify the user that made the match. Should a race condition occur, I need to ensure that the first user's update is not overwritten by another user. To accomplish this I intend to: 1. Read row 2. Lock table 3. Read row again and compare to original row 4. If rows match update, otherwise do nothing (another user has already updated the row) Based on information I found on Google, I

why doesn't this lock statement work?

余生颓废 提交于 2020-01-13 11:10:49
问题 class Program { static object test = new object(); static void Main(string[] args) { new Program().test2(); Console.ReadKey(); } public void test1() { lock (test) { Console.WriteLine("test1"); } } public void test2() { lock (test) { test1(); Console.WriteLine("test2"); } } } Is the code above supposed to first finish statements in lock statement of test2() then go to the test1()? (i.e. Doesn't the output supposed to be like this? : test2 test1 ) 回答1: A monitor is re-entrant on the same thread

Is the volatile keyword required for fields accessed via a ReentrantLock?

为君一笑 提交于 2020-01-12 02:29:29
问题 My question refers to whether or not the use of a ReentrantLock guarantees visibility of a field in the same respect that the synchronized keyword provides. For example, in the following class A , the field sharedData does not need to be declared volatile as the synchronized keyword is used. class A { private double sharedData; public synchronized void method() { double temp = sharedData; temp *= 2.5; sharedData = temp + 1; } } For next example using a ReentrantLock however, is the volatile

iphone auto lock value?

只谈情不闲聊 提交于 2020-01-11 10:47:13
问题 Isn't possible to retrieve auto lock value? Or anyone know how to modify the auto lock time like those existing alarm application? Thanks. 回答1: You can do it. you need to implement the following methods - (void)applicationWillResignActive:(UIApplication *)application This gets called when the phone is locked. In this method I would store the time and then impliment this method on the app delegate. - (void)applicationDidBecomeActive:(UIApplication *)application This function will be called

Thread safe usage of lock helpers (concerning memory barriers)

折月煮酒 提交于 2020-01-11 05:32:07
问题 By lock helpers I am referring to disposable objects with which locking can be implemented via using statements. For example, consider a typical usage of the SyncLock class from Jon Skeet's MiscUtil: public class Example { private readonly SyncLock _padlock; public Example() { _padlock = new SyncLock(); } public void ConcurrentMethod() { using (_padlock.Lock()) { // Now own the padlock - do concurrent stuff } } } Now, consider the following usage: var example = new Example(); new Thread

SQL Server, insert one row locks whole table

喜你入骨 提交于 2020-01-11 00:43:08
问题 We have an issue with some deadlock and I posted this question. With some help and a lot of searching myself I believe I figured out what is going on. In order to solve the deadlocks without controlling lock escalation I need to understand why sql server locks the whole table on inserting one row. Here is my insert statement (with renamed variables): DECLARE @Type1 INT = 11, @Type2 INT = NULL, @Value1 VARCHAR(20) = '0', @Value2 VARCHAR(20) = '0', @Value3 VARCHAR(20) = '0', @Value4 VARCHAR(20)

import inside of a Python thread

十年热恋 提交于 2020-01-10 22:27:47
问题 I have some functions that interactively load python modules using __import__ I recently stumbled upon some article about an "import lock" in Python, that is, a lock specifically for imports (not just the GIL). But the article was old so maybe that's not true anymore. This makes me wonder about the practice of importing in a thread. Are import / __import__ thread safe? Can they create dead locks? Can they cause performance issues in a threaded application? EDIT 12 Sept 2012 Thanks for the