2014-02-26 5 views
0

Mongo DB에서 데이터를 가져오고 집계하는 클래스가 있습니다. 이것은 모두 잘 작동합니다 ... 동일한 쿼리에서 동일한 클래스의 연결에 대해 여러 쿼리를 실행했습니다. 그러나, 나는이 페이지를 새로 고침 할 때마다, 새로운 접속이 열립니다 ... 내 mongod 출력에서 ​​볼 수 있습니다 :이 카운트 모든 페이지 새로 고침과 함께 볼PHP - MongoClient 연결이 겹쳐져 있지 않습니다.

[initandlisten] connection accepted from 127.0.0.1:46770 #12 (6 connections now open) 

합니다. 이것은 괜찮을 것이라고 나는 생각한다. 그러나 연결은 절대로을 닫는 것처럼 보입니다.

잠시 후 연결/잠금이 Mongo에서 너무 많은 메모리를 차지하며 더 이상 쿼리를 실행할 수 없습니다.

이 dev 환경은 Mongo의 32 비트 버전이므로이 문제로 인해이 문제가 발생하는지는 알 수 없습니다. (우리의 prod env는 64 비트이고, 지금 dev env를 바꿀 수 없습니다.)

내 질문 : 특정 사용자에 대한 모든 쿼리가 완료된 후 연결을 닫아야합니까? 연결 풀을 어떻게 처리해야합니까?

서비스 클래스 :

protected function collection(){ 
    if(!isset($this->collection)){ 
     $this->collection = $this->db()->selectCollection($this->collectionName); 
    } 
    return $this->collection; 
} 

을 그리고 쿼리가 같은 수행됩니다 :

$results = $this->collection()->aggregate($ops); 

이인가 쿼리가

class MongoService{ 
    protected $mongoServer; 
    public function setSpokenlayerMongoServer($mongoServer) 
    { $this->mongoServer = $mongoServer; } 

    protected $mongoUser; 
    public function setSpokenlayerMongoUser($mongoUser) 
    { $this->mongoUser = $mongoUser; } 

    protected $mongoPassword; 
    public function setSpokenlayerMongoPassword($mongoPassword) 
    { $this->mongoPassword = $mongoPassword; } 

    protected $conn; 
    public function setServiceConnection($conn) 
    { $this->conn = $conn; } 

    public function connect(){ 
     try { 
      $this->conn = $this->getMongoClient(); 
     } catch(Exception $e) { 
      /* Can't connect to MongoDB! */ 
      // logException($e); 
      die("Can't do anything :("); 
     } 
    } 

    public function getDatabase($name){ 
     if(!isset($this->conn)){ 
      $this->connect(); 
     } 
     return $this->conn->$name; 
    } 

    protected function getMongoClient($retry = 3) { 
     $connectString= "mongodb://".$this->mongoUser.":".$this->mongoPassword."@". $this->mongoServer.""; 
     try { 
      return new MongoClient($connectString); 
     } catch(Exception $e) { 
      /* Log the exception so we can look into why mongod failed later */ 
      // logException($e); 
     } 
     if ($retry > 0) { 
      return $this->getMongoClient(--$retry); 
     } 
     throw new Exception("I've tried several times getting MongoClient.. Is mongod really running?"); 
    } 
} 

그리고 서비스 클래스에

올바른 행동?

답변

0

Azure를 사용하는 경우 알려진 문제, 다른 플랫폼에서 발생할 수있는 문제.

하나의 옵션은 몽고의 구성을 변경하는 것입니다 : 이것은 5 분 후에 모든 유휴 연결을 죽일

MongoDefaults.MaxConnectionIdleTime = TimeSpan.FromMinutes(5); 

.

관련 문제