|
Posted by Colin McKinnon on 11/18/67 11:42
usenet@shat.net wrote:
>
> I've created a custom session handler that saves session data to a
> MySQL database. If the database server is down, I'd like to fallback to
> PHP's default session handler (files) until the server comes back up.
>
> After calling session_set_save_handler(), is there a way to tell PHP to
> ignore this setting and revert to temp files for session storage? If I
> try an ini_set on session.save_handler upon detecting that the DB
> server is down, it doesn't work because the session is already active.
>
You're trying to solve the wrong problems in the wrong way.
Unless you've got a very unusual application, the biggest problem you are
going to have is detecting the state of the DBMS. You don't want to be
waiting for every database operation to timeout. This then solves your
problem but shifts the emphasis onto a reliable method for detecting an
outage.
Since the interval between reading and writing a session *should be* a lot
shorter than the interval between writing and reading a session, there
there will be very little chance of rescuing session data during the
failure. So don't waste your time trying to change the save handler after
you've set it up. Again this makes your life simpler, although its possibly
not the news you wanted to hear.
This just leaves a simple if wrapper around the session handler:
if ((file_exists($database_working_semaphore)) || (rand(0,100)>98)) {
if (test_db_connection()) {
file_put_contents($database_working_semaphore, time());
} else {
unlink($database_working_semaphore);
}
}
if (file_exists($database_working_semaphore)) {
// use database session handler
} else {
// use file handler
}
Now if you are wanting a fault-tolerant solution instead of a failover
solution....that's a different story. But you'll have to raise your game a
lot. (ouch ! mixed metaphor).
C.
Navigation:
[Reply to this message]
|