5, September 2008

Simultaneous access needs database locking solution - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  Access_user Class (Moderator: Olaf)
Topic: Simultaneous access needs database locking solution
« previous next »
Pages: [1] Print

Author Topic: Simultaneous access needs database locking solution  (Read 1201 times)
Metal slug addict
*
Posts: 19
126 credits
Members referred : 0


« on: Apr 17, 2007, 09:09:13 PM »

Hi,

In the header of session_handler.php you say...
/* This class code is based on the tutorial of Matt Wade
from http://www.zend.com/zend/spotlight/code-gallery-wade8.php Visit through proxy */

Looking at that page there was a very active discussion about a possible locking problem
http://www.zend.com/zend/spotlight/code-gallery-wade8.php?article=code-gallery-wade8&kind=sl&id=2752&open=1&anc=0&view=1#notes Visit through proxy

The question that prompted the discussion was along the lines of
"The standard php sessions seem to use file locks to
prevent concurrent usage, but there is no
equivalent here (when a database is used to handle sessions)."

The conclusion seemed to be that the code did need improving. One suggested solution is here http://mgrier.com/code/sess.php.html Visit through proxy

Have you any comments on this? I am wondering whether I need to implement the suggested changes.

Cheers

Mike
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6309
38674 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Apr 17, 2007, 10:09:56 PM »

Thanks Mike for sharing this, since the session handler was advised by some PHP official I didn't test on this kind of problems.

I will check this thread and also the modification and let you know what there has to be done...


Last blog : Is your website is down? Know before your visitors do!
OMG!I am geek
**
Posts: 57
394 credits
Members referred : 0


« Reply #2 on: May 23, 2007, 01:28:03 PM »

What do you think about the posted solution for the database locking problem?
Metal slug addict
*
Posts: 19
126 credits
Members referred : 0


« Reply #3 on: May 23, 2007, 02:40:54 PM »

After looking at it in more detail it seems the problem only occurs if there are 2 overlapping accesses with the same session id

This usually doesn't happen. It does happen if a user opens more than one browser window (At least in Firefox) and browses different parts of the same website in each window. Or if the site is designed to use Frames.

Personally I find the solutions  too complicated to risk incase they introduce other problems. 

It might be worth giving some thought to what would be the worst that could happen in the Access_User class by using it in 2 simultaneaous windows. I have tried opening 2 windows and have seen in the database that they are sharing the same session row. When I log out of one and then try to do something in the other it prompts me to log in again. So the session has been corrupted but in a safe way. There might be other situations though that aren't so good?

Mike

OMG!I am geek
**
Posts: 57
394 credits
Members referred : 0


« Reply #4 on: May 23, 2007, 02:52:45 PM »

I assumed the solution could protect a usual table locking problem. I mean if two users tries to read or write in the same database row. So you think this is not in that case?
Metal slug addict
*
Posts: 19
126 credits
Members referred : 0


« Reply #5 on: May 23, 2007, 02:58:33 PM »

No that isn't a problem. Because if you have 2 users then they will have different session ids so they will be using different rows.

Unless one is a hacker of course  Smiley
OMG!I am geek
**
Posts: 57
394 credits
Members referred : 0


« Reply #6 on: May 23, 2007, 03:15:11 PM »

In an other case it could be a problem I think. For example the admin and a user try to insert something in users_profile table and there is no locking featcher, which data will be insert and shown at next?

I assume that problem could be solved by only allow only one user to edit/view the same row by checking if this row has an session for a tempoary insert which should be deleted afted the insert in done. What do you think of it?
« Last Edit: May 23, 2007, 03:22:27 PM by tomz »
Metal slug addict
*
Posts: 19
126 credits
Members referred : 0


« Reply #7 on: May 23, 2007, 03:35:58 PM »

Quote
In an other case it could be a problem I think. For example the admin and a user try to insert something in users_profile table and there is no locking featcher, which data will be insert and shown at next?

Yes that could be a problem but it is a different problem.

I would think the best solution there would be for the admin to make sure that situation didn't occur.  Grin
For example wait until they are logged off then deactivate the user, do the changes and re activate them.

Depends how often and how complicated the changes are I suppose.

Mike
OMG!I am geek
**
Posts: 57
394 credits
Members referred : 0


« Reply #8 on: May 23, 2007, 03:48:40 PM »

...
For example wait until they are logged off then deactivate the user, do the changes and re activate them.

Depends how often and how complicated the changes are I suppose.

Mike

I know it's an new case but it's importend enough to think about. I don't think that if would be a good way to wait until the user has loged out, it can take minutes. I think it's only nessesary to lock viewing/edditing the user until the  admin has finished  edding something.
Metal slug addict
*
Posts: 19
126 credits
Members referred : 0


