For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > July 2007 > Re: [PHP-DB] connection id mystery









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PHP-DB] connection id mystery
Stut

2007-07-25, 6:58 pm

Charles Whitaker wrote:
> Since I'm requesting persistent connections, why don't I get the same
> connection each time? Or, to ask it another way: I notice that the
> number of threads slowly increases as I continue to access records --
> why would this happen if I'm using persistent connections? I assumed
> that the first time I did 'new PDO' I'd create a connection, and each
> time after that I'd get the same connection. At least that's how I read
> the documentation.


Using persistant connections simply means that you will be given a
connection from a pool of open connections rather than creating a new
one each time. There is no guarantee you'll get the same connection from
request to request.

> Can anyone point me in the right direction? Thanks.


I would suggest you add another field to the table you are trying to
lock. Put an ID in there that you can pass from request to request. Use
the locking feature to lock the table, read that value for a record, if
it's not set to something write your ID to it then unlock it. If it does
already contain an ID you treat it as locked. you just need to make sure
you unlock the row when you're done (probably by setting that value to NULL.

If you're already using sessions I would strongly recommend using the
session ID as the lock ID. If not you can easily generate one but you'll
need to pass it manually from request to request.

That may not be as clean as a pure SQL solution, but it will work. You
may also want to add a timestamp row to allow for a timeout on the lock.

-Stut

--
http://stut.net/
Stut

2007-07-26, 9:58 pm

Charles Whitaker wrote:
>
> Right. I had assumed that I would get a connection previously initiated
> by me, and that there would therefore be only one, so I would always get
> the same connection. So much for assumptions.
>
>
> I did as you suggest, and it seems to work fine. I surrounded the lock
> code with get_lock() and release_lock(), to make it quasi-atomic.
>
> Thanks for the suggestion, and thanks to Charles Morris as well for his
> response.


No problem, but please include the list when replying in future.

-Stut

--
http://stut.net/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com