0

i'm creating a browser rpg game where the player has an inventory and a warehouse. Imagine that at some point, a user wants to move an item from the inventory to the warehouse. And now the security has to be tight.

I suppose that this has to be a transaction. Now, you see the possibilities for a race condition here. Move from inv->warehouse at the same time as move from warehouse->inv could mean that an item is duplicated.

So, how can i handle that to make sure nothing like that happens ?

EDIT -- RACE CONDITION FOR THAT EXAMPLE

Moving from inv to warehouse is a function where the item from inv is first added to warehouse and then deleted from the inventory. Moving from warehouse to inv is the same idea.

Now, think of 2 simultaneous moves. An inv moving function adds item to warehouse. At the very same time, the opposite begins. A warehouse moves the exact item to inventory. It will find the item to move, since it was just moved. The inventory now deletes item from inventory. The warehouse deletes item from warehouse.

Result : Item is lost

Spyros
  • 46,820
  • 25
  • 86
  • 129
  • You have not provided enough details about your implementation for anyone to have a clue what you're getting at, or why there might be potential for a race condition. The simplest schema I can think of that you -could- be talking about is that there is a many-to-many from places to items, in which case, moving an item would simply mean changing its place_id. This would be inherently atomic, without any risk of duplication or need for any explicit transaction. – Steve Jorgensen Apr 04 '11 at 08:25
  • really :O ? i think my example clearly illustrates why there can be a race condition. It's the same as in money transaction. – Spyros Apr 04 '11 at 08:28
  • 3
    No -- it doesn't. As I said, in the simplest implementation that I can think of for the relationship between items and places, I don't see any way that there could be a race condition. If an item points to a place, then it can only be in one place at a time. Changing the pointer is changing the place, and it's inherently atomic. It's one change to one field value in one record, so how could there be a race? Now, if you have some more complex architecture that you have not described... – Steve Jorgensen Apr 04 '11 at 08:51
  • Edited my answer to describe how race conditions work for that case. – Spyros Apr 04 '11 at 19:36
  • what are the model associations for your items and warehouse? i would assume a many to many and in this case, item and warehouse id should both be unique. thus eiminating any sort of race condition problem you have...rght? – corroded Apr 04 '11 at 19:51
  • But the associations do not have anything to do with the race condition. This happens because of requests happening at the same time. – Spyros Apr 04 '11 at 20:04
  • Hi @SpyrosP, I would assume, as do the others that an item has a relation to a warehouse. Then an item could only have one relation to a warehouse. If on the other hand, the item is some string inside a record, then it could be possible. So please explain your model, or show the relevant code. – nathanvda Apr 10 '11 at 11:03

2 Answers2

1

I actually found how to prevent the race condition in the Agile Rails book. This is how it would be done with a transaction for money :

Account.transaction do
  account1.deposit(100)
  account2.withdraw(100)
end
Spyros
  • 46,820
  • 25
  • 86
  • 129
0

Spyros,

Putting this in a transaction will solve some problems, but not necessarily all. See this question:

Do database transactions prevent race conditions?

Community
  • 1
  • 1
jim
  • 1,025
  • 12
  • 17