2016-06-21 67 views
2

我正在使用mongo cashbah Scala驅動程序我想在我的代碼中使用連接池,但我不確定我的代碼是否正在使用連接池或者我也讀過,我們需要創建MongoClient情況只有一次,再重複使用它,所以我不知道我的代碼重用,或每次創建新實例,請指導我在這裏是我的代碼mongodb scala驅動程序casbah是否自動管理連接池

object MongoFactory { 

    val log = LoggerFactory.getLogger(this.getClass) 
    val config = ConfigFactory.load() 
    var client:MongoClient=null 

    private var SERVER:ServerAddress = { 
     val hostName=config.getString("db.hostname") 
     val port=config.getString("db.port").toInt 
    new ServerAddress(hostName,port) 
     }  
    private var DATABASE:String = config.getString("db.dbname") 


     def createConnection: MongoClient = { 
     log.info("server "+SERVER + "DATABASE" +DATABASE) 

     client=MongoClient(SERVER) 
     client 
     } 

     def getConnection : MongoClient = { 
     log.debug("In method getConnection") 
     if(client==null) 
     { 
     log.debug("mongoclient instance is null") 
     client=createConnection 

     log.debug("mongoclient is {}",client) 
     log.debug("Leaving method getConnection with returned value {}",client) 
     client 
     } 
     else 
     {  
     log.debug("Leaving method getConnection with returned value {}",client) 
     client 
     } 
     } 

     def getCollection(conn: MongoClient,collectionName:String): MongoCollection = { 
     conn(DATABASE)(collectionName) 
     } 

    def closeConnection(conn: MongoClient) { 
     conn.close 
     } 



class Abc 
{ 
def readAll() 
{ 
    var connection=MongoFactory.getConnection 
    var collection=MongoFactory.getCollection(connection, "User") 
    val cursor=collection.find() 
    while(cursor.hasNext) 
    { 
    // here fetching the data from database 
    } 
    MongoFactory.closeConnection(connection) 
} 

def readById()={ 
    var connection=MongoFactory.getConnection 
    var collection=MongoFactory.getCollection(connection, "User") 
    val cursor=collection.find(q.get) 
    while(cursor.hasNext) 
    { 
    // here fetching the data from database 
    } 
    MongoFactory.closeConnection(connection) 
} 
} 

object test extends App { 
MongoFactory.getConnection 

val abc=new Abc() 
abc.readAll() 
abc.readById() 
} 

我有一個關於上面的代碼

一些問題
  1. 這段代碼是使用conn撓度池

  2. 這段代碼重用mongoClient實例或它的每一次

  3. 我是否需要關閉每個查詢後連接,如果不是當 我應該關閉連接

創造新 實例

請指導我

UPDATE

我做了如下修改代碼

object MongoFactory { 

    val log = LoggerFactory.getLogger(this.getClass) 
    val config = ConfigFactory.load() 
    var client:MongoClient=null 

    private var SERVER:ServerAddress = { 
     val hostName=config.getString("db.hostname") 
     val port=config.getString("db.port").toInt 
    new ServerAddress(hostName,port) 
     }  
    private var DATABASE:String = config.getString("db.dbname") 

    val connectionMongo = MongoConnection(SERVER) 
    val collectionMongo = connectionMongo(DATABASE)("artGroup") 
} 
class Abc 
    { 
    def readAll() 
    { 
     val cursor=collectionMongo.find() 
     while(cursor.hasNext) 
     { 
     // here fetching the data from database 
     } 

    } 

    def readById()={ 
     val cursor=collectionMongo.find(q.get) 
     while(cursor.hasNext) 
     { 
     // here fetching the data from database 
     } 

    } 
    } 

    object test extends App { 

    val abc=new Abc() 
    abc.readAll() 
    abc.readById() 
    } 

做這個更新的代碼被重用蒙戈連接或它創建一個新的實例每次請指導我

回答

2

請參閱本question。因此,無論何時創建MongoConnection實際上正在創建連接池。

關於您的特定代碼:您每次要獲取記錄時都會創建MongoConnection。將其分配給val,將其移至上一級並始終使用它。應用程序停止時關閉它。

+0

你可以通過例子來解釋它會很好,非常有幫助 – swaheed

+0

現在看起來不錯 – tkachuko

相關問題