views:

80

answers:

2

What exactly happens when the database attached to symfony goes offline or is unreachable? Maybe the answer to this question is in my face and I don't realize it, but I've been searching for the last while and still coming up empty handed.

I've attempted to simulate this myself (in the "production" environment) by just stopping the database service (I'm developing on my Windows laptop--deploying on a LAMP server), but it just sat there until it exceeded the PHP execution time limit.

I'm running symfony 1.4 with Doctrine. One would assume that it would throw some sort of catchable exception or maybe even be able to set a connection timeout and then throw an exception. I can't help but cringe at the thought of a customer just sitting there for 30 seconds waiting on a database query to finish that will never happen.

If there isn't any preventative measures already in the symfony core, can anyone make any suggestions on how they handle it? I'll of course have a heartbeat monitor on the database, but if it goes down the site needs to be taken down, too.

Update: Threw together a test script to just connect to the localhost database via Mysqli and it still reached the maximum execution time. However, it did throw a warning for the failed connection.

-- Logan

A: 

I dont have experience with symfony, but with standard PHP, if a script can't connect to MySQL, normally it displays a bunch of error messages. Why it takes so long, I don't know but my script usually complains immediately.

luckytaxi
Thanks for your response! :) I use HeidiSQL to manage my database (it's an awesome program, if you haven't heard of it) and it failed almost immediately on attempting to connect. Not sure what's going on.
Logan Bibby
+4  A: 

It depends. If the site is being accessed in the "dev" environment, it will give you a 500 internal server error with a message like this: "PDO Connection Error: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)" and a stack trace below that (like normal Exceptions in Symfony).

If you access the site in the "prod" environment you just get the standard (or custom if you've created one for 500 errors): "Oops! An Error Occurred The server returned a '500 Internal Server Error'." Without a stack trace.

In either case, it is an Exception being thrown (that is catchable). It's a Doctrine_Connection_Exception. You would likely want to try/catch this exception around a call to a model's save() method.

I'm not sure why it is taking so long and just timing out though, when I attempt it on my laptop locally, it returns the error almost immediately.

greggory.hz
Well, there's a SELECT query that happens with about every request so it should probably throw that exception no matter what... This is somewhat frustrating, haha. Do you know of any Doctrine settings that I could take a look through? This is my first time using symfony (which I learned it rather fast) but Doctrine is still a completely new ballgame since I've never used a DB abstraction/ORM that I didn't make myself.
Logan Bibby
Sorry, not just save(), but anything that causes a query to he run (execute(), fetchOne(), etc). I dont know where to cinfigure doctrine outside of outside of databases.yml and your schema. I'll let you know if I come across anything else.
greggory.hz
Since you're timing out without symfony as well, you probably have a more significant database problem. It sounds like you've restarted the db server? Perhaps try a re-install of wamp. But I'm not sure how simple that is, I really only have experience with lamp. Can you use something like phpmyadmin or navicat to connect to the database and run queries against it?
greggory.hz
@greggory.hz - I use HeidiSQL, which works about the same as NaviCat and phpMyAdmin. When the database is off, HeidiSQL errors immediately... I'll try reinstalling everything. It's not too difficult. I'll let you know how things go. Thanks for your help! :)
Logan Bibby