I've built a page with POST method form.
The form has a list of orders and tracking number for each order.
On this page two people are working at the same time, updating tracking numbers for order.
Example: when person 1 is updating the form with tracking number for order #126792 and click Submit, the database will be update tracking number for order #126792 and leave empty field for order #127299
At the same time person 2 is updating tracking number for order #127299, the database will update tracking number for order #127299 BUT insert empty tracking number for order #126792
You can see the form below in attached image, how can this situation be solved ?
Thank you.
Your question is too broad since it really depends on how your system and your users behave. Also, without you providing any code, I can't really answer with code.
However, I can provide a kind of decision alghorithm that might help you...
Questions:
Can the same record be updated by 2 different users at the same time?
No: Proceed to 2
Yes: Solution C, D or E
Can a user update 2 orders at the same time?
No: Solution A
Yes: Proceed to 3
Are empty values meaningful? (that is, can a user update/delete a record by submitting an empty "tracking code"?)
No: Solution B
Yes: Solution C, D or E
Solutions:
A. Change the frontend so that the user can only update 1 order BOX at a time
B. Ignore empty/false/null values in the backend.
C. Lock the records being edited:
User selects the records to update and informs the backend
The backend locks the records being updated until the user updates it or the "update time" expires
D. Use a version system: make each update a new version of the records and if a conflict arises, ask the last user that tried to update to resolve before committing the data.
E. Make the records "real time" (with short polling or long polling)
NOTE: Keep in mind that this assumes users are "smart", that is, they only behave in a predictable way (that you can specify without code) and they will oblige and won't try funny stuff.
Related
I am writing a hotel booking software using PHP and MySQL. I am pretty much done with it but I ran into a race condition problem. For example there is only one standard room left and when 2 guests both select it it shows available to both of them. I tried fixing it by checking the room again when the guest clicks confirm before payment but that still has issues. I also tried making the room status to pending when whoever clicks the confirm first but I can't figure out how to change it back to available if the guest decides not to pay or just closes the browser. I searched SO for answers but I didn't really find a definitive answer. Thanks in advance
One solution is to add two columns to a table in the database. One column is the session ID or user ID or whatever of the user that is being offered the room. The second column is a timestamp indicating when that offer will expire.
Then, in your app, only show rooms that have an expired timestamp in the hold column. (Set the initial timestamp to 0 so that it starts out expired.) When a room is selected, check the column again. If there's an unexpired timestamp there, the user gets a "sorry, you were too slow" message. Otherwise, put a timestamp there for 15 minutes into the future or whatever, and proceed.
You see this on travel sites and ticket-purchasing sites a lot where it says something like "We're holding these seats for you for another 14 minutes. Please complete the transaction by then or it will be released blah blah blah."
I would also go with the pending state. You could save the state together with the session id of this user and have a cronjob that deletes all pending states that have expired session ids associated to them.
So I'm planning to create a code that will save in my database. (I have no problem in saving to my database thou.)
But I'm having a bit problem. The problem is to avoid having the same seat information displayed for the specific customer.
Something like this? (Sorry for the sample data that I will be doing.) The case is both are doing their inputs for their reservation at the same time.
[X] = taken data, [0] = blank data, [R] = customer data
1st viewing customer (having 2 data to be stored)
[X][X][X][R][R]
[0][0][0][0][0]
[0][0][0][0][0]
2nd viewing customer (having 1 data to be stored, and must be showed for this customer)
[X][X][X][X][X]
[R][0][0][0][0]
[0][0][0][0][0]
What the 2nd viewing customer actually sees. Which the customer mustn't encounter. And so on if there are other more customers are doing their inputs.
[X][X][X][R][0]
[0][0][0][0][0]
[0][0][0][0][0]
My first tactical plan is to create two database tables, one database table will serve as a dummy and the other will serve as the main database table. The logic is the dummy table will always update if there are customers making their inputs (which will have a problem by the time the customer closes the browser or didn't finish the transaction, the data stored at the dummy will still remain even after other customer accessed it again - which must not happen). And the main table will only update when the customer completed the whole transaction.
I know from the way I typed it is a bit confusing. But I really need some help in creating a logic to avoid this conflict the customer will see in my program. Or some tips if this kind of plan will not work. Like giving a note that (same sample above with 1st and 2nd customer) "This data is just a sample and sometimes will not be followed or the same with the e-mail sent to your registered e-mail addressed from this website."
Thank you in advance for your responses.
Well I don't think it's a good idea to save all clicks in final seat info table. Instead of that you can make something like temporary_seats base which will keep temporary information about reserved seats. In this table you would include information about seats that customers wishes to reserve.
Then you need to create javascript code querying API and getting all reserved (from destination table) and temporary reserved (from tmp table) and mark those seats free/occupied. You can do it constantly or check seat availability only when t
I think my current problem is describable very quick.
I have a symfony2 project with doctrine accessing the mysql database.
Now multiple (many) users can visit this page at the same time.
Now, my problem is the following:
Users can click a button to mark himself as a participant of an entry.
An entry can have a maximum of allowed participants.
E.g.:
ENTRY A, max. users = 3 <----- USER A, B, C and D viewing ENTRY A.
Now, A marks himself as an participant by clicking an Ajax Button.
B do the same (not at the same time -> no problem).
Now C and D fight for the last free slot in ENTRY A.
Both click on the button at the same time.
It can happen now that C and D both can join this entry, even if the max. users of 3 is exeeded.
If an user clicks on the Ajax Button, an ajax call gets send.
On the server side, in the ajax handler, my symfony2 code will use doctrine to access the db.
It simply requests the entry from database and checks if the maximum of users already reached.
If not, the user gets saved/marked as an participant of the entry.
But that's not allowed. I want to avoid that 2 users can click on the button at the same time to join the entry.
In this state, max. users in the entry is defined to 3 and the amount of participants for this entry is 4. That's not the goal of this function.
Can you explain me how to fix this?
Do i need table locking or something else?
If it is not exactly clear what the problem is, then just ask.
I look forward to answers and i am grateful for each hint.
Best
titan
Seeing as you're using MySQL you could use
http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock
http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_release-lock
How you code it is up to you, but it can be used to mitigate concurrency issues like the one you have.
Have a look at Transaction and concurency on doctrine doc http://doctrine-orm.readthedocs.org/en/latest/reference/transactions-and-concurrency.html
I have a script that is written in PHP. It uses MySQL database to store records.
Basically, I have team of users that are making random calls to a different business. I want to add list of phone number in a queue "pool table". The system will need to assign the new call to the user. Now If a user is already working on a phone call I don't want another user to start calling the same number. I need a solution to prevent 2 people having the same record assigned to them. So if phone number 000-000-0000 is assigned to the user X the same record will be skipped and the next one in line get assigned to the next available user.
This table will be accessed a lot so I need a good solution that will prevent 2 people from working on the same record and also not cause system issues.
One way I can think of but looking for a better solution is
open transaction
select a call where record status is available
update that call by changing the status from records available to record pending.
commit transaction.
If the use completed the call then updated with a status of completed otherwise make the record available again.
what are other solution available for me?
Thanks
Without a little more information about the workflow, it's hard to know what to suggest, but it sounds like users are interacting with the application somehow while they are taking calls...true??
If so, you must have some way for the user to alert the system they are ready for a call.
ie...
I just started my shift... Deal me a number.
Or...
Submit notes from last call... click submit and Deal me another number.
In this scenario, it seems like it should pretty easy to just let the users "request" the next number. You could probably just insert the users id on that record so it shows in their queue.
I am making a doctor appointment slot booking mechanism,where in doctor appointment slots will be divided into 30 mins slot each...i have achieved all the working code.. 1 problem i am facing is that..this booking is made at 2 places i.e 2 receptions..so when 1 selects a slot(radio button) not yet confirmed and saved in DB.other reception must not be able to select .how do i do it.any help on this...how do i go abt it.
This is a case of accessing "shared data". You'll need thread to make sure that only one thread has access to the data at a time to ensure it's integrity. The following might provide some ideas
http://www.alternateinterior.com/2007/05/communicating-with-threads-in-php.html
I would use some AJAX/AJAJ functions to periodically refresh data about free slots, or I would do that much more simple - when saving the appointment, just check it, if the slot will be taken, your app redirects user back to form to choose another slot.
In essence,
I will just ask for the slot timings upfront and the remaining details later.
In case the slot is available, it sends a request to the server to lock it, so that the other client cannot use it.
In case, it is not available, it will receive a small notification that this slot is unavailable, click to see available slots.
I would go with AJAX (if this is a web app). This is a distributed systems problem which resembles Blue Army - White Army problem.
Add a "Locked By" field to the table.
When booking a slot, do something like:
UPDATE tablename
SET LockedBy = userid, ...
WHERE LockedBy IS NULL
After the update, you can select to see if LockedBy is set to your userid.
If not, then someone else must have beaten you to the punch, and you need to tell the user to pick a different slot.