Improved error handling in DB connection failure

release-0.6
thomascube 19 years ago
parent a2f2c5e1b7
commit 8affba5be0

@ -2,7 +2,7 @@
/* /*
+-----------------------------------------------------------------------+ +-----------------------------------------------------------------------+
| RoundCube Webmail IMAP Client | | RoundCube Webmail IMAP Client |
| Version 0.1-20060220 | | Version 0.1-20060314 |
| | | |
| Copyright (C) 2005, RoundCube Dev. - Switzerland | | Copyright (C) 2005, RoundCube Dev. - Switzerland |
| Licensed under the GNU GPL | | Licensed under the GNU GPL |
@ -110,6 +110,15 @@ if ($_framed)
// init necessary objects for GUI // init necessary objects for GUI
load_gui(); load_gui();
// check DB connections and exit on failure
if ($err_str = $DB->is_error())
{
raise_error(array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__,
'message' => $err_str), FALSE, TRUE);
}
// error steps // error steps
if ($_action=='error' && !empty($_GET['_code'])) if ($_action=='error' && !empty($_GET['_code']))
{ {

@ -74,10 +74,12 @@ function rcmail_startup($task='mail')
$DB = new rcube_db($CONFIG['db_dsnw'], $CONFIG['db_dsnr']); $DB = new rcube_db($CONFIG['db_dsnw'], $CONFIG['db_dsnr']);
$DB->sqlite_initials = $INSTALL_PATH.'SQL/sqlite.initial.sql'; $DB->sqlite_initials = $INSTALL_PATH.'SQL/sqlite.initial.sql';
$DB->db_connect('w');
// we can use the database for storing session data // we can use the database for storing session data
// session queries do not work with MDB2 // session queries do not work with MDB2
if ($CONFIG['db_backend']!='mdb2' && is_object($DB)) if ($CONFIG['db_backend']!='mdb2' && !$DB->is_error())
include_once('include/session.inc'); include_once('include/session.inc');

@ -35,7 +35,7 @@ require_once('DB.php');
* @package RoundCube Webmail * @package RoundCube Webmail
* @author David Saez Padros <david@ols.es> * @author David Saez Padros <david@ols.es>
* @author Thomas Bruederli <roundcube@gmail.com> * @author Thomas Bruederli <roundcube@gmail.com>
* @version 1.16 * @version 1.17
* @link http://pear.php.net/package/DB * @link http://pear.php.net/package/DB
*/ */
class rcube_db class rcube_db
@ -45,6 +45,9 @@ class rcube_db
var $db_connected = false; // Already connected ? var $db_connected = false; // Already connected ?
var $db_mode = ''; // Connection mode var $db_mode = ''; // Connection mode
var $db_handle = 0; // Connection handle var $db_handle = 0; // Connection handle
var $db_pconn = false; // Use persistent connections
var $db_error = false;
var $db_error_msg = '';
var $a_query_results = array('dummy'); var $a_query_results = array('dummy');
var $last_res_id = 0; var $last_res_id = 0;
@ -56,13 +59,14 @@ class rcube_db
* @param string DSN for read/write operations * @param string DSN for read/write operations
* @param string Optional DSN for read only operations * @param string Optional DSN for read only operations
*/ */
function __construct($db_dsnw, $db_dsnr='') function __construct($db_dsnw, $db_dsnr='', $pconn=false)
{ {
if ($db_dsnr=='') if ($db_dsnr=='')
$db_dsnr=$db_dsnw; $db_dsnr=$db_dsnw;
$this->db_dsnw = $db_dsnw; $this->db_dsnw = $db_dsnw;
$this->db_dsnr = $db_dsnr; $this->db_dsnr = $db_dsnr;
$this->db_pconn = $pconn;
$dsn_array = DB::parseDSN($db_dsnw); $dsn_array = DB::parseDSN($db_dsnw);
$this->db_provider = $dsn_array['phptype']; $this->db_provider = $dsn_array['phptype'];
@ -74,9 +78,9 @@ class rcube_db
* *
* @see rcube_db::__construct * @see rcube_db::__construct
*/ */
function rcube_db($db_dsnw,$db_dsnr='') function rcube_db($db_dsnw, $db_dsnr='', $pconn=false)
{ {
$this->__construct($db_dsnw,$db_dsnr); $this->__construct($db_dsnw, $db_dsnr);
} }
@ -90,12 +94,17 @@ class rcube_db
function dsn_connect($dsn) function dsn_connect($dsn)
{ {
// Use persistent connections if available // Use persistent connections if available
$dbh = DB::connect($dsn, array('persistent' => TRUE)); $dbh = DB::connect($dsn, array('persistent' => $this->db_pconn));
if (DB::isError($dbh)) if (DB::isError($dbh))
{ {
$this->db_error = TRUE;
$this->db_error_msg = $dbh->getMessage();
raise_error(array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__, raise_error(array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__,
'message' => $dbh->getMessage()), TRUE, FALSE); 'message' => $this->db_error_msg), TRUE, FALSE);
return FALSE;
} }
else if ($this->db_provider=='sqlite') else if ($this->db_provider=='sqlite')
@ -142,7 +151,18 @@ class rcube_db
$dsn = $this->db_dsnw; $dsn = $this->db_dsnw;
$this->db_handle = $this->dsn_connect($dsn); $this->db_handle = $this->dsn_connect($dsn);
$this->db_connected = true; $this->db_connected = $this->db_handle ? TRUE : FALSE;
}
/**
* Getter for error state
*
* @param boolean True on error
*/
function is_error()
{
return $this->db_error ? $this->db_error_msg : FALSE;
} }
@ -203,6 +223,9 @@ class rcube_db
$mode='w'; $mode='w';
$this->db_connect($mode); $this->db_connect($mode);
if (!$this->db_connected)
return FALSE;
if ($this->db_provider == 'sqlite') if ($this->db_provider == 'sqlite')
$this->_sqlite_prepare(); $this->_sqlite_prepare();

Loading…
Cancel
Save