2009-08-21 4 views
2

PHP를 사용하여 데이터베이스를 모니터링하고 데이터베이스가 다운 된 경우 웹 사이트에 유지 관리 페이지를 게시하는 프로세스를 만듭니다.PHP PDO를 사용하여 사용할 수없는 데이터베이스를 찾는 방법은 무엇입니까?

저는 PDO와 오라클을 사용하고 있습니다.

데이터베이스 연결을 한 번 사용하고 분당 한 번 쿼리하여 문제가있는 경우 사람들에게 경고합니다.

06:56:46: SUCCESS -- I take down the database after this success 
07:12:48: FAILURE - sent email 
07:13:48: FAILURE 
... 

내가 바로 이메일을 받고 싶지 : 데이터베이스가 다운 될 경우, 스크립트 분마다 확인하도록되어 내 프로세스는 다음과 같이보고 끝나는 그래서 문제가 있다는 걸 파악하기 전에 15 분을 기다립니다 15 분이지가 아니었다. 지속적인 데이터베이스 연결을 유지하는 방법이 있습니까? 아니면 쿼리를 준비하고 실행할 때마다 새 데이터베이스를 만들어야합니까? 그것이 도움이 경우

여기에 코드의 덩어리입니다 :

$last_email_time = null; // the time of the last error email sent 
$db_conn = null; 
$script_start_time = time(); 

while(true) { 
    $success = false; 

    // attempt to create a database connection 
    if(!$db_conn) { 
     try { 
      $db_connection_data = $g_pdo_connection_data['freedom']; 
      $db_conn = new PDO($db_connection_data['string'], $db_connection_data['user'], $db_connection_data['password']); 
      $db_conn->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
      $db_conn->setAttribute (PDO::ATTR_TIMEOUT, 60); 
      if(!$db_conn) { 
       throw new Exception("Unable to create a database connection"); 
      } 
     } catch(Exception $e) { 
      $last_email_time = handle_error($last_email_time, $e->getMessage()); 
      $db_conn = null; 
     } 
    } 

    // attempt a query 
    if($db_conn) { 
     try { 
      $q = $db_conn->prepare("SELECT 1 FROM DUAL"); 
      $q->execute(); 
      $q->closeCursor(); 
      if(!$q) { 
       throw new Exception("Unable to query the database"); 
      } 
      $success = true; 
     } catch(Exception $e) { 
      $last_email_time = handle_error($last_email_time, $e->getMessage()); 
     } 
    } 

    // remove the maintenance page if we were successful, else clear the connection 
    if($success) { 
     handle_success(); 
     $last_email_time = null; 
    } else { 
     $db_conn = null; 
    } 

    flush(); 
    if(ob_get_contents() != '') { 
     ob_flush(); 
    } 
    sleep(60); 
} 

답변

3

당신은 단지 루프를 통해 각 시간을 연결하고 데이터베이스에 연결을 해제 할 수있다. 그렇다면 매 분마다 예외를 잡을 것입니다. 성능에 부정적인 영향을 미치는지 테스트하기 위해 테스트해야합니다.

+0

그래, 그게 내가 걱정 한거야. –

관련 문제