PHP/MySQL Concurrent use

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chathura86
    New Member
    • May 2007
    • 227

    PHP/MySQL Concurrent use

    Hi,

    i want to block others from opening a record for updating if anyone else is already using that record for updating. one record is a one or more rows in MySQL. it's like locking those rows until he finish updating it.

    Thank you

    Chathura Bamunusinghe
  • Ranjan kumar Barik
    New Member
    • Aug 2007
    • 95

    #2
    Add a field in database with datatype boolean (make the default value 0 i.e. unlocked. U may take 1 also). when some one wants to update particular record just run a query to make field values 1(i.e. locked) for all records having the particular Primary Key. Then just add a little more into your update query by checking that if the rows are locked or not. If not then you can update, else alert a message to the user. After updation run another query to make those fields unlock. So that some one else could be able to update after that.

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      Not sure, but one way to do it would be to have a variable in the record/table which is either a 1 or 0. So let's call the variable $checked_out.

      Your table will have all your normal columns but also one called checked_out. When you open the page to change the record/table once it has accessed it, it will check to see if checked_out is set to 1. If so it will not display the information but just a note saying it is checked out. If it returns 0, then the user may retrieve the information and once the page has loaded it will change checked_out to 1 so no one else may log into it.

      The problem with this method is if the user does not sign out (which will set the checked_out variable to 0), but instead closes the browser. This means the record/table will permanently be locked out.

      Two solutions:
      1. Record the time of last user activity in the record/table as well, and if it has exceeded 20mins and someone tries to login, allow the second person to retrieve the record/table and also do not allow the previous user to submit the data.
      2. Record the user who signed out the record/table and display it as the error: "You cannot edit this Record because TheServant has it signed out. Contact administration if this has been signed out for over one hour." Then you can either contact TheServant to tell him to sign out (have an automatic emailing system) or do it yourself.

      Sorry, it's the end of a long day, that's all I could come up with.

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        Originally posted by Ranjan kumar Barik
        Add a field in database with datatype boolean (make the default value 0 i.e. unlocked. U may take 1 also). when some one wants to update particular record just run a query to make field values 1(i.e. locked) for all records having the particular Primary Key. Then just add a little more into your update query by checking that if the rows are locked or not. If not then you can update, else alert a message to the user. After updation run another query to make those fields unlock. So that some one else could be able to update after that.
        lol, beat me to it :P

        Comment

        • chathura86
          New Member
          • May 2007
          • 227

          #5
          Thanks for the replies.
          That is a good solution.
          Please let me know if found any other answer.

          Thanks again.

          Comment

          • Ranjan kumar Barik
            New Member
            • Aug 2007
            • 95

            #6
            Originally posted by TheServant
            lol, beat me to it :P
            Take one field which records the login time, update it in every page and every time the user comes to the page.
            And let it allow you if it has passed your desired time elapsed with your login time.
            hope the mystery solved.

            :))

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              Originally posted by Ranjan kumar Barik
              Take one field which records the login time, update it in every page and every time the user comes to the page.
              And let it allow you if it has passed your desired time elapsed with your login time.
              hope the mystery solved.

              :))
              Also keep a record of who has it checked out and check if the user accessing it is the same as the user who has it checked out every page. That way if someone leaves their computer (without logging out or closing their browser) and comes back, they will not be able to access it if someone else has taken it out. I hope that made sense.

              Comment

              • Ranjan kumar Barik
                New Member
                • Aug 2007
                • 95

                #8
                Originally posted by TheServant
                Also keep a record of who has it checked out and check if the user accessing it is the same as the user who has it checked out every page. That way if someone leaves their computer (without logging out or closing their browser) and comes back, they will not be able to access it if someone else has taken it out. I hope that made sense.
                It,s a Big Idea
                Thanks

                :))

                Comment

                Working...