--- wp-includes/wp-db.php.orig	2009-11-28 18:11:46.000000000 -0300
+++ wp-includes/wp-db.php	2009-11-28 18:39:51.000000000 -0300
@@ -686,36 +686,62 @@
 		global $db_list, $global_db_list;
 		if( is_array( $db_list ) == false )
 			return true;
-
-		if( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) {
-			$action = 'global';
-			$details = $global_db_list[ mt_rand( 0, count( $global_db_list ) -1 ) ];
-			$this->db_global = $details;
-		} elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) {
-			$action = 'write';
-			$details = $db_list[ 'write' ][ mt_rand( 0, count( $db_list[ 'write' ] ) -1 ) ];
-			$this->db_write = $details;
-		} else {
-			$action = '';
+			
+			
+		/**
+		 * Open One connection for reading
+		 */
+
+		$action = 'read';
+		$dbhname = "dbh$action";
+		if(!is_resource($this->$dbhname)){
 			$details = $db_list[ 'read' ][ mt_rand( 0, count( $db_list[ 'read' ] ) -1 ) ];
 			$this->db_read = $details;
+			$this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );
+
+			if (!$this->$dbhname ) {
+				$this->bail("
+			<h1>Error establishing a database connection for Reading</h1>
+			<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>$dbhost</code>. This could mean your host's database server is down.</p>
+			<ul>
+				<li>Are you sure you have the correct username and password?</li>
+				<li>Are you sure that you have typed the correct hostname?</li>
+				<li>Are you sure that the database server is running?</li>
+			</ul>
+			<p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
+			");
+			}
+			@mysql_query( "SET NAMES '{$this->charset}'", $this->$dbhname);//set NAMES
+			$this->select( $details[ 'db_name' ], $this->$dbhname );
 		}
 
-		$dbhname = "dbh" . $action;
-		$this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );
-		if (!$this->$dbhname ) {
-			$this->bail("
-<h1>Error establishing a database connection</h1>
-<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>$dbhost</code>. This could mean your host's database server is down.</p>
-<ul>
-	<li>Are you sure you have the correct username and password?</li>
-	<li>Are you sure that you have typed the correct hostname?</li>
-	<li>Are you sure that the database server is running?</li>
-</ul>
-<p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
-");
+		/**
+		 * Open connection for writing
+		 */
+			
+		$action = 'write';
+		$dbhname = "dbh$action";
+		if(!is_resource($this->$dbhname)){
+			$details = $db_list[ 'write' ][ mt_rand( 0, count( $db_list[ 'write' ] ) -1 ) ];
+			$this->db_write = $details;
+
+			$this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );
+
+			if (!$this->$dbhname ) {
+				$this->bail("
+			<h1>Error establishing a database connection for Writing</h1>
+			<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>$dbhost</code>. This could mean your host's database server is down.</p>
+			<ul>
+				<li>Are you sure you have the correct username and password?</li>
+				<li>Are you sure that you have typed the correct hostname?</li>
+				<li>Are you sure that the database server is running?</li>
+			</ul>
+			<p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
+			");
+			}
+			@mysql_query( "SET NAMES '{$this->charset}'", $this->$dbhname);//set NAMES
+			$this->select( $details[ 'db_name' ], $this->$dbhname );
 		}
-		$this->select( $details[ 'db_name' ], $this->$dbhname );
 	}
 
 	/**
@@ -750,32 +776,29 @@
 		// Perform the query via std mysql_query function..
 		if ( defined('SAVEQUERIES') && SAVEQUERIES )
 			$this->timer_start();
-		
+
 		// use $this->dbh for read ops, and $this->dbhwrite for write ops
 		// use $this->dbhglobal for gloal table ops
 		unset( $dbh );
 		if( defined( "WP_USE_MULTIPLE_DB" ) && CONSTANT( "WP_USE_MULTIPLE_DB" ) == true ) {
-			if( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) {
-				if( false == isset( $this->dbhglobal ) ) {
-					$this->db_connect( $query );
-				}
-				$dbh =& $this->dbhglobal;
-				$this->last_db_used = "global";
-			} elseif ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) {
+
+			if ( preg_match("/^\\s*(alter table|create|insert|delete|update|replace) /i",$query) ) {
 				if( false == isset( $this->dbhwrite ) ) {
 					$this->db_connect( $query );
 				}
 				$dbh =& $this->dbhwrite;
 				$this->last_db_used = "write";
 			} else {
-				$dbh =& $this->dbh;
+				if( false == isset( $this->dbhread ) ) {
+					$this->db_connect( $query );
+				}
+				$dbh =& $this->dbhread;
 				$this->last_db_used = "read";
 			}
 		} else {
 			$dbh =& $this->dbh;
 			$this->last_db_used = "other/read";
 		}
-
 		$this->result = @mysql_query($query, $dbh);
 		++$this->num_queries;
 
