race-condition

Race conditions in django

扶醉桌前 提交于 2019-11-26 18:54:01
问题 Here is a simple example of a django view with a potential race condition: # myapp/views.py from django.contrib.auth.models import User from my_libs import calculate_points def add_points(request): user = request.user user.points += calculate_points(user) user.save() The race condition should be fairly obvious: A user can make this request twice, and the application could potentially execute user = request.user simultaneously, causing one of the requests to override the other. Suppose the

MySQL INSERT IF (custom if statements)

做~自己de王妃 提交于 2019-11-26 18:52:05
First, here's the concise summary of the question: Is it possible to run an INSERT statement conditionally? Something akin to this: IF(expression) INSERT... Now, I know I can do this with a stored procedure. My question is: can I do this in my query? Now, why would I want to do that? Let's assume we have the following 2 tables: products: id, qty_on_hand orders: id, product_id, qty Now, let's say an order for 20 Voodoo Dolls (product id 2) comes in. We first check if there's enough Quantity On Hand: SELECT IF( ( SELECT SUM(qty) FROM orders WHERE product_id = 2 ) + 20 <= ( SELECT qty_on_hand

Can we have race conditions in a single-thread program?

杀马特。学长 韩版系。学妹 提交于 2019-11-26 15:46:47
问题 You can find on here a very good explanation about what is a race condition. I have seen recently many people making confusing statements about race conditions and threads. I have learned that race conditions could only occur between threads. But I saw code that looked like race conditions, in event and asynchronous based languages, even if the program was single thread, like in Node.js, in GTK+, etc. Can we have a race condition in a single thread program? 回答1: All examples are in a

Hidden threads in Javascript/Node that never execute user code: is it possible, and if so could it lead to an arcane possibility for a race condition?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 14:47:13
问题 See bottom of question for an update, based on comments/answers: This question is really about the possibility of hidden threads that do not execute callbacks. I have a question about a potential arcane scenario involving the Node Request module in which: A complete HTTP request is constructed and executed over the network (taking however many ms or even seconds) ... before a single function is executed at runtime on the local machine (typically in the nanoseconds?) - see below for details I

Do database transactions prevent race conditions?

天涯浪子 提交于 2019-11-26 12:22:26
问题 It\'s not entirely clear to me what transactions in database systems do. I know they can be used to rollback a list of updates completely (e.g. deduct money on one account and add it to another), but is that all they do? Specifically, can they be used to prevent race conditions? For example: // Java/JPA example em.getTransaction().begin(); User u = em.find(User.class, 123); u.credits += 10; em.persist(u); // Note added in 2016: this line is actually not needed em.getTransaction().commit(); (I

Why would try/finally rather than a “using” statement help avoid a race condition?

ぐ巨炮叔叔 提交于 2019-11-26 09:46:24
问题 This question relates to a comment in another posting here: Cancelling an Entity Framework Query I will reproduce the code example from there for clarity: var thread = new Thread((param) => { var currentString = param as string; if (currentString == null) { // TODO OMG exception throw new Exception(); } AdventureWorks2008R2Entities entities = null; try // Don\'t use using because it can cause race condition { entities = new AdventureWorks2008R2Entities(); ObjectQuery<Person> query = entities

How to make sure there is no race condition in MySQL database when incrementing a field?

我是研究僧i 提交于 2019-11-26 08:07:40
问题 How to prevent a race condition in MySQL database when two connections want to update the same record? For example, connection 1 wants to increase \"tries\" counter. And the second connection wants to do the same. Both connections SELECT the \"tries\" count, increase the value and both UPDATE \"tries\" with the increased value. Suddenly \"tries\" is only \"tries+1\" instead of being \"tries+2\", because both connections got the same \"tries\" and incremented it by one. How to solve this

MySQL INSERT IF (custom if statements)

偶尔善良 提交于 2019-11-26 06:37:56
问题 First, here\'s the concise summary of the question: Is it possible to run an INSERT statement conditionally? Something akin to this: IF(expression) INSERT... Now, I know I can do this with a stored procedure. My question is: can I do this in my query? Now, why would I want to do that? Let\'s assume we have the following 2 tables: products: id, qty_on_hand orders: id, product_id, qty Now, let\'s say an order for 20 Voodoo Dolls (product id 2) comes in. We first check if there\'s enough

Atomic UPDATE .. SELECT in Postgres

扶醉桌前 提交于 2019-11-26 04:39:33
问题 I\'m building a queuing mechanism of sorts. There are rows of data that need processing, and a status flag. I\'m using an update .. returning clause to manage it: UPDATE stuff SET computed = \'working\' WHERE id = (SELECT id from STUFF WHERE computed IS NULL LIMIT 1) RETURNING * Is the nested select part the same lock as the update, or do I have a race condition here? If so, does the inner select need to be a select for update ? 回答1: While Erwin's suggestion is possibly the simplest way to

SQL Server Process Queue Race Condition

…衆ロ難τιáo~ 提交于 2019-11-25 23:37:03
问题 I have an order queue that is accessed by multiple order processors through a stored procedure. Each processor passes in a unique ID which is used to lock the next 20 orders for its own use. The stored procedure then returns these records to the order processor to be acted upon. There are cases where multiple processors are able to retrieve the same \'OrderTable\' record at which point they try to simultaneously operate on it. This ultimately results in errors being thrown later in the