RE: [fw-general] Zend_Db Exception throwing?
Right. The behavior used to be that the connection was initiated lazily, only when a query was executed. This required the user to make a "dummy" query like "SELECT 1" to force the connection, as Jude found. In ZF 0.8 I made the getConnection() method force a connection, to give people a chance to initiate without having to run a query. But there is still value in making the connection lazily. Regards, Bill Karwin > -Original Message- > From: Guillaume Millet [mailto:[EMAIL PROTECTED] > Sent: Wednesday, March 21, 2007 4:02 AM > To: Zend Framework General > Subject: Re: [fw-general] Zend_Db Exception throwing? > > This is the behaviour I expected the first time I used Zend_Db. However, > when I found out that the connection is made only when necessary, I was > more willing to instanciate my $db object in my config file (so it's > only in a single place) since it doesn't connect to my database server > for pages that don't fetch data from it. > > Regards, > > Guillaume > > Doctorrock a écrit : > > > That's true Bill, but why not to put that code inside the Framework > > itself ? > > > > That way, when setting up a connection via the Factory, the Framework > > could try (itself) to connect to the database, and throw an Exception > > if it could not, instead of relying on the programmer to do so. > > > > - Julien from http://www.z-f.fr - > > > > > > 2007/3/19, Bill Karwin < [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>: > > > > FYI, you can also do this: > > > > $db->getConnection(); > > > > No need to execute a dummy query. The getConnection() method > > initiates > > the connection if one has not already been made. > > > > Regards, > > Bill Karwin > > > > > -Original Message- > > > From: Jude Aakjaer [mailto:[EMAIL PROTECTED] > > <mailto:[EMAIL PROTECTED]>] > > > Sent: Monday, March 19, 2007 2:09 AM > > > To: Zend Framework General > > > Subject: Re: [fw-general] Zend_Db Exception throwing? > > > > > > Excellent thanks. I've added in a "SELECT 1" query inside my > > try/catch > > > block to grab a connection error now > > > > > > -Jude A. > > > > > > On Mon, 19 Mar 2007 17:11:54 +0900, Alexander Netkachev > > > <[EMAIL PROTECTED] > > <mailto:[EMAIL PROTECTED]>> wrote: > > > > > > > Zend_Db_Adapter does not create a connection to the database > when > > you > > > > create > > > > it. The "real" PDO Connection is created when you execute first > > query or > > > > create instance of Zend_Db_Table class. So, the try/catch > > block does > > not > > > > catch this exception just because it is fired later in the code, > > when > > > the > > > > "real" connection is created. > > > > > > > > Sincerely, > > > > > > > > > > >
Re: [fw-general] Zend_Db Exception throwing?
This is the behaviour I expected the first time I used Zend_Db. However, when I found out that the connection is made only when necessary, I was more willing to instanciate my $db object in my config file (so it's only in a single place) since it doesn't connect to my database server for pages that don't fetch data from it. Regards, Guillaume Doctorrock a écrit : That's true Bill, but why not to put that code inside the Framework itself ? That way, when setting up a connection via the Factory, the Framework could try (itself) to connect to the database, and throw an Exception if it could not, instead of relying on the programmer to do so. - Julien from http://www.z-f.fr - 2007/3/19, Bill Karwin < [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>: FYI, you can also do this: $db->getConnection(); No need to execute a dummy query. The getConnection() method initiates the connection if one has not already been made. Regards, Bill Karwin > -Original Message- > From: Jude Aakjaer [mailto:[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>] > Sent: Monday, March 19, 2007 2:09 AM > To: Zend Framework General > Subject: Re: [fw-general] Zend_Db Exception throwing? > > Excellent thanks. I've added in a "SELECT 1" query inside my try/catch > block to grab a connection error now > > -Jude A. > > On Mon, 19 Mar 2007 17:11:54 +0900, Alexander Netkachev > <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote: > > > Zend_Db_Adapter does not create a connection to the database when you > > create > > it. The "real" PDO Connection is created when you execute first query or > > create instance of Zend_Db_Table class. So, the try/catch block does not > > catch this exception just because it is fired later in the code, when > the > > "real" connection is created. > > > > Sincerely, > > >
Re: [fw-general] Zend_Db Exception throwing?
That's true Bill, but why not to put that code inside the Framework itself ? That way, when setting up a connection via the Factory, the Framework could try (itself) to connect to the database, and throw an Exception if it could not, instead of relying on the programmer to do so. - Julien from http://www.z-f.fr - 2007/3/19, Bill Karwin <[EMAIL PROTECTED]>: FYI, you can also do this: $db->getConnection(); No need to execute a dummy query. The getConnection() method initiates the connection if one has not already been made. Regards, Bill Karwin > -Original Message- > From: Jude Aakjaer [mailto:[EMAIL PROTECTED] > Sent: Monday, March 19, 2007 2:09 AM > To: Zend Framework General > Subject: Re: [fw-general] Zend_Db Exception throwing? > > Excellent thanks. I've added in a "SELECT 1" query inside my try/catch > block to grab a connection error now > > -Jude A. > > On Mon, 19 Mar 2007 17:11:54 +0900, Alexander Netkachev > <[EMAIL PROTECTED]> wrote: > > > Zend_Db_Adapter does not create a connection to the database when you > > create > > it. The "real" PDO Connection is created when you execute first query or > > create instance of Zend_Db_Table class. So, the try/catch block does not > > catch this exception just because it is fired later in the code, when > the > > "real" connection is created. > > > > Sincerely, > > >
RE: [fw-general] Zend_Db Exception throwing?
FYI, you can also do this: $db->getConnection(); No need to execute a dummy query. The getConnection() method initiates the connection if one has not already been made. Regards, Bill Karwin > -Original Message- > From: Jude Aakjaer [mailto:[EMAIL PROTECTED] > Sent: Monday, March 19, 2007 2:09 AM > To: Zend Framework General > Subject: Re: [fw-general] Zend_Db Exception throwing? > > Excellent thanks. I've added in a "SELECT 1" query inside my try/catch > block to grab a connection error now > > -Jude A. > > On Mon, 19 Mar 2007 17:11:54 +0900, Alexander Netkachev > <[EMAIL PROTECTED]> wrote: > > > Zend_Db_Adapter does not create a connection to the database when you > > create > > it. The "real" PDO Connection is created when you execute first query or > > create instance of Zend_Db_Table class. So, the try/catch block does not > > catch this exception just because it is fired later in the code, when > the > > "real" connection is created. > > > > Sincerely, > > >
Re: [fw-general] Zend_Db Exception throwing?
Excellent thanks. I've added in a "SELECT 1" query inside my try/catch block to grab a connection error now -Jude A. On Mon, 19 Mar 2007 17:11:54 +0900, Alexander Netkachev <[EMAIL PROTECTED]> wrote: Zend_Db_Adapter does not create a connection to the database when you create it. The "real" PDO Connection is created when you execute first query or create instance of Zend_Db_Table class. So, the try/catch block does not catch this exception just because it is fired later in the code, when the "real" connection is created. Sincerely,
Re: [fw-general] Zend_Db Exception throwing?
On 3/19/07, Jude Aakjaer <[EMAIL PROTECTED]> wrote: I'm not sure if this is a related issue or a different one, but using the following code I cannot catch the failing exception try { $db = Zend_Db::factory($config->db->adaptor, array('host' => 'localhost', 'username' =>'noexistant', 'password' => 'failme', 'dbname' => 'random')); } catch (Exception $e) { echo "caught an exception! "; echo $e->getMessage(); exit; } I keep getting Fatal error: Uncaught exception 'Exception' with message 'SQLSTATE[28000] [1045] Access denied for user 'nonexistant'@'localhost' (using password: YES)' Without the database name I can successfuly catch an exception telling me that the dbname key hasn't been filled in, but I seemingly cannot catch the "failed to connect to db" exception Zend_Db_Adapter does not create a connection to the database when you create it. The "real" PDO Connection is created when you execute first query or create instance of Zend_Db_Table class. So, the try/catch block does not catch this exception just because it is fired later in the code, when the "real" connection is created. Sincerely, -- Alexander http://www.alexatnet.com/ - Blog and CMS created with Zend Framework and Ajax.
Re: [fw-general] Zend_Db Exception throwing?
I'm not sure if this is a related issue or a different one, but using the following code I cannot catch the failing exception try { $db = Zend_Db::factory($config->db->adaptor, array('host' => 'localhost', 'username' =>'noexistant', 'password' => 'failme', 'dbname' => 'random')); } catch (Exception $e) { echo "caught an exception! "; echo $e->getMessage(); exit; } I keep getting Fatal error: Uncaught exception 'Exception' with message 'SQLSTATE[28000] [1045] Access denied for user 'nonexistant'@'localhost' (using password: YES)' Without the database name I can successfuly catch an exception telling me that the dbname key hasn't been filled in, but I seemingly cannot catch the "failed to connect to db" exception -Jude A. On Sat, 10 Mar 2007 03:42:47 +0900, Bill Karwin <[EMAIL PROTECTED]> wrote: That's a good point. Using the PDO adapters should be transparent, and the Zend_Db classes should throw exceptions that extend Zend_Db_Exception in all cases. So we need to catch PDOException in the PDO adapters and re-throw as a more appropriate exception. Can you please log a bug on this in JIRA, and I'll fix it. If you have time to help me out, can you please identify the places in the PDO adapter code where usage of PDO methods may throw PDOException and need to be caught and re-thrown. I'll also make unit tests as part of the fix. Regards, Bill Karwin -Original Message- From: Ralph Schindler [mailto:[EMAIL PROTECTED] Sent: Friday, March 09, 2007 9:58 AM To: Zend Framework General Cc: Bill Karwin Subject: [fw-general] Zend_Db Exception throwing? Bill et. al., Out of curiosity, if I am using Zend_Db(_Adapter_Abstract) to execute a select object (query), should I be getting a Zend_Db_Exception or derivative thereof when invalid sql is queried? Ie., i cannot catch the following: try { $s = $database_connection->select(); $s->from('valid_table') ->where('not_valid_column = ?', 'value'); $database_connection->fetchAll($s); } catch (Zend_Db_Exception $e) { echo ""; print_r($e); die(); } Currently, I see the database connection is throwing a PDOException when there is invalid sql.. like the above (not a valid column) Is this the proper behavior? Thanks, Ralph
RE: [fw-general] Zend_Db Exception throwing?
That's a good point. Using the PDO adapters should be transparent, and the Zend_Db classes should throw exceptions that extend Zend_Db_Exception in all cases. So we need to catch PDOException in the PDO adapters and re-throw as a more appropriate exception. Can you please log a bug on this in JIRA, and I'll fix it. If you have time to help me out, can you please identify the places in the PDO adapter code where usage of PDO methods may throw PDOException and need to be caught and re-thrown. I'll also make unit tests as part of the fix. Regards, Bill Karwin > -Original Message- > From: Ralph Schindler [mailto:[EMAIL PROTECTED] > Sent: Friday, March 09, 2007 9:58 AM > To: Zend Framework General > Cc: Bill Karwin > Subject: [fw-general] Zend_Db Exception throwing? > > Bill et. al., > > Out of curiosity, if I am using Zend_Db(_Adapter_Abstract) to execute a > select object (query), should I be getting a Zend_Db_Exception or > derivative thereof when invalid sql is queried? Ie., i cannot catch the > following: > > > > try { >$s = $database_connection->select(); >$s->from('valid_table') > ->where('not_valid_column = ?', 'value'); >$database_connection->fetchAll($s); > } catch (Zend_Db_Exception $e) { >echo ""; >print_r($e); >die(); > } > > Currently, I see the database connection is throwing a PDOException when > there is invalid sql.. like the above (not a valid column) > > Is this the proper behavior? > > Thanks, > Ralph