Singleton DB Connection with Sessions stored in DB - problem
Posted: Mon Oct 09, 2006 3:01 am
Hi all,
I'm using a singleton as a DB connection class, it's a plain vanilla singleton - nothing fancy at all. I'm also storing my sessions in a database, via a session class (which obviously needs to access the database). So I've changed the session handler to use my session class.
I've found however, that my Singleton works fine in the session open and session read phases, but gives me the error "could not fetch mysqli" in the write and close phases.
Can it be that the db connection class is being unloaded from memory before the session finishes?
I've found that if I don't use a singleton (that is, I create a concrete instance of the db connection and use that) then I don't get this problem, because the object seems to persist after the session. But with the singleton I get that error "Unable to fetch mysqli", in the write and close phases of the session.
I notice also that if I put a session_write_close() command immediately before the closing php tag that this problem goes away (presumably because the db connection class is still in memory at this point).
Has anyone experienced anything similar? Are there any resources that explain what PHP does (and in what order) when it reaches the closing php tag on the page?
I'm using a singleton as a DB connection class, it's a plain vanilla singleton - nothing fancy at all. I'm also storing my sessions in a database, via a session class (which obviously needs to access the database). So I've changed the session handler to use my session class.
I've found however, that my Singleton works fine in the session open and session read phases, but gives me the error "could not fetch mysqli" in the write and close phases.
Can it be that the db connection class is being unloaded from memory before the session finishes?
I've found that if I don't use a singleton (that is, I create a concrete instance of the db connection and use that) then I don't get this problem, because the object seems to persist after the session. But with the singleton I get that error "Unable to fetch mysqli", in the write and close phases of the session.
I notice also that if I put a session_write_close() command immediately before the closing php tag that this problem goes away (presumably because the db connection class is still in memory at this point).
Has anyone experienced anything similar? Are there any resources that explain what PHP does (and in what order) when it reaches the closing php tag on the page?