Bug 579147 part 2 - log table name, db name, username, and hostname on a db error 1.3.1
authorToby Elliott <telliott@mozilla.com>
Mon, 19 Jul 2010 14:54:29 -0700
changeset 135 cf4f2faa6986
parent 134 d011b1e5d4b2
child 136 aa8ae58ac5af
push id1
push usertziade@mozilla.com
push date2010-10-19 10:41 +0000
bugs579147
Bug 579147 part 2 - log table name, db name, username, and hostname on a db error
1.0/weave_storage/mysql.php
--- a/1.0/weave_storage/mysql.php
+++ b/1.0/weave_storage/mysql.php
@@ -95,16 +95,25 @@ class WeaveStorage implements WeaveStora
 		
 		if (defined('WEAVE_USER_TABLE_MODULO') && is_int(WEAVE_USER_TABLE_MODULO))
 			$this->_db_name .= ($this->_username % WEAVE_USER_TABLE_MODULO);
 			
 		if (defined('WEAVE_MYSQL_COLLECTION_TABLE_NAME'))
 			$this->_collection_table_name = WEAVE_MYSQL_COLLECTION_TABLE_NAME;
 	}
 
+	function connection_details_string()
+	{
+		$string = "host => " . WEAVE_MYSQL_STORE_READ_HOST;
+		$string .= ", db => " . WEAVE_MYSQL_STORE_READ_DB;
+		$string .= ", table => " . $this->_db_name;
+		$string .= ", user => " . $this->_username;
+		return $string;
+	}
+
 	function open_connection() 
 	{		
 		if ($this->_dbh)
 			return;
 			
 		try
 		{
 			if ($_SERVER['REQUEST_METHOD'] == 'GET')
@@ -120,17 +129,17 @@ class WeaveStorage implements WeaveStora
 									(defined('WEAVE_MYSQL_STORE_WRITE_PORT') ? ';port=' . WEAVE_MYSQL_STORE_WRITE_PORT : '') . 
 									';dbname=' . WEAVE_MYSQL_STORE_WRITE_DB, 
 									WEAVE_MYSQL_STORE_WRITE_USER, WEAVE_MYSQL_STORE_WRITE_PASS); 
 			}
 			$this->_dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 		}
 		catch( PDOException $exception )
 		{
-			error_log($exception->getMessage());
+			error_log("open_connection (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 	}
 	
 	function get_connection()
 	{
 		$this->open_connection();
 		return $this->_dbh;
@@ -140,17 +149,17 @@ class WeaveStorage implements WeaveStora
 	{
 		$this->open_connection();
 		try
 		{
 			$this->_dbh->beginTransaction();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("begin_transaction: " . $exception->getMessage());
+			error_log("begin_transaction (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		return true;
 	}
 
 	function commit_transaction()
 	{
 		$this->_dbh->commit();
@@ -174,17 +183,17 @@ class WeaveStorage implements WeaveStora
 				
 			$sth = $this->_dbh->prepare($select_stmt);
 			$sth->bindParam(':userid', $this->_username);
 			$sth->bindParam(':collection', $collection);
 			$sth->execute();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("get_collection_id: " . $exception->getMessage());
+			error_log("get_collection_id (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		
 		$result = $sth->fetchColumn();
 		if (!$result)
 		{
 			$result = $this->store_collection_id($collection);
 		}
@@ -203,17 +212,17 @@ class WeaveStorage implements WeaveStora
 			$select_stmt = '/*[queryName=get_max_collection_id]*/ select max(collectionid) from ' . $this->_collection_table_name . ' where userid = :userid';
 				
 			$sth = $this->_dbh->prepare($select_stmt);
 			$sth->bindParam(':userid', $this->_username);
 			$sth->execute();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("store_collection_id: " . $exception->getMessage());
+			error_log("store_collection_id (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		$result = $sth->fetchColumn();
 		if (!$result)
 			$result = 100;
 		$result += 1;
 		
 		$sth->closeCursor();
@@ -223,17 +232,17 @@ class WeaveStorage implements WeaveStora
 		
 		try
 		{
 			$sth = $this->_dbh->prepare($insert_stmt);
 			$sth->execute($values);
 		}
 		catch( PDOException $exception )
 		{
-			error_log("store_collection_id: " . $exception->getMessage());
+			error_log("store_collection_id (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		
 		return $result;
 		
 	}
 
 	function get_collection_name($collection_id)
@@ -254,17 +263,17 @@ class WeaveStorage implements WeaveStora
 				
 			$sth = $this->_dbh->prepare($select_stmt);
 			$sth->bindParam(':userid', $this->_username);
 			$sth->bindParam(':collection', $collection_id);
 			$sth->execute();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("get_collection_name: " . $exception->getMessage());
+			error_log("get_collection_name (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		
 		$result = $sth->fetchColumn();
 		return $result;		
 		
 	}
 
@@ -276,17 +285,17 @@ class WeaveStorage implements WeaveStora
 		{
 			$select_stmt = '/*[queryName=get_users_collection_list]*/ select collectionid, name from ' . $this->_collection_table_name . ' where userid = :userid';
 			$sth = $this->_dbh->prepare($select_stmt);
 			$sth->bindParam(':userid', $this->_username);
 			$sth->execute();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("get_users_collection_list: " . $exception->getMessage());
+			error_log("get_users_collection_list (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		
 		
 		$collections = array();
 		while ($result = $sth->fetch(PDO::FETCH_NUM))
 		{
 			$this->WEAVE_COLLECTION_NAMES[$result[0]] = $result[1];
@@ -313,17 +322,17 @@ class WeaveStorage implements WeaveStora
 				
 			$sth = $this->_dbh->prepare($select_stmt);
 			$sth->bindParam(':username', $this->_username);
 			$sth->bindParam(':collection', $collection);
 			$sth->execute();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("get_max_timestamp: " . $exception->getMessage());
+			error_log("get_max_timestamp (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		
 		$result = $sth->fetchColumn();
 		return $result;		
 	}
 
 	function get_collection_list()
@@ -334,17 +343,17 @@ class WeaveStorage implements WeaveStora
 		{
 			$select_stmt = '/*[queryName=get_collection_list]*/ select distinct(collection) from ' . $this->_db_name . ' where username = :username';
 			$sth = $this->_dbh->prepare($select_stmt);
 			$sth->bindParam(':username', $this->_username);
 			$sth->execute();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("get_collection_list: " . $exception->getMessage());
+			error_log("get_collection_list (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		$results = $sth->fetchAll(PDO::FETCH_NUM);
 		$sth->closeCursor();
 			
 		$collections = array();
 		foreach ($results as $result)
 		{
@@ -373,17 +382,17 @@ class WeaveStorage implements WeaveStora
 		{
 			$select_stmt = '/*[queryName=get_collection_list_with_timestamps]*/ select collection, max(modified) as timestamp from ' . $this->_db_name . ' where username = :username group by collection';
 			$sth = $this->_dbh->prepare($select_stmt);
 			$sth->bindParam(':username', $this->_username);
 			$sth->execute();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("get_collection_list: " . $exception->getMessage());
+			error_log("get_collection_list (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		$results = $sth->fetchAll(PDO::FETCH_NUM);
 		$sth->closeCursor();
 		
 		$collections = array();
 		$user_collections = 0;
 		foreach ($results as $result)
@@ -412,17 +421,17 @@ class WeaveStorage implements WeaveStora
 		{
 			$select_stmt = '/*[queryName=get_collection_list_with_counts]*/ select collection, count(*) as ct from ' . $this->_db_name . ' where username = :username group by collection';
 			$sth = $this->_dbh->prepare($select_stmt);
 			$sth->bindParam(':username', $this->_username);
 			$sth->execute();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("get_collection_list_with_counts: " . $exception->getMessage());
+			error_log("get_collection_list_with_counts (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		$results = $sth->fetchAll(PDO::FETCH_NUM);
 		$sth->closeCursor();
 		
 		$collections = array();
 		$user_collections = 0;
 		foreach ($results as $result)
@@ -451,17 +460,17 @@ class WeaveStorage implements WeaveStora
 		{
 			$select_stmt = '/*[queryName=get_collection_list_with_all]*/ select collection, max(modified) as timestamp, count(*) as ct from ' . $this->_db_name . ' where username = :username group by collection';
 			$sth = $this->_dbh->prepare($select_stmt);
 			$sth->bindParam(':username', $this->_username);
 			$sth->execute();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("get_collection_list_with_counts: " . $exception->getMessage());
+			error_log("get_collection_list_with_counts (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		$results = $sth->fetchAll(PDO::FETCH_NUM);
 		$sth->closeCursor();
 		
 		$collections = array();
 		$user_collections = 0;
 		foreach ($results as $result)
@@ -512,17 +521,17 @@ class WeaveStorage implements WeaveStora
 		try
 		{
 			$sth = $this->_dbh->prepare($insert_stmt);
 			$sth->execute($values);
 			return $sth->rowCount();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("store_object: " . $exception->getMessage());
+			error_log("store_object (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 	}
 
 	function update_object(&$wbo)
 	{
 		$this->open_connection();
 
@@ -596,17 +605,17 @@ class WeaveStorage implements WeaveStora
 		try
 		{
 			$sth = $this->_dbh->prepare($update);
 			$sth->execute($params);
 			return $sth->rowCount();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("update_object: " . $exception->getMessage());
+			error_log("update_object (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 	}
 	
 	function delete_object($collection, $id)
 	{
 		$this->open_connection();
 
@@ -621,17 +630,17 @@ class WeaveStorage implements WeaveStora
 			$sth->bindParam(':collection', $collectionid);
 
 			$sth->bindParam(':id', $id);
 			$sth->execute();
 			return $sth->rowCount();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("delete_object: " . $exception->getMessage());
+			error_log("delete_object (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 	}
 	
 	
 	function delete_objects($collection, $id = null, $parentid = null, $predecessorid = null, $newer = null, 
 								$older = null, $sort = null, $limit = null, $offset = null, $ids = null, 
 								$index_above = null, $index_below = null)
@@ -753,17 +762,17 @@ class WeaveStorage implements WeaveStora
 		try
 		{
 			$sth = $this->_dbh->prepare($select_stmt);
 			$sth->execute($params);
 			return $sth->rowCount();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("delete_objects: " . $exception->getMessage());
+			error_log("delete_objects (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 	}
 
 	function retrieve_object($collection, $id)
 	{
 		$this->open_connection();
 
@@ -776,17 +785,17 @@ class WeaveStorage implements WeaveStora
 			$collectionid = $this->get_collection_id($collection);
 			$sth->bindParam(':collection', $collectionid);
 
 			$sth->bindParam(':id', $id);
 			$sth->execute();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("retrieve_object: " . $exception->getMessage());
+			error_log("retrieve_object (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		
 		$result = $sth->fetch(PDO::FETCH_ASSOC);
 		$wbo = new wbo();
 		$wbo->populate($result);
 		if ($wbo->validate())
 			return $wbo;
@@ -913,17 +922,17 @@ class WeaveStorage implements WeaveStora
 
 		try
 		{
 			$sth = $this->_dbh->prepare($select_stmt);
 			$sth->execute($params);
 		}
 		catch( PDOException $exception )
 		{
-			error_log("retrieve_collection: " . $exception->getMessage());
+			error_log("retrieve_collection (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 
 		if ($direct_output)
 		{
 			$direct_output->set_rowcount($sth->rowCount());
 			$direct_output->first();
 			while($result = $sth->fetch(PDO::FETCH_ASSOC))
@@ -962,17 +971,17 @@ class WeaveStorage implements WeaveStora
 		{
 			$select_stmt = '/*[queryName=get_storage_total]*/ select sum(payload_size) from ' . $this->_db_name . ' where username = :username';
 			$sth = $this->_dbh->prepare($select_stmt);
 			$sth->bindParam(':username', $this->_username);
 			$sth->execute();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("get_storage_total: " . $exception->getMessage());
+			error_log("get_storage_total (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		
 		return $sth->fetchColumn();		
 	}
 	
 	function get_collection_storage_totals()
 	{
@@ -982,17 +991,17 @@ class WeaveStorage implements WeaveStora
 		{
 			$select_stmt = '/*[queryName=get_collection_storage_totals]*/ select collection, sum(payload_size) from ' . $this->_db_name . ' where username = :username group by collection';
 			$sth = $this->_dbh->prepare($select_stmt);
 			$sth->bindParam(':username', $this->_username);
 			$sth->execute();
 		}
 		catch( PDOException $exception )
 		{
-			error_log("get_storage_total: " . $exception->getMessage());
+			error_log("get_storage_total (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		$results = $sth->fetchAll(PDO::FETCH_NUM);
 		$sth->closeCursor();			
 		
 		$collections = array();
 		$user_collections = 0;
 		foreach ($results as $result)
@@ -1032,17 +1041,17 @@ class WeaveStorage implements WeaveStora
 			$delete_stmt2 = 'delete from ' . $this->_collection_table_name . ' where userid = :username';
 			$sth = $this->_dbh->prepare($delete_stmt2);
 			$sth->bindParam(':username', $this->_username);
 			$sth->execute();
 
 		}
 		catch( PDOException $exception )
 		{
-			error_log("delete_user: " . $exception->getMessage());
+			error_log("delete_user (" . $this->connection_details_string() . "): " . $exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		return true;
 
 	}
 
 	function heartbeat()
 	{