简体   繁体   中英

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

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

  3. do i need to close connection after every query and if not when should i close the connection

please guide me

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

Please refer to this question . So whenever you create MongoConnection actually under the hood connection pool is being created.

Regarding your particular code: you are creating MongoConnection each time you want to fetch record(s). Assign it to val , move it to upper level and use it all the time. Close it when application stops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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