質問

I am using mysql, php. table user_meetup id, user_id, meetup_id with unique on (user_id, meetup_id) if my meetup is limited by places(10 places) 9 users already marked rsvp and if 2 users doing RSVP at the same time (i just have consider a case of concurrency)

A -> select count(id) from user_meetup -> result : 9 hence go ahead
B -> select count(id) from user_meetup -> result : 9 hence go ahead
A -> insert ......   
B -> insert ......

now both of them will get place and my count became 11, my solution was to add one more table user_meetup_count id, meetup_id, user_count ( count set to 0 as default value and record is created as soon as meetup is created) now if use update query like update user_meetup_count set user_count = user_count + 1 where user_count < 10 and if I write in user_meetup table based on number of rows updated by above query if it returns 0 mean its full and if it returns 1 I got the place

will this work in case 2 user try at same time ? is my approach to solve concurrency problem right ? is there a better way ? what are the tools to testing this type of situations ?

役に立ちましたか?

解決

Use lock tables before counting. And unlock it after inserting.

Or you could use GET_LOCK then RELEASE_LOCK. With this you do not need to lock all entry table.

Or explore theme about gap locking for innodb tables. With this you need to use transactions.

And you could use jMeter for testing your queries.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top