« Reply #9 on: May 23, 2007, 03:52:40 PM »

Quote
I think it's only nessesary to lock viewing/edditing the user until the  admin has finished  edding something.

You are right. Good luck in getting Olaf to change it  Smiley

Mike
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6309
38674 credits
Members referred : 374


It's time to use PHP5!


« Reply #10 on: May 23, 2007, 04:01:18 PM »

not sure, its a little strange that there is only one solution available to handle this kind of problem...

I don't like to replace an old script with another one, I guess I will check a second way to handle this...


Last blog : Is your website is down? Know before your visitors do!
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #11 on: May 23, 2007, 04:02:59 PM »

I think this is too much buzz for nothing. First of all a DBMS (the database you are using) locks table when writes anyway, so it is impossible to have a broken session. When you are using files for session handling php locks the files, because if the same session do a write request at the same time (which is not very possible, but still is possible) the problem could be huge, because a situation like this can even crash the server.

Now even if you lock the row while you are writing on it, the next request will fail so this doesn't solve anything. In my opinion the only way to solve this is by using UPDATE statements, and instead of having a serialized php array in your table, use a table with one field for every important variable. This way you wont have any troubles with this.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6309
38674 credits
Members referred : 374


It's time to use PHP5!


« Reply #12 on: May 23, 2007, 04:04:44 PM »

After looking at it in more detail it seems the problem only occurs if there are 2 overlapping accesses with the same session id

This usually doesn't happen. It does happen if a user opens more than one browser window (At least in Firefox) and browses different parts of the same website in each window. Or if the site is designed to use Frames.

Personally I find the solutions  too complicated to risk incase they introduce other problems. 

It might be worth giving some thought to what would be the worst that could happen in the Access_User class by using it in 2 simultaneaous windows. I have tried opening 2 windows and have seen in the database that they are sharing the same session row. When I log out of one and then try to do something in the other it prompts me to log in again. So the session has been corrupted but in a safe way. There might be other situations though that aren't so good?

Mike



to get the same result IN IE you should start your session like here: http://www.finalwebsites.com/snippets.php?id=42 Visit through proxy


Last blog : Is your website is down? Know before your visitors do!
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6309
38674 credits
Members referred : 374


It's time to use PHP5!


« Reply #13 on: May 23, 2007, 04:08:01 PM »

I think this is too much buzz for nothing. First of all a DBMS (the database you are using) locks table when writes anyway, so it is impossible to have a broken session. When you are using files for session handling php locks the files, because if the same session do a write request at the same time (which is not very possible, but still is possible) the problem could be huge, because a situation like this can even crash the server.

Now even if you lock the row while you are writing on it, the next request will fail so this doesn't solve anything. In my opinion the only way to solve this is by using UPDATE statements, and instead of having a serialized php array in your table, use a table with one field for every important variable. This way you wont have any troubles with this.

I have that Idea too, and the whole item is not hot enough Smiley

I guess we need a file based safe session handling routine to use it on shared hosting platforms


Last blog : Is your website is down? Know before your visitors do!
Metal slug addict
*
Posts: 19
126 credits
Members referred : 0


« Reply #14 on: May 23, 2007, 04:09:28 PM »

Quote
not sure, its a little strange that there is only one solution available to handle this kind of problem...

I don't like to replace an old script with another one, I guess I will check a second way to handle this...

Olaf, just to clarify. Tomz started talking about something completely different to this thread. His solution has nothing to do with the thread problem.

Mike
Metal slug addict
*
Posts: 19
126 credits
Members referred : 0


« Reply #15 on: May 23, 2007, 04:13:32 PM »

Quote
I think this is too much buzz for nothing. First of all a DBMS (the database you are using) locks table when writes anyway, so it is impossible to have a broken session.

It is possible cos I did it.  Smiley Just do the 2 window thing in Firefox.

Mike
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #16 on: May 23, 2007, 04:16:07 PM »

You did this with db driven sessions? And what was the problem?

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Metal slug addict
*
Posts: 19
126 credits
Members referred : 0


« Reply #17 on: May 23, 2007, 04:17:31 PM »

Just read reply number 3 in this thread
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #18 on: May 23, 2007, 04:21:21 PM »

Just read reply number 3 in this thread

Right Smiley

I think you can't really solve this problem except than using a table structure like I said before.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=6400
Tags : access user locking Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  Access_user Class (Moderator: Olaf)
Topic: Simultaneous access needs database locking solution
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Sep 05, 2008, 04:59:39 PM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 36.293
Total Topics: 7.476
Total Members: 3.899
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: speedy5044

27 Guests, 4 Users online :

16 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.