8 – How can I forget a Drupal lock?

I understand how locks work, thanks to this post. Very easy:

Function mymodule_long_operation () {
if (lock_acquire (& # 39; mymodule_long_operation & # 39;)) {
// Do the long operation here.
// ...
lock_release (? mymodule_long_operation?);
}
}

My problem is that my function can "hang" if lock_release is never hit. So, how can I get my function working again if something goes wrong?

Function mymodule_long_operation () {
if (lock_acquire (& # 39; mymodule_long_operation & # 39;)) {
// SOMETHING IS WRONG HERE!
lock_release (? mymodule_long_operation?);
}
}

Yes, I can put the entire function around a try / catch and unlock the catch. But as a developer, I often include "the ()" in my code when I'm working. So I can easily put this function in an unexecutable state.

Is there a way to invalidate the lock after a while? Or to lapse the lock as part of a guaranteed garbage collection feature?

Any ideas would be highly appreciated! Many Thanks!

** UPDATE **

Forget, I put the () in my code. What if the function expires? Then it will not reach the publication. I'm sure there are more cases that confirm this as a problem.

c ++ – Mutex Lock Benchmark

I'm trying to measure how many CPU cycles a mutex lock takes. Can you find problems in my Mutext-Lock benchmark?

#include 
#include 
#include 
#include 
#include 

std :: mutex twiceMutex;
typedef std :: int64_t i64;

i64 twice (i64x)
{
return 2 * x;
}

i64 twice M (i64 x)
{
std :: lock_guard lock (twice mutex);
return 2 * x;
}

const i64 I = 100 & 000; 000 & 000; // number of iterations

struct benchmark
{
i64 (* fn) (i64);

i64 operator () () const
{
Namespace chrono = std :: chrono;
auto startTime = chrono :: steady_clock :: now ();
i64 res = 0;
for (i64 i = 0; i <i; i ++)
{
res + = fn (i);
}
auto endTime = chrono :: steady_clock :: now ();

auto elapsed = chrono :: duration_cast(endTime - startTime) .count ();
Return expired;
}
};

int main ()
{
const double T = Benchmark {twice} (); // nanoseconds without mutex
const double TM = Benchmark {twice M} (); // Total Noseconds with Mutex

const double K = 3.7; // GHz of my CPU
const double t = T / I; // nanoseconds per iteration without mutex
const double tm = TM / I; // nanoseconds per iteration with mutex
const double m = tm - t; // Difference nanoseconds per iteration. This should be the cost of locking a mutex in nanoseconds

// print the results in cycles
std :: cout << t * K << std :: endl;
std :: cout << tm * K << std :: endl;
std :: cout << m * K << std :: endl;

{
std :: string_;
std :: getline (std :: cin, _);
}
}

I intentionally ignore thread conflicts. I am only interested in the best case costs.

Security – Remove the lock on the app

I installed a specific app two months ago. When I opened it for the first time, a pop-up opened with a suggestion to enable the screen lock pin. And I have activated the screen lock. From this point on, the PIN is always requested when opening the app. I have used the app the last two months without any problems. However, in the last few days, I can not open the same application because the PIN entry is displayed even if I entered the correct PIN. I thought it was a mistake of that particular application and reported it to them. They told me that the application did not ask for a PIN to access it. I was asked to check the access and security information on my phone, open the apps, and remove the app from the list of apps. But I can not find any attitude to check on my phone. Could someone tell you how to check the access and security information to open the apps and how to remove them?

postgresql – Does postgres use a lock better than a transaction when reading data?

I have a race condition in the code for my database and wonder how to stop it by using locks or transactions. (And which one is best)

I use an ORM to update values, but the ORM does not support locks or transactions (though it supports native queries, so I need to do some of that).

However, the code can be summarized as follows:

I want to refresh (an object) ON) the reference to Table X, from ID 1 to 2. – The Orm maintains these as simplified numerical IDs, not foreign keys.

The maximum number of references to an ID in Table X is based on the corresponding Spots column in this table.

Before the update, so first the number of free seats is checked, then some more checks are performed (calculatePrimeNumber (1000)). And finally, the field is updated.

Or real: I have a table Posts who have a reference to Events,

Whenever I want to update that event For an entry, I check how many free seats there are in the event. (based on events.spots and the number of entries containing the reference to this entry).

I'll do a few more checks. And then update it.


Now the problem starts when there are multiple requests to update the event data at the same time: It can happen that two requests are successful, even though there is only one point in the event.

What is the best way to prevent this? Using transactions or how should I lock the database?

So far, I can only find locks within a single transaction / command. However, this would not allow server-level verification, as in:

entries = rawQuery (SELECT * FROM public. "entry" WHERE id = & 39; + newEventId)
events = rawQuery (SELECT * FROM public. "event" WHERE id = & 39; + newEventId)
if (events[0].spots <entries.length) {
// end treatment, return error that no update can take place
}
// DO FURTHER TESTS
rawQuery (& nbsp; UPDATE public. "entry" SET "event" = & # 39; + newEventId + & nbsp; WHERE id = & # 39; + entry.id)

Multi-tenancy with lock tables .. Please review my approach

I'm in the process of creating a multi-tenant web app.

It may happen that multiple users update the user tables, and I need to maintain some sort of data integrity by locking and taking into account the latency on the Web compared to the contention for the records. Users must have a reasonable amount of time to be sure that the documents will not change as they make changes. 10 minutes is the time I chose.

Instead of locking records in database tables, I have a table with pseudo locks. To change a record, the user must successfully insert and manage a record in the LockTable. If the lock succeeds, the user receives the data if no error occurs and receives the message that it has been locked by another user. To maintain the lock, the user must edit the document so that the browser prompts the server to refresh the lock at least every 10 minutes. After 10 minutes another user can close the lock. When a user with a lock sends an update or other action, the server first checks to see if the lock persists

Documents are created in a classic way with a header and a detail table.

Simplified example:

For example, an order has a single OrderHeader record and multiple OrderDetails records. The user has received part of the order and would like to update the order status and received quantities.

The user sends a request for a list of orders from the server.
User selects document "27"

The browser sends a request to the server to lock and retrieve document "27".

On the server

GetOrderDocument (document and user information)
{

Insert into LockTable USER: = CurrentUser TABLE: = ORDERHEADER RECORD: = 27 LockTime: = Now

if (Insertion of lock fails) // Document is already locked
{
GetLock TABLE: = ORDERHEADER RECORD: = 27 // Reads the current lock
if (Now - LockTime> 10 min) // If the lock is older than 10 min
{
Apply the lock by changing USER: and Locktime
}
otherwise
{
Feedback to Browser "Document is locked by "
}
}
/ * User now has the lock * /
Use LockTime to select a document header and details in a Json
Give Json back
}

Changes are made to the browser user by pressing Save

browser

Save data()
{
Gather the data to be updated
Set up an Ajax call and send the update data and last LockTime received from the server
}

On the server

UpdateData (changes, OldLockTime)
{
Check DBLockTime in DB with OldLockTime
if (DBLockTime == OldLockTime)
{
Update LockTime with LockTime: = Now
}
otherwise
{
message = "You have lost the document lock. Changes not saved."
feedback
}
Update the records as needed
Return event status and new LockTime
}

rinse and repeat.
When the user finishes the document, a request to remove the LockTable record is sent to the server. If this fails, it can only take a maximum of 10 minutes.

This addresses latency issues when modifying documents over the Web and ensures that a user has adequate time to do his job. However, it ensures that the records become available when the user leaves or his Internet dies.

Now I have to throw a few things in the mix.

Sometimes, in addition to the main document, the server must examine and update another record in another table. The record is a counter record that must be read and then incremented. The server only needs this record for the time of the other document updates, but reading and updating this counter must be done atomically with and in the same transaction as the main document.

What I do in this case is

On the server

UpdateData (changes, OldLockTime)
{
Check DBLockTime in DB with OldLockTime
if (DBLockTime == OldLockTime)
{
Update LockTime with LockTime: = Now
}
otherwise
{
message = "You have lost the document lock. Changes not saved."
feedback
}
set haveLock = false
Loop 10 times
/ * Attempt to insert new lock record in Locktable * /
if (GetCounterRecordLock == false)
sleep 1 second
otherwise
{
haveLock = true
break out of the loop.
}
endless loop
if (haveLock == false)
Error updating the error message

Start the transaction

Read and update counter table.

Update the main document records as needed
undertake trans.
Delete CounterRecordLock from LockTable

Return event status and new LockTime
}

I know that blocking entries works very well. That was checked to death.

My concern is to get this second lock on the counter record.

Is the attempt to get it 10 times and wait 1 second between trials the right approach? The thoughts of others are appreciated.

Many Thanks.

Uncollected exception & # 39; PDOException & # 39; – Serialization error: 1213 Deadlock while trying to get a lock; Restart the transaction

I am getting this error when multiple users try to update records in a master table that manages the inventory, multiple processes that affect that table, and constantly make events in it (the most common being the update). Note that all events that move this table are executed with Ajax.

For example:
Case 1: The user reads some barcodes that are on the products to be packaged. Each time he reads a barcode, he goes to the table to subtract 1 from the amount of product.

Update Inventory Set Returns Inventory = Inventory + 1, Quantity Inventory = Quantity Inventory + 1 Where ProductID =?

Case 2: The user reads barcodes to store products in the inventory.

Update inventory record entriesInventory = entriesInventory + 1, QuantityInventory = Quantity Inventory + 1 where ProductID =?

Case 3: The user moves products from one place to another

Update inventory set zonabodega =? where ProductID =?

And there are a number of cases in which this table is touched throughout the working day, in which the error appears in part as a statement and in other cases not. How can I fix or avoid this error?