Am 08.01.2015 um 16:01 schrieb bruce:
hey. within php (or any other language) is there a way to create the mysql sql, and execute the sql, where the process can "wait" until the network connection for the mysql command/process is actually valid? IE (phpesque) $pdo=new pdo() sql = "select * from foo where a=:a" $s=$pdo->prepare($sql) $s->bind("a",$one) $s->execute() The issue we're seeing, is that the network (it's all wifi) is really bad.. and the app machine, might not have a connection to the db box. We're wondering if there's a way to simply have mysql/php detect when a valid connection is there, and then proceed. We've thought about the try/catch process, any others?
snippets from our db-layer to get an idea * everytime a error happens the reconnect method is triggered * reconnect tries 300 times with a small sleep to connect again * after it returns the last query is fired againworks that way for around 8 years now and we can restart mysqld at every random moment without lose a single web request
________________________________________ if(!in_array(@mysqli_errno($this->conn), array_keys($this->ignored_errors))) { $this->reconnect(); $fehler = 0; switch($unbuffered) {case true: $result = @mysqli_query($this->conn, $sql, MYSQLI_USE_RESULT) or $fehler = 1; break; default: $result = @mysqli_query($this->conn, $sql, MYSQLI_STORE_RESULT) or $fehler = 1; break;
} } public function reconnect() { $this->disconnect(); $this->connect($this->persistent); } if(!$rw) { for($retry=1; $retry<=300; $retry++) { /** Initialisieren */ $this->conn = @mysqli_init(); /** SSL-Encryption wenn aktiviert und TCP */ if($this->ssl && $this->host != 'localhost') {/** Wenn kein Zertifikat angegeben ist Dummy-Eintrag fuer Standard-Handshake da sonst keine Verschluesselung etabliert wird */
if($this->ssl_crt === '') { $this->ssl_crt = 'dummy.crt'; } /** SSL aktivieren */$this->conn->ssl_set($this->ssl_key, $this->ssl_crt, $this->ssl_ca, NULL, 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:RSA-AES256-SHA');
} /** Verbindung herstellen */ switch($persistent) {case 1: $rw = @mysqli_real_connect($this->conn, 'p:' . $this->host, $this->user, $this->pwd, $this->db, $this->port, '', $flags); break; default: $rw = @mysqli_real_connect($this->conn, $this->host, $this->user, $this->pwd, $this->db, $this->port, '', $flags); break;
}/** Wenn Server wieder verfuegbar Verbindung erneut trennen und aufbauen um Fehlern waehrend des Initialisieren des Dienstes aus dem Weg zu gehen */
if($rw) { /** Initialisieren */ $this->conn = @mysqli_init(); /** SSL-Encryption wenn aktiviert und TCP */ if($this->ssl && $this->host != 'localhost') {/** Wenn kein Zertifikat angegeben ist Dummy-Eintrag fuer Standard-Handshake da sonst keine Verschluesselung etabliert wird */
if($this->ssl_crt === '') { $this->ssl_crt = 'dummy.crt'; } /** SSL aktivieren */$this->conn->ssl_set($this->ssl_key, $this->ssl_crt, $this->ssl_ca, NULL, 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:RSA-AES256-SHA');
} /** Verbindung herstellen */ switch($persistent) {case 1: $rw = @mysqli_real_connect($this->conn, 'p:' . $this->host, $this->user, $this->pwd, $this->db, $this->port, '', $flags); break; default: $rw = @mysqli_real_connect($this->conn, $this->host, $this->user, $this->pwd, $this->db, $this->port, '', $flags); break;
} break; } usleep(50000); }/** Es konnte trotz mehrmaligem Versuch keine Verbindung hergestellt werden */
if(!$rw) { $host_resolved = @gethostbyaddr($this->host); if(!$host_resolved) { $host_resolved = $this->host; } $this->conn = 0;$this->error('Verbindung zu Datenbank-Server <span style="white-space:nowrap;">"' . trim($host_resolved . ':' . $this->port . ' ' . $this->db) . '"</span> konnte nicht hergestellt werden.<br /><br />' . mysqli_connect_error());
} }
signature.asc
Description: OpenPGP digital signature