简体   繁体   English

mongodb scala驱动程序casbah是否自动管理连接池

[英]Does mongodb scala driver casbah manage connection pool automatically

I am using mongo cashbah Scala driver i want to use connection pooling in my code but i am not sure my code is using connection pooling or not also i have read that we need to create MongoClient instance only once and reuse it again so i am not sure my code is reusing it or creating a new instance every time please guide me here is my code 我正在使用mongo cashbah Scala驱动程序,我想在代码中使用连接池,但是我不确定我的代码是否在使用连接池,我还读过我们只需要创建一次MongoClient实例,然后再次使用它,所以我不是确保我的代码每次都在重用它或创建一个新实例,请指导我这里是我的代码

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()
}

I have some questions regarding the above code 我对上述代码有疑问

  1. does this code is using connection pooling 这段代码使用连接池吗

  2. does this code reuses the mongoClient instance or its creating new instance every time 这段代码是否每次都会重用mongoClient实例或其创建新实例

  3. do i need to close connection after every query and if not when should i close the connection 我是否需要在每次查询后关闭连接?如果没有,我什么时候应该关闭连接

please guide me 请指导我

UPDATE UPDATE

i have made following changes to the code 我对代码进行了以下更改

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()
    }

does this updated code is reusing the mongo connection or its creating a new instance every time please guide me 每次更新后的代码是否重用mongo连接或其创建新实例时,请指导我

Please refer to this question . 请参考这个问题 So whenever you create MongoConnection actually under the hood connection pool is being created. 因此,无论何时创建MongoConnection实际上都会在引擎盖下创建连接池。

Regarding your particular code: you are creating MongoConnection each time you want to fetch record(s). 关于您的特定代码:每次要获取记录时,您都在创建MongoConnection Assign it to val , move it to upper level and use it all the time. 将其分配给val ,将其移至更高级别并一直使用。 Close it when application stops. 应用程序停止时将其关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM