Re: [PHP] __sleep, __wakeup and persistent connections

2005-08-21 Thread Richard Lynch
On Sat, August 20, 2005 6:33 am, Marcus Bointon wrote:
 I'm sorting out some code to handle session object storage,

I can't help with the OO stuff.

 given that the connection is persistent, and may thus be used by
 other scripts, is calling mysql_close a particularly bad idea? Should
 I just not bother closing the connection, letting PHP deal with it -
 the object will not attempt to re-use the stale connection because it
 will get a new instance courtesy of __wakeup when unserialized.

The persistence is all on the MySQL side -- From PHP's perspective,
it's more like a re-usable or a return for deposit connection :-)

When you close the persistent connection, MySQL still keeps around
the data structures to re-use it for the next script, which is what
persistent really means.

You don't have to close the connection -- PHP *is* going to close it
for you as the script ends.

When the same process (Apache child) asks for a connection with the
same username/password from MySQL, MySQL can re-use the existing data
structures to save the time of building them.  That time is actually
rather expensive, as it involves a lot of buffers, structures, and, I
believe, a fair amount of resource-locking to be sure the current
connection will not be in a race condition with other connections for
things like mysql_insert_id() and, if you use innoDB, transactions.

So saving this time for MySQL to build a connection from scratch
instead of using the parts laying around from the last one is the
persistent connection.

HTH

NOTE: Be sure you have a few *more* max_connections in /etc/my.cnf
than the number of Apache MaxChildren -- Otherwise your Apache
children can lock up all the MySQL persistent connections and leave
you with Apache children starved for MySQL connection, as well as NO
WAY for you to use mysqladmin or mysqldump since *ALL* the available
connections are taken up by your Apache server.  That's bad.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] __sleep, __wakeup and persistent connections

2005-08-20 Thread Marcus Bointon
I'm sorting out some code to handle session object storage,  
particularly where the objects contain DB connections. I've found  
lots of articles that go on about how __sleep should clean up db  
stuff, close connections etc and how __wakeup should reconnect, but  
weirdly enough I've not found a single concrete example of doing this!


It's also not quite clear how this behaviour interacts with  
persistent connections. For example, If I do this:


class foo {

protected $db;

public function __construct() {
$this-db = mysql_pconnect();
}

protected function __wakeup() {
$this-db = mysql_pconnect();
}

protected function __sleep() {
mysql_close($this-db);
$this-db = NULL;
return array();
}
}

given that the connection is persistent, and may thus be used by  
other scripts, is calling mysql_close a particularly bad idea? Should  
I just not bother closing the connection, letting PHP deal with it -  
the object will not attempt to re-use the stale connection because it  
will get a new instance courtesy of __wakeup when unserialized.


I'm also unclear about how __sleep acts in subclasses - it seems that  
as soon as I have a __sleep function in a base class, I don't have  
any choice but to implement __sleep functions in every subclass of  
it, even if I don't want them to do anything that would not be done  
if __sleep were not defined. in the exampel, the base class has no  
properties to save, so it returns an empty array from __sleep, but  
that's unlikely to be useful for a subclass that does have properties  
(and serializing an object without any properties is pointless!).


Ideas?

Marcus
--
Marcus Bointon
Synchromedia Limited: Putting you in the picture
[EMAIL PROTECTED] | http://www.synchromedia.co.uk

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php