Skip to content

Instantly share code, notes, and snippets.

@lf-uraku-yuki
Created November 7, 2017 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lf-uraku-yuki/519038e8e5b87bb8c17c3feb0b03238b to your computer and use it in GitHub Desktop.
Save lf-uraku-yuki/519038e8e5b87bb8c17c3feb0b03238b to your computer and use it in GitHub Desktop.
Codeigniter 3.x Database Connect Retry Sample
<?php
function reconnectDb()
{
if ($this->db->conn_id !== false) {
return;
}
log_message('error', 'DB接続失敗。再接続開始');
// スクリプト実行可能時間を延ばしておく
ini_set('max_execution_time', '180');
for ($retry_count = 0; $retry_count < 8; $retry_count ++) {
sleep(15);
$this->load->database();
if ($this->db->conn_id !== false) {
log_message('debug', 'DB再接続処理OK');
return;
}
log_message('debug', 'DB再接続処理NG');
}
}
@lf-uraku-yuki
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